Scripting in cocos2d-x

The Problem:

I’m using plist files to define my terrain, and that plist can get quite large. Although it is essentially just a list of points, there’s additional data (such as the image used for the ‘sub-terrain’ and the sprite to use for the surface) too.

In some levels it would be nice to be able to:
a) Have a somewhat random terrain.
b) Calculate the terrain within given parameters
c) Have repetitive sections of terrain

Initially I thought of writing the various options in C++, then specifying various parameters in my plist file. But this has the disadvantage that, if I want to do a different type of terrain, it’s hard for me to do so without building a new version of the App.

But what if I could define the terrain and the algorithm in a text file – that could be downloaded without re-releasing the App?

This sounded like a good idea to me, so I started investigating scripting for cocos2d-x.

The most obvious solution would seem to be to use Lua – which comes with Cocos2d-x (or so it seems) but after spending some time, I couldn’t find any good info on adding Lua scripting to an existing C++ project.

Then I came across ChaiScript at and

So I spent quite a long time investigating, playing, swearing and asking questions of the very helpful and very very clever developer.

And now I think I have the basics…

In my next post I will document how to create a cocos2d-x project that includes ChaiScript scripts to define settings for the App.

Leave a Reply

Your email address will not be published. Required fields are marked *