• Advertisement
Sign in to follow this  

Lua integration with C++ pointers

This topic is 1367 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 trying to share pointers into lua (not for lua to use) but to pass back into the program when required, can I just return the pointer as a integer and then return it back to the program as a integer? the only problem I can see is if I compile as 64bit, the pointer becomes the size of a long which would overflow the integers, meaning only 32bit would match lua and work. am I right in thinking this or does someone have tutorial on implementing c++ objects into lua to be used without constant function calls to get the variables of a pointer on the c++ side.

 

Edit: the question wasn't really specific, does anyone have a methodology of exposing C++ objects to Lua?

Edited by Joshhua5

Share this post


Link to post
Share on other sites
Advertisement

I'm a bit confused. You say you want to share a pointer with Lua, but not because Lua should use it?

 

It's possible to save a C++ (smart) pointer in a Lua variable for easier access. If you would be using for example this wrapper that would require one line of code. Could you please explain what you mean?

Share this post


Link to post
Share on other sites

You can pass pointer with lua_pushlightuserdata(). Lua won't be able to do anything with it, except passing it around.

Edited by vstrakh

Share this post


Link to post
Share on other sites
Light user data is the way to go if you literally just want to pass a pointer around. That's all a light user data is: a void* that is passed around in Lua values which can't be manipulated or modified by Lua itself.

A problem with light user data, however, is that you can't tell what _kind_ of pointer it is. It really is just a void*. So a C++ function bound to Lua could have _any_ light user data passed to it and you have no way to distinguish whether it's a GameObject* or a Texture*, unless you build your own system outside of Lua to identify these objects from a void*.

Full user data lets you attach a unique metatable to values which can be used to distinguish the types of the pointers they wrap. When you retrieve the void* encapsulated by the user data you can also grab the metatable and make sure it matches the one you expect so that you can tell those game objects and textures apart. Full user data is heavier-weight than "light" user data (obviously). There was a patch posted recently to the Lua user list that added a way to type-tag light user data without the overhead of full user data, if you really need the extra performance (you very well might not).

Share this post


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

  • Advertisement