Sign in to follow this  
McFury

Luabind::object .at() problem

Recommended Posts

McFury    144
Hi, I have a problem, i have set up a table in a LUA script that has a function stored in it:
State_Guard = {}

State_Guard["Execute"] = function(unit)

	print("Inside executing...")
end
I try to access it the following way from the Cpp (where m_CurrentState is a luabind::object)
//This is in the init code to set a starting state
m_CurrentState = luabind::get_globals(m_LuaState)["State_Guard"];

....

//This is called every cycle
m_CurrentState.at("Execute")(m_Owner);
It gets through the first cycle fine, and displays the print out in the script file, before this point 'm_CurrentState' had a type of 5 (which is the luabind enum for a table, which is correct). But after the .at() function has been called 'm_CurrentState' now has a type of 3 (which is the luabind enum for a number). So the next time it comes back around to call this .at() statement 'm_CurrentState' is a number, not a table!, and it crashes when trying to access it as a table. To me this makes no sense because i dont know how it can be cast into a number from a table just by calling the .at function(). Ive been trying to solve it for ages, I would be very thankful for any help, I really need to solve this :( ps. I know im using an older version of lua/luabind, but i dont really have the time to switch at this present moment.

Share this post


Link to post
Share on other sites
Cygon    1219
Tried this code

lua_State *myLuaState = lua_open();
luaL_openlibs(myLuaState);

// ...and here ;)
luaL_dostring(
myLuaState,
"State_Guard = {}\n"
"\n"
"State_Guard[\"Execute\"] = function(unit)\n"
" print(\"Inside executing...\")\n"
"end\n"
);

{
luabind::object guard = luabind::globals(myLuaState)["State_Guard"];
guard["Execute"](123);
guard["Execute"](123);
guard["Execute"](123);
}

lua_close(myLuaState);


And it prints "Inside executing..." three times. Using Lua 5.1.3, LuaBind from 2008-02-08 with Boost 1.14.1.

I've got the binaries (and sources) of what I'm using for download linked on my page: Quick Introduction to LuaBind.

Sorry, but I can't think of any other option than to upgrade.

Well, on the other hand, maybe there's a chance that your LuaBind version doesn't clean up the lua stack correctly and you could achieve something by either pushing an additional null value onto the stack before invoking the function or by popping one more value from the stack after calling the function. Maybe it also changes the behavior if the function returns something.

Share this post


Link to post
Share on other sites
McFury    144
Thank you so much for taking the time to respond so quickly :)

I just had a question, will the binaries on your site work on Microsoft Visual Studio 2003, or are they specifically for Visual Studio 2005/2008?

If they are 2005 specific, i would really appreciate it if you could give me a quick tut (or point me in the right direction) on how to compile the LUA libraries for 2003? :)

Cheers

Share this post


Link to post
Share on other sites
Cygon    1219
I've included binaries for Visual C++ 2005 SP1 and Visual C++ 2008, but I doubt any of them would work for the 2003 edition.

The archive on my site contains a file named library_sources.zip with the exact Lua and LuaBind snapshots I used to build the binaries. Maybe those could be used to compile Lua and LuaBind for Visual Studio 2003 (project files can be found in the "Build" subfolders).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this