|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Accessing functions stored in a table using luabindHello!
I have stored some functions in a lua table and i want to access them using C++. The script file is as follows: ------------------------------------------------------------------------------- -- create the StateAcON state ------------------------------------------------------------------------------- StateAcON = {} StateAcON["Enter"] = function() print ("[Lua]: AC is turning on...") end StateAcON["Execute"] = function() print ("[Lua]: WOOSHHHHHHHHHHhhhhhh") end StateAcON["Exit"] = function() print ("[Lua]: AC is tuning off...") end The C++ code is listed below: extern "C" { #include <lua.h> #include <lualib.h> #include <lauxlib.h> } #include <iostream> #include <luabind/luabind.hpp> using namespace std; int main() { //create a lua state lua_State *pLua = lua_open(); luaL_openlibs(pLua); //open luabind luabind::open(pLua); /* load and run the script here */ if (int error = luaL_dofile(pLua, "script.lua") != 0) { // if there is an error on any of these functions it is left on top of the stack as a string. cout << lua_tostring(pLua, -1) << "\n"; return 0; } // Luabind luabind::object states; states = luabind::globals(pLua); //states["StateAcON"]; ??? //states["Execute"](); ??? //tidy up lua_close(pLua); return 0; } I am not too sure how to proceed after we access the global lua state. Basically what I want to do is to access a function stored inside a lua table from within C++. First i need to access the table and then the function stored in it. Thanks, fb ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ luabind-user mailing list luabind-user@... https://lists.sourceforge.net/lists/listinfo/luabind-user |
|
|
Re: Accessing functions stored in a table using luabindHello Again!
I solved the problem using the following statement: luabind::globals(pLua)["StateAcON"]["Execute"](); But when I try to do this: luabind::object states; states = luabind::globals(pLua); states["StateAcON"]["Execute"](); The script is executed fine, but the program crashes upon termination. The moment I assign the returned object from globals the program crashes upon termination after executing the script successfully. The moment I comment the following line: states = luabind::globals(pLua); the crash vanishes. This happens irrespective of the following statement: states["StateAcON"]["Execute"](); The problem is an unhanded win32 exception at line 169 in ref.cpp (according to the debugger). This is the statement it breaks to: lua_rawgeti(L, t, FREELIST_REF); I have tried this in a minimalistic application. The moment I assign the object returned by globals to a local instance of object, the application runs fine but crashes upon exit. On the contrary, using a nameless temporary works fine as I mentioned above. Any ideas? I tried this too but no luck (same result): luabind::object states; states = luabind::globals(pLua)["StateAcON"]; luabind::object foo = states; states["Execute"](); Thanks, fb,
|
|
|
Re: Accessing functions stored in a table using luabindChances are, the problem is that the destructor for "states" is being called after you close the Lua state "pLua". The destructor calls some code from the Lua API, and if you've closed pLua already, you're going to get a pointer error. To get around this, I generally make instances of luabind::object pointers so I can manually delete them before I close the state (obviously not necessary if the luabind::object is short-lived).
- James On 9/27/07, framebuffer <framebuffer@...> wrote:
[snip] ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ luabind-user mailing list luabind-user@... https://lists.sourceforge.net/lists/listinfo/luabind-user |
|
|
Re: Accessing functions stored in a table using luabindDear James,
Thanks one more time for saving the day! The following solution worked great! luabind::object *pStates = new luabind::object; *pStates = luabind::globals(pLua); (*pStates)["StateAcON"]["Execute"](); delete pStates; Thanks! fb.
|
| Free embeddable forum powered by Nabble | Forum Help |