Jump to content
  • Advertisement
Sign in to follow this  
lude

Lua global variable access

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

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!