3 notes &
From LUA to JSON
I was planning on writing an article about how to integrate LUA into a game using iPhone Wax. This is how I wrote the AI for iBots Launch and how I wrote the first prototype of Chicken Pox. There were a few advantages to this strategy but I think in the end there were a lot more disadvantages. So this is an article on why I’m not using wax or LUA any more.
Why use LUA anyway?
I’m used to coding in ruby and javascript which are much more concise languages with a lot more runtime flexibility than objective-c. Also I really like using closures and I was targeting iOS 3.2 so no blocks.
The other main reason is I wanted a run-time interpreter. I’m a firm believer that shorter development cycles have a massive productivity boost. My goal is to get the time it takes to make a change and see it running live down to zero. In iBots I could telnet into the game and execute arbitrary code which helped a lot during development (Thanks Miguel!).
So what where the problems??
Instruments
Compared to the visibility that Xcode’s Instruments gives you over your objective-c code, LUA is a black box. Wax does some really clever object mapping between the two languages and releases and retains objects as necessary. Unfortunately you can run into situations where LUA is holding objects alive and it’s hard to see why or where it’s happening. I ended up spending a fair amount of time debugging LUA code in iBots Launch looking for retained variables.
Speed
This was fine for the AI code in iBots, but I wrote 99% of the Chicken Pox prototype in LUA and it was sloooooooow. It’s hard to say whether the speed issues where in the LUA VM or the iPhone Wax bridge but it was clear that it wasn’t the correct approach.
So where to now?
Right now i’ve rewritten Chicken Pox in objective-c and it’s back to 60Hz and plays much nicer.
I’m also trying to tackle the live updating with a different approach. Instead of running arbitrary LUA code at runtime I’m limiting myself to live updating data. My scene graphs are defined in json files which look something like this:
{
"GameScene": {
"children": [{
"name": "hud", "type": "layer",
"children": [{
"type": "sprite", "frame": "pause_menu_bg.png",
"position": [291,142],
"visible":false
}]
}]
}
}
I then have the game start a zeromq server when it launches, I start a local filewatcher to watch the json files, and anytime I edit and save a file I transfer it to the device which parses it and updates the scene graph. This also works for arbitrary data which when used with KVO makes live changes possible.
Stay tuned to see some more detail on how this stuff works because it’s really fun!
