views:

2178

answers:

9

I want to move verious parts of my app into simple scripts, to allow people that do not have a strong knowleagd of c++ to be able to edit and implement various features.

Because its a real time app, I need to have some kind of multi-tasking for these scripts, idealy I want it so that the c++ app calls a script function which then continues running (under the c++ thread) untill either a pause point (Wait(x)), or it returns. In the case of it waiting the state needs to be saved ready for the script to be restarted the next time the app loops after the duration has expired.

The scripts also need to be able to call c++ functions, idealy using the c++ classes rather than just plain functions which wrap the c++ classes.

I don't want to spend a massive amount of time implementing this, so using an existing scripting lanaguage is prefered to trying to write my own. I heard that Python and Lua can be integrated into a c++ app, but I do not know how to do this to achive my goals.

  • The scripts must be able to call c++ functions
  • The scripts must be able to "pause" when certain functiosn are called (eg Wait), and be restarted again by the c++ thread
  • Needs to be pretty fast, this is for a real time app and there could potentialy be alot of scripts running.

I can probebly roll the multi-tasking code fairly easily, provideing the scripts can be saved, and restarted (posibly by a diffrent thread to the origenal)

+2  A: 

Take a look at the Boost.Python library. It looks like it should be fairly straightforward to do what you want.

Ferruccio
+7  A: 

I can highly recommend that you take a look at Luabind. It makes it very simple to integrate Lua in your C++ code and vice versa. It is also possible to expose whole C++ classes to be used in Lua.

TrolleFar
+3  A: 

You can definitely do what you want with Python. Here are the docs on embedding Python into an application. I'm pretty sure Lua would work too, I'm just less familiar with it.

You're describing cooperative multi-tasking, where the script needs to call a Break or Wait function periodically. Perhaps a better solution would be to run the scripting language in its own thread, and then use mutexes or lock-free queues for the interfaces between the scripting language and the rest of your program. That way a buggy script that doesn't call Break() often enough can't accidentally freeze your program.

dmazzoni
+4  A: 

Your best bet is to embed either lua (www.lua.org) or python (www.python.org). Both are used in the game industry and both access extern "C" functions relatively easily with lua having an edge here (because data types are easier to translate between lua and C). Interfacing to C++ objects will be a bit more work on your end, but you can look up how to do this on Google, or on lua or python discussion forums.

I hope that helps!

Kevin
Ok, they both seem good, but do they support the pausing/restarting and the way I want them to work with threads (eg using the thread that called the script untill it either returns or pauses)
Fire Lancer
I haven't used either of these in a threaded environment. You might want to post this question on lua and python specific forums.
Kevin
+1  A: 

You can also embed C/C++ scripts using Ch. I've been using it for a game project I'm working on, and it does well. Nice blend of power and adaptability.

mos
+13  A: 

You can use either Lua or Python. Lua is more "lightweight" than python. It's got a smaller memory footprint than python does and in our experience was easier to integrate (people's mileage on this point might vary). It can support a bunch of scripts running simultaneously. Lua, at least, supports stopping/starting threads in the manner you desire.

Boost.python is nice, but in my (limited) experience, it was difficult for us to get compiling for our different environments and was pretty heavyweight. It has (in my opinion) the disadvantage of requiring Boost. For some, that might not be a problem, but if you don't need Boost (or are not using it), you are introducing a ton of code to get Boost.python working. YMMV.

We have built Lua into apps on multiple platforms (win32, Xbox360 and PS3). I believe that it will work on x64. The suggestion to use Luabind is good. We wound up writing our own interface between the two and while not too complicated, having that glue code will save you a lot of time and perhaps aggravation.

With either solution though, debugging can be a pain. We currently have no good solution for debugging Lua scripts that are embedded into our app. Since we haven't used python in our apps I can't speak to what tools might be available there, but a couple of years ago the landscape was roughly the same -- poor debugging. Having scripting to extend functionality is nice, but bugs in the scripts can cause problems and might be difficult to locate.

The Lua code itself is kind of messy to work with if you need to make changes there. We have seen bugs in the Lua codebase itself that were hard to track down. I suspect that Boost::Python might have similar problems.

And with any scripting language, it's not necessarily a solution for "non-programmers" to extend functionality. It might seem like it, but you will likely wind up spending a fair amount of time either debugging scripts or even perhaps Lua.

That all said, we've been very happy with Lua and have shipped it in two games. We currently have no plans to move away from the language. All in all, we've found it better than other alternatives that were available a couple of years ago. Python (and IronPython) are other choices, but based on experience, they seem more heavy handed than Lua. I'd love to hear about other experiences there though.

Mark
How exactly have you got Lua working with your games? eg does each script run all the time in it's own thread untill finished, is each script handled like a function (ie running in the calling thread untill it returns), or like its own mini-program (like I described above)?
Fire Lancer
Lua scripts can run in "threads" and don't necessarily need to be handled inline like functions. It will do what you want.
Mark
Ok. I'll go take a more detailed look at it then and write some tests to work out exactly how I want to integrate it with my code base :)
Fire Lancer
+2  A: 

Take a look at SWIG. I've used it to interface with Python, but it supports many other languages.

Harold Ekstrom
+2  A: 

One more vote for Lua. It's small, it's fast, it doesnt consume much memory (for games your best bet is to allocate big buffer at the initialization and re-direct all Lua memory allocations there). We used tolua to generate bindings, but there are other options, most of them much smaller/easier to use (IMO) than boost.python.

yrp
+2  A: 

As for debugging Lua (if you go that route), I have been using DeCoda, and it has not been bad. It pretends to be an IDE, but sorta fails at that, but you can attach The debugging process to visual studio, and go down the call stack at break points. Very handy for Tracking down that bug.