Read a list of parameters from a LuaRef using LuaBridge

503 views Asked by At

[RESOLVED]

I'm building a game engine that uses LuaBridge in order to read components for entities. In my engine, an entity file looks like this, where "Components" is a list of the components that my entity has and the rest of parameters are used to setup the values for each individual component:

-- myEntity.lua

Components = {"MeshRenderer", "Transform", "Rigidbody"}

MeshRenderer = {
    Type = "Sphere",
    Position = {0,300,0}
}

Transform = {
    Position = {0,150,0},
    Scale = {1,1,1},
    Rotation = {0,0,0}
}

Rigidbody = {
    Type = "Sphere",
    Mass = 1
}

I'm currently using this function (in C++) in order to read the value from a parameter (given its name) inside a LuaRef.

template<class T>
T readParameter(LuaRef& table, const std::string& parameterName)
{
    try {
        return table.rawget(parameterName).cast<T>();
    }
    catch (std::exception e) {
        // std::cout ...
        return NULL;
    }
}

For example, when calling readVariable<std::string>(myRigidbodyTable, "Type"), with myRigidbodyTable being a LuaRef with the values of Rigidbody, this function should return an std::string with the value "Sphere".

My problem is that when I finish reading and storing the values of my Transform component, when I want to read the values for "Ridigbody" and my engine reads the value "Type", an unhandled exception is thrown at Stack::push(lua_State* L, const std::string& str, std::error_code&).

I am pretty sure that this has to do with the fact that my component Transform stores a list of values for parameters like "Position", because I've had no problems while reading components that only had a single value for each parameter. What's the right way to do this, in case I am doing something wrong?

I'd also like to point out that I am new to LuaBridge, so this might be a beginner problem with a solution that I've been unable to find. Any help is appreciated :)

1

There are 1 answers

0
jorgmo02 On BEST ANSWER

Found the problem, I wasn't reading the table properly. Instead of

LuaRef myTable = getGlobal(state, tableName.c_str());

I was using the following

LuaRef myTable = getGlobal(state, tableName.c_str()).getMetatable();