I have a problem with luabind, or at least i expect its a problem
I have an entity class that ive registered with lua,
ideally i want to subclass it and override its functions, from there i want to send it back to c++ and store it
additionally i want to be able to call its new functions from c++ from the stored object/pointer
however im currently struggling to even get c++ to take the object of type cEntity* back? in the lua script i can load the class, call its variables and functions, i try and send it to takeClass or takebOject but it comes out as a blank class with nothing set on it
for example foo->name is "" rather than "Entity1" and id is 0 rather than 1
anyideas what im doing wrong? ive searched on google for at least a week now with no luck of understanding this problem and its completely halting my progress on my project?
//#######################################################################
// Test function
//#######################################################################
void luaTest::TakeClass(cEntity* foo)
{
cout << foo->name << endl;
}
void luaTest::TakeObject(luabind::object foo)
{
cEntity* foobar = luabind::object_cast<cEntity*>(foo);
cout << foobar->name << endl;
}
void luaTest::luabindClass(lua_State* L)
{
//Somewhere else
module(L)
[
class_<luaTest>("luaTest")
.def(constructor<>())
.def("TakeClass", &luaTest::TakeClass)
.def("TakeObject", &luaTest::TakeObject)
];
globals(L)["test"] = this;
}
//#######################################################################
// Entiy Class
//#######################################################################
class cEntity
{
public:
string name;
int id;
cEntity();
~cEntity();
static void luabindClass(lua_State* L);
};
//#######################################################################
cEntity::cEntity()
{
name = "NotSet";
id = 0;
}
cEntity::~cEntity()
{
}
void cEntity::luabindClass(lua_State* L)
{
module(L)
[
class_<cEntity>("cEntity")
.def(constructor<>())
.def_readwrite("name", &cEntity::name)
.def_readwrite("id", &cEntity::id)
];
}
//#######################################################################
// Lua File
//#######################################################################
entity = cEntity();
entity.name = "Entity1";
entity.id = 1;
test:TakeClass(entity);
test:TakeObject(entity);
//#######################################################################
//#######################################################################
// main
//#######################################################################
....
/* run the script */
if (luaL_dofile(L, "avg.lua")) {
std::cout << lua_tostring(L, -1) << std::endl; // Print out the error message
}
....
//#######################################################################
https://gist.github.com/take-cheeze/7264dbf1ea6e08a2d24a
#include <iostream>
#include <string>
#include "luabind/luabind.hpp"
extern "C" {
#include "lauxlib.h"
#include "lualib.h"
}
class cEntity
{
public:
std::string name;
int id;
cEntity();
~cEntity();
std::string getName() { return name; }
void setName(std::string n) { name = n; }
int getID() { return id; }
void setID(int n) { id = n; }
virtual void testFunction(){};
static void luabindClass(lua_State* L);
};
//#######################################################################
// Test function
//#######################################################################
struct luaTest {
void TakeClass(cEntity* foo)
{
std::cout << foo->name << std::endl;
}
void TakeObject(luabind::object foo)
{
cEntity* foobar = luabind::object_cast<cEntity*>(foo);
std::cout << foobar->name << std::endl;
}
void luabindClass(lua_State* L)
{
//Somewhere else
luabind::module(L)
[
luabind::class_<luaTest>("luaTest") // < "Animation" how we want to name the Class in the Lua runtime
.def(luabind::constructor<>())
.def("TakeClass", &luaTest::TakeClass)
.def("TakeObject", &luaTest::TakeObject)
];
luabind::globals(L)["test"] = this;
}
};
//#######################################################################
// Entiy Class
//#######################################################################
//#######################################################################
cEntity::cEntity()
{
name = "NotSet";
id = 0;
}
cEntity::~cEntity()
{
}
void cEntity::luabindClass(lua_State* L)
{
luabind::module(L)
[
luabind::class_<cEntity>("cEntity") // < "Animation" how we want to name the Class in the Lua runtime
.def(luabind::constructor<>()) // < Binds the empty constructor
.def_readwrite("name", &cEntity::name)
.def_readwrite("id", &cEntity::id)
];
}
char const* script =
"entity = cEntity();\n"
"entity.name = \"Entity1\";\n"
"entity.id = 1;\n"
"\n"
"test:TakeClass(entity);\n"
"test:TakeObject(entity);\n";
int main() {
lua_State* L = lua_open();
luabind::open(L);
cEntity::luabindClass(L);
luaTest test;
test.luabindClass(L);
/* run the script */
if (luaL_dostring(L, script)) {
std::cout << lua_tostring(L, -1) << std::endl; // Print out the error message
}
lua_close(L);
}
Printed:
$ ccache clang++ -lluabind -llua -g3 ~/test.cxx && ./a.out
Entity1
Entity1
So there maybe problem in script loading. Is there any error message printed?
(Sorry that I can't answer this in IRC. Took a while to create testing env)