Category Archives: Uncategorized

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.

Sometime I hate Computers

so a nice chap on the cocos2d-x site has written a nce utility function to take a string like


and return 10 filenames from 0 to 9.

Handy for places where I have animations, for example.

So I implemented it, but it didn’t handle [9-0] (i.e. down from 9 to 0)

So I fixed that & tried to do a pull request

had a few problems – but got there. Then turned out my code was crap.

Made the change.

Pushed again.

During this process, XCode seemed to get its knickers in a knot.

Long and short – every single project in my Projects folder was deleted!

All of them!

Holy hell!