Jump to content
  • Advertisement
Sign in to follow this  
Kurasu1415

Lua send reference to C++ class to another class

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

If you have a class that creates an instance of Lua and then you create a new instance of a class inside of Lua, is there a way to return a reference of the new class back to the class that creates the Lua instance? For example, I have a Manager class that manages enemies. The enemies are created via a Lua script. Each time an enemy is created in Lua, the Manager class needs a reference to the new enemy. Is this possible, and does anyone know how to do it?

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

If you have a class that creates an instance of Lua and then you create a new instance of a class inside of Lua, is there a way to return a reference of the new class back to the class that creates the Lua instance? For example, I have a Manager class that manages enemies. The enemies are created via a Lua script. Each time an enemy is created in Lua, the Manager class needs a reference to the new enemy. Is this possible, and does anyone know how to do it?

Thanks in advance.


Yes, you can use lightuserdata in lua to manage a reference to a C/C++ object.

Share this post


Link to post
Share on other sites
First, if I understand correctly, though Lua creates enemy objects, the objects are actually your C++ objects and created by your C++ code.
Right? If not, and enemy is some kind of Lua table, maybe you go wrong.

Then, when your C++ code is creating any Enemy object for Lua, C++ must create Enemy as a user data (not light user data).
Then you create an enemy object and put the pointer to user data.

Now your C++ code has a pointer to the enemy, and Lua is happy to accept the enemy as a user data.

If you want deep understand, you can read the source code of MLuabind (not luabind).
It's much simpler reading than luabind.
You can see how it uses LuaCustomVariable to manage objects.

PS: by setting __GC in the user data meta table, your C++ code can get called when the user data is going to destroy, then you can free the object in __GC handler.

Share this post


Link to post
Share on other sites
Firstly when ever I hear a type which has "manager" as part of it's name, my first reaction is always you are doing it wrong.

It sounds like what you want it a Lua reference [1] yet you have to take into account that this reference by default will keep the object alive.


[1] http://www.lua.org/manual/5.1/manual.html#luaL_ref

Share this post


Link to post
Share on other sites
I think I may have found a way to solve my issue. Basically, I just want users to be able to easily create new instances of an object from Lua. I think I misunderstood how Lua works because if I understand correctly Lua doesn't actually create an instance of a class. So the best way would be to have all the creation of new instances handled in the manager and just have the script look something like this:

createNew("Enemy")

That way Lua just sends a String to the C++ causing the creation to happen.

Also, every object should have it's own Lua instance correct?

Thanks again,
Kurasu

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!