Lua global variable access

Started by
0 comments, last by lude 16 years, 1 month ago
I'm currently attempting to embed Lua in my game engine and am using tolua++ to help bind my C++ code to Lua. I want to be able to take a variable from C++ and access it from within Lua as a global variable. For example, if I had a specific Actor class bound into Lua which represents the player, I would want to be able to manipulate that player using syntax similar to:

player:move_to(4,3)
Pushing the player object as light user data like

lua_pushlightuserdata(L, &player);
lua_setglobal(L, "player")
does not work because as I understand it, light user data is nothing more than a pointer to Lua. One workaround using the above global setting code would be to expose another global method which takes a player, and its new position as arguments. This would then call the given player's move_to method like so:

move_to(player, 4, 3)
Well this works, but I would really rather have Lua know that the user data I gave it from C++ was of a certain type and be able to operate on it directly using the syntax I first posted above. Also it is annoying to have to create wrapper functions for everything. Is this possible? If not, how is this usually done?
Advertisement
Of course I find a solution shortly after posting. It seems using the tolua++ function tolua_pushusertype allow me to use the syntax I want.

This topic is closed to new replies.

Advertisement