Sign in to follow this  

Lua/C++ Object Management

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

In the game I'm currently working on with a team of friends, our core functionality is all written in C++, but some of the people on the team would like it if they could code a lot of the gameplay-centric stuff in a scripting language. I've looked into a handful of different languages and tried gauging their ease of embedding versus features they provide and decided to give Lua 5.1 a try, using tolua++ as my binding library. Problem is as follows. Say I have some kernel object in C++ that holds and manages a list of tasks that gets updated every game tick, where the class of each task actually inherits from some Task class that defines the basic interface (so you might have a RenderFrameTask, PlayAudioTask, etc.). Individual tasks are free to add and remove tasks to/from the task list in response to changes in the game state. For example's sake, let's say we have two tasks, FooTask and BarTask, and at some point when FooTask is running, it spawns a new BarTask and adds it to the kernel. In the C++ code, it looks roughly like this:
void FooTask::update( float deltaMS ) {
    // ...

    // TaskPtr is basically boost::shared_ptr< Task >
    TaskPtr bar = TaskPtr( new BarTask );
    kernel.addTask( bar );

    // from here on out the TaskPtr is managed by the kernel

    // ...
}
Now we want to script the behavior (essentially the update() method) of FooTask and BarTask using Lua. The question I have is, assuming the classes have been properly bound, is it possible to instantiate an object of a C++ class from a Lua script and then have its lifetime and memory resources be managed by the C++ code (i.e., so that it doesn't get garbage collected by the Lua environment), effectively passing the C++ object created in Lua back to C++? Also, could I get the same behavior if I used raw pointers instead of a shared pointer class? I've read through the Lua documentation and ran across the idea of light user data, and also noticed in the tolua++ documentation, it provides Lua functions like takeownership() and releaseownership(), but I don't have enough experience to tell me if these will help or not. Any thoughts or suggestions are appreciated.

Share this post


Link to post
Share on other sites
Never mind. After spending more time with Google, I finally found what I was looking for (apparently the search keywords I was looking for in this case were "transfer ownership".. doh!). Anyways, I found this post which apparently describes something close to what I want to do. It uses something called an "adopt policy" and uses luabind instead of tolua++. I can only guess that the takeownership() and releaseownership() module methods tolua++ provides would've done the same thing (just you would have to do that on the Lua end instead of the C++ end). After spending some time comparing the two, I'm going to opt to give luabind a try instead. Its binding mechanism appeals to me a little more, it seems to make ownership transfer and a features other things a bit more transparent to the Lua scripter, and plus the latest SVN checkout is reported to be stable with Lua 5.1 (the last official release apparently only supports 5.0).

Share this post


Link to post
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this