Using C++ and scripting together
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.
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.
Run a Python process? Nooo....
Embed.
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/
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.
精彩评论