luabind - c++ to lua to c++ -
i have problem luabind, or @ least expect problem
i have entity class ive registered lua,
ideally want subclass , override functions, there want send c++ , store it
additionally want able call new functions c++ stored object/pointer
however im struggling c++ take object of type centity* back? in lua script can load class, call variables , functions, try , send takeclass or takeboject comes out blank class nothing set on it
for example foo->name "" rather "entity1" , id 0 rather 1
anyideas im doing wrong? ive searched on google @ least week no luck of understanding problem , halting progress on 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", ¢ity::name) .def_readwrite("id", ¢ity::id) ]; } //####################################################################### // lua file //####################################################################### entity = centity(); entity.name = "entity1"; entity.id = 1; test:takeclass(entity); test:takeobject(entity); //####################################################################### //####################################################################### // main //####################################################################### .... /* run script */ if (lual_dofile(l, "avg.lua")) { std::cout << lua_tostring(l, -1) << std::endl; // print out 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 want name class in 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 want name class in lua runtime .def(luabind::constructor<>()) // < binds empty constructor .def_readwrite("name", ¢ity::name) .def_readwrite("id", ¢ity::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 script */ if (lual_dostring(l, script)) { std::cout << lua_tostring(l, -1) << std::endl; // print out error message } lua_close(l); }
printed:
$ ccache clang++ -lluabind -llua -g3 ~/test.cxx && ./a.out entity1 entity1
so there maybe problem in script loading. there error message printed?
(sorry can't answer in irc. took while create testing env)
Comments
Post a Comment