Jump to content

  • Log In with Google      Sign In   
  • Create Account

Lua integration with C++ pointers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Joshhua5   Members   -  Reputation: 478

Like
0Likes
Like

Posted 26 April 2014 - 10:13 AM

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, 26 April 2014 - 10:44 AM.


Sponsor:

#2 ProtectedMode   Members   -  Reputation: 1287

Like
1Likes
Like

Posted 26 April 2014 - 10:33 AM

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?



#3 Joshhua5   Members   -  Reputation: 478

Like
0Likes
Like

Posted 26 April 2014 - 10:35 AM

I want to pass a pointer of a entity to lua, so the lua can then pass the entity pointer into a function.



#4 vstrakh   Members   -  Reputation: 518

Like
1Likes
Like

Posted 26 April 2014 - 11:39 AM

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


Edited by vstrakh, 26 April 2014 - 11:39 AM.


#5 SeanMiddleditch   Members   -  Reputation: 7269

Like
2Likes
Like

Posted 26 April 2014 - 11:47 AM

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).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS