Jump to content
  • Advertisement
Sign in to follow this  
Quasimodem

Passing Lua Objects to C

This topic is 4480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a tough LUA problem I'm hoping somebody has also experienced, though I cannot find any mention of it elsewhere in the forums. I am running Lua5.0 with toLua++ and can create C++ objects in my Lua scripts just fine. However, the problem occurs if I want to pass these C++ objects created in Lua back to C++. I can cast the return of lua_touserdata() to the correct type, but the object I get is not the same object that Lua knew about, specifically, all of its data members are uninitialized. For example, I have a class: class Account { public: int _x; Account(int x) { _x = x; } void printX() { printf("_x is: %i\n", _x); } }; and a function: static int showAccount(lua_State* L) { Account* account = (Account*)lua_touserdata(L, 1); account->printX(); return 1; } I bind this class and this function to Lua. In my Lua script, I call: myAcc = Account:new(100) myAcc:printX() showAccount(myAcc) The call within Lua to myAcc:printX() correctly prints out "100". However, the call from within the showAccount function prints out garbage; the void* returned by lua_touserdata() does not point to the same object that Lua originally instantiated. Are there any Lua experts out there with insight into this? My intent is to be able to run scripts on the fly that can instantiate and manage in-game objects, but still be able to register these objects with my C functions used for drawing and such or to have a script instantiate a new Item and add it the C Inventory object. Any advice is appreciated!

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure if this is the problem, but you call lua_touserdata(L, 1), which will return the userdata representation of the first object in the stack. But what if, hypothetically, the stack had other data on it before your C API function was called? Then the first item in the stack wouldn't be the userdata passed to your function. Maybe you could try lua_touserdata(L, -1), which will return the last object in the stack, which would be what you want; the first parameter passed to your function.
Thats just what I do, at least; is the stack guaranteed to be clean for called functions?
Another thing I see is that you return 1; in your C API function, which would return 1 value, the first parameter passed to the function, the Account object. Is that what you want?

Share this post


Link to post
Share on other sites
Not au-fait with toLua, do you create the Lua user data, and if so can you show the code that does it?
I've had this same problem where I was actually pushing a pointer address rather than the pointer itself (doh)!

Share this post


Link to post
Share on other sites
I have the exact same problem!

Aph3x: u were passing the address of the pointer? from lua? how is that possible? in lua, for example, if i had a class i binded in c++ called Vector. in lua script i would do "v = Vector()". Then down along the code, i pass the "v" down the function as Quasimodem did. is the v's pointer, or address of pointer?

and the lua_touserdata returns null in C++, but lua_topointer returns garbage.

im using lua 4.0.

thx

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!