Search code examples
c++scriptingembeddinghybrid

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.


Solution

  • 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.