Sign in to follow this  

Lua global variable access

This topic is 3585 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

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?

Share this post


Link to post
Share on other sites

This topic is 3585 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.

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