Why boost python for AnKi?

Every game engine, at some level, needs to be able to compile and run code at runtime. You need, for example, to trigger a custom event when the hero pushed a button. The button should contain a bunch of code that may call an elevator or/and open a door or/and even kill the hero. All these custom events are possible by embedding a scripting language on top of the native language the game uses. Most of the game developers use native languages like C/C++ (very popular), Objective C (iPhone) and Java (other mobile platforms and rarely on PC). As for the scripting languages, the developers face many more choices with custom scripting languages (Unreal Script), lua, javascript (through various implementations), Python, Squirrel and more.

AnKi, just like every other game engine, needed a scripting language and the searching/prototyping started almost a year back. We have made prototypes for lua, javascript (V8 and SpiderMonkey), squirrel, Qt script, Python and others. Lets see why we chose boost python over others…

Key elements of choosing a scripting language

Lets analyze what are the key elements of choosing a scripting language for a game:

  • Glue code. The embedding is a process that, unfortunately, comes with additional development cost. The developer needs to expose (aka wrap) classes and functions by writing glue code. This is not an easy task and it varies from API to API. For example squirrel was designed to be an embedded language in games and its fairly easy to write glue code. Python on the other hand is not. Using python C API is very unproductive.
  • Speed. Some interpreter implementations offer faster responsiveness over others. For example Google’s V8 engine offers JIT (just in time) compilation, a feature that made Google’s Chrome one of the fastest browsers out there.
  • Platform support. The language binaries/libraries should be portable among popular gaming platforms like PC and consoles. In most cases this is not a problem because most of the interpreters are written in C/C++ which makes them fairly portable. There is a big “but” though. Some interpreters support JIT compilation, a feature that implies generation of machine dependent code at runtime. One example is Google’s V8 again. In order to use V8 you have to build only 32bit executables of your application. Ok that not a big deal, the big deal is that the generated  code is for x86 and ARM platforms and it wont run in console hardware without major modifications.
  • Popularity: Being a popular language means that there are lots of tutorials, documentation and people who can help you. Some languages are fairly new like Squirrel and they lack excessive support. Its a risk to use a language and have to switch later because of lack of support.

Pros and cons of the tested implementations

Lets see what are the pros and cons of all these implementations:

  • Lua: Fast, small memory footprint, very popular among game developers, moderate glue code writing, weird syntax (IMHO)
  • Qt script: Speed is undetermined, javascript, zero usage among game developers, very easy to write glue code, difficult to port to consoles, Qt dependent
  • V8: Smoking fast, javascript, moderate writing of glue code, non portable, bad documentation
  • SpiderMonkey family: Fast to smoking fast, javascript, moderate writing of glue code, the SpiderMonkey is portable, the TraceMonkey is portable but the JägerMonkey may not be due to JIT compiler, good documentation
  • Squirrel: Speed is undetermined, glue code writing is easy, designed for game embedding, not that many developers behind it
  • Python: Not the fastest around, huge in non-game developers and popular among game developers, lots of glue code when using C API but very very easy with boost python

*Note: As we can see javascript is very popular with various implementations but for games it may be a no go due to luck of support of operator overloading. The math library in AnKi uses operator overloading and to map this in javascripts requires different notations. It may be ugly.

Why boost python?

After creating (and still creating) prototypes we choose boost python. Boost python is a very smart wrapper on top of Python’s C API that makes the writing of glue code very very easy. Python is the most popular scripting language around, it may be slower than lua but for the py3k (Python 3.x) there is a JIT compiler under development. On the downside, boost python is template oriented and most of the time you find yourself in front of compiler errors with obfuscated cause. Also the documentation is lucking.

References

http://squirrel-lang.org
http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html
http://code.google.com/apis/v8/embed.html
https://developer.mozilla.org/en/JavaScript_C_Engine_Embedder%27s_Guide

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>