Using lua and luabind has been very difficult, yet the results I've gotten have made it worthwhile.
I have managed to get everything to compile without errors or warnings. I also managed to bind my classes, their functions, and their variables to Lua. I even got some variables bound to lua to sync with my variables in C++.
In fact there is one thing left that I need to get working before I can apply everything to all my other classes.
I need to access an std::vector where I store my assets.
I made these two classes to manage game entities:
class entity: public sprite, public mesh, public grid
{
private:
std::string name;
bool render;
// 0 for sprite, 1 for 3D mesh
int type;
// xyz location in space. Z will not be used in 2D games.
float x, y, z;
// xyz rotation. x and y will not be used in 2D games.
float rotx, roty, rotz;
public:
entity();
// lua communication functions
void setName(std::string name);
std::string getName();
void setRender(int r);
int getRender();
void setType(int t);
int getType();
void setLocX(float Lx);
void setLocY(float Ly);
void setLocZ(float Lz);
float getLocX();
float getLocY();
float getLocZ();
void setVelX(float v);
void setVelY(float v);
void setVelZ(float v);
void draw();
};
class entityControl
{
int noOfEntities;
public:
std::vector<entity> entityData;
entityControl();
void newEntity();
// lua get private vars
int getNoOfEntities();
};
This is the function I use to add a new entity into the array:
void entityControl::newEntity()
{
entity x;
//entityData.insert(it, x);
entityData.push_back(x);
noOfEntities++;
}
My binding code (I'm only showing whats being bound):
class_<entity>("entity")
.def(constructor<>())
.property("render", &entity::getRender, &entity::setRender)
.property("type", &entity::getType, &entity::setType)
.property("x", &entity::getLocX, &entity::setLocX)
.property("y", &entity::getLocY, &entity::setLocY)
.property("z", &entity::getLocZ, &entity::setLocZ)
.def("setVelX", &entity::setVelX)
.def("setVelY", &entity::setVelY)
.def("setVelZ", &entity::setVelZ),
class_<entityControl>("entityControl")
.def_readwrite("entityData", &entityControl::entityData, return_stl_iterator)
.property("noOfEntities", &entityControl::getNoOfEntities)
.def("newEntity", &entityControl::newEntity)
My lua script:
print(entityManager.noOfEntities)
-- accessing this variable gives me no problem. It prints 0 like it should
entityManager.newEntity()
-- this hangs the program. I got it to work once, but...
entityData[1].setType(0)
-- if the above command worked then this would crash the program
And finally the resulting run-time error:
terminate called after throwing an instance of 'luabind::error'
what(): lua runtime error
Program received signal: “SIGABRT”.
So now I'm thinking that I'm taking the wrong approach to accessing the elements of the vector. And even if I could properly bind and access a vector with lua, I would still need to get every value I changed back into C++.
If I could do this, then I could apply it to all my other classes and complete the basics of my game engine.
However, as I've been writing this post I feel that if I could get this working, I should make it easier for other people using luabind and the good people on these forums by making a video walk-through explaining how to set up and use lua and luabind. This way less luabind questions might be asked on this forum.
So how do I access an std::vector from lua, and send it back to C++?