Lua for multi-platform game model implementation: iOS, Android, ActionScript, etc.

From time to time we need to translate some application from one platform/language to another. For example, we need to implement the same mobile/social game for web/flash, iOS and Android. Of course, we can use some multi-platform framework like Corona to build such system, but it’s not so power like native software development tools.

The idea is to design and implement the core (model) of the game in some universal module in Lua programming language. The model may be pretty complicated for the games like chess or poker. I implemented the game ‘Guess The Number’ in Lua as an example. Then I separated the model:

GuesGame = {}GuesGame.WIN = 1GuesGame.LOSE = 2GuesGame.GREATER = 3GuesGame.LESS = 4GuesGame.MIN = 1GuesGame.MAX = 10GuesGame.MAX_TRY = 3function GuesGame:new()        return setmetatable({                _counter = GuesGame.MAX_TRY        }, {                __index = GuesGame        })endfunction GuesGame:start()        self._x = math.random(GuesGame.MIN, GuesGame.MAX)        self._counter = 0endfunction GuesGame:play(a)        if self._counter >= GuesGame.MAX_TRY then                return GuesGame.LOSE        end        self._counter = self._counter + 1        if a == self._x then                return GuesGame.WIN        else                if self._counter >= GuesGame.MAX_TRY then                        return GuesGame.LOSE                elseif self._x > a then                        return GuesGame.GREATER                else                        return GuesGame.LESS                end        endend

As you can see, there is just classic ‘guess the number’ game. The program chooses integer number from 1 to 10 randomly. And the player is trying to guess it in 3 tries.

Next steps were implementation of the game as console applications in pure lua, Objective-C and Java. Lua engine is built as C library, so, there are not problems with Objective-C. In case of Java I used LuaJ. Keep in mind, I used exactly the same lua code of the game model for all implementations. Generally, it was not only that lua code. I need to add some support code for initialization and interface with UI side implemented in native language specified for the appropriate case.

E:ProjectsGuesGamepure_Lua>lua GuesGame.luaq - quit, s - start new game, 1-10 - guesTry to gues a number between 1 and 10?5Less3Win! :-)q

After that I implemented iOS version as iPhone application in the same way. There were no problems at all.

Ios_lua_gues_the_numver_game

To do flash (action script) game I used lua via alchemy. It costs me some performance. Alchemy is going slow at line of calling it from action script side because of huge serialization. So, if you are going to touch you game model often, then you better avoid such scenario and port the model to action script.

Flash_alchemy_lua_gues_the_number_game

We can’t use LuaJava on Android as for pure Java because of differences in JVM. So, I used Android NDK to compile lua engine as C module for Android game.

Android_ndk_lua_gues_the_number_game

Advertisements
1 comment
  1. Ivan Kurmanov said:

    Very cool, Alexander. It looks very different on these platforms (I like the iPhone version most), but it is a very cool approach.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: