🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Thoughts on binding C++ objects to Lua scripts

Started by
0 comments, last by The_Incubator 20 years, 5 months ago
I''m looking at using Lua to script my C++ application... I''m pretty well settled on Lua due to it''s size and ease of embedding. Now what I''m looking at, is since the application is written in C++, and the game''s subsystems are C++ classes. Now, needless to say I want to use scripts to script the activity of these subsystems, or at least objects that live in them (like running a script to control the behavior of a game object living in my game object manager). Since Lua is written in C, it''s easy to bind a C function to Lua. No problem, I''ve done that. But what I need to do is create a wrapper layer of C functions that will be exported to Lua and, when called, will activate the appropriate C++ objects. At the moment, my game subsystems are data members in an application class... I realize now that, with Lua being a C api, I don''t exactly have the ability to either directly call methods of my application class through Lua, or to directly call methods of my game systems classes through Lua. Is the best way around this to simply make my game subsystems global objects, and then write a wrapper that calls their methods and is exposed to Lua? This at least seems to be the most obvious and painless approach, but it gives up some flexibility (the wrapper will always be hard-coded so that it can only operate on the global objects), but I really don''t think that will be a disadvantage in my application, especially compared to the hassle that might be needed for a more general solution. Any other thoughts on how to bind Lua function calls to specific, already instantiated C++ objects? It would be nice if I could have functionality that allowed me, for example, to associate a script with a given game object, and if the script had a function call like takeDamage( 10 ), it would be set up so it called the takeDamage method of the object associated with the script, without needing an extra parameter to explicitly tell which object to call the method on. I think in this case I am asking too much of a scripting language in general and Lua in particular. Thanks for any advice [Edited by - The_Incubator on June 14, 2006 2:33:56 PM]
Advertisement
The best thing I have seen for this is LuaBind. It allows you to bind C++ methods, inherit from C++ classes, create object instances in C++ and have Lua take ownership of them and vice versa.
I haven''t yet used gotten to the stage of using it in my engine but it is the best thing I have found so far.

This topic is closed to new replies.

Advertisement