views:

124

answers:

4

I am developing simple games as a hobby. For my new project, I want some parts to be scriptable. I am familiar with python but don't mind learning new languages. Here is the question:

I am planning to implement path-finding, field-of-vision, collision detection etc. in C++ but want to use scripts for AI state machines, scripted events. What type of structure is used for this kind of job? I imagine I can make a C++ program run a python process which in turn calls C++ methods, but it seems inefficient. Another idea is to develop a library to be called from python, which doesn't sound very attractive either. What is the regular way of doing this(except for writing my own language and parser?) I heard lua is popular for embedding into C programs.

+4  A: 

Run a Python process? Nooo....

Embed.

Ignacio Vazquez-Abrams
+2  A: 

From personal experience I can highly recommend Google's Javascript engine V8. It's very performant, written in C++, is trivially easy to embed, has no other dependencies and a really beautiful native interface.

http://code.google.com/p/v8/

flitzwald
+6  A: 

From my personal experience, both lua and tcl have fantastic C APIs for embedding. Both languages are very simple. If you're writing a command interface, I'd probably say go with tcl, but if you're just using an embedded interpreter, I'd recommend lua. Given that you're using C++, you might also want to look into the luabind API, I have heard good things about it.

For AI scripting, or other state machine-type things, this blog post by Zed Shaw is a good read. Coroutine-based AI code can look a lot nicer for complex scripts, rather than managing an enormous pile of states and their transitions.

If you're using python, you might be better off extending rather than embedding.

Jack Kelly
+1 for the extend vs embed link and lua props
Matt Joiner
+2  A: 

I agree that Tcl and Lua are one of the easiest to embed in a c/c++ application. Mainly because that was a design decision from the very start. Another language that was designed for embedding is Guile.

If you are interested in embedding a scripting language in your c++ application take a look at Swig. Swig can automatically create the glue code for a number of scripting languages including python which you already know. The main advantage is that it handles many different scripting languages.

srean
Guile bills itself as an embedding language, but it never felt like that was the right place for it, compared to how nice the lua or tcl APIs are. It seems like it's mutated into a general-purpose scripting language more than an embedding language. Have a +1, especially for the mention of swig.
Jack Kelly
@Jack Kelley I agree :) Guile developers tend not to optimize the language implementation in the interest of ease of embedding. But I am not sure how much value that adds.
srean