RIP Lua Programming on iPhone

It’s over. Steve killed you. We had a good run, but it came to an end sooner than I expected. I was always worried that something like this might happen, but it was still a surprise.

We knew each other only for a couple of months. Those months were the best time of my life. I will always remember the fun we had. I can still remember asking you to create a complete table including the data model with just one line of Lua code. You did all the hard work for me. I never knew what was happening behind the scenes. I bet your autopsy will reveal thousands of lines of ugly Objective-C code, but I don’t care. I will always remember you and your clean and nice Lua interface.

I remember when we did the multi-player code. You just said that I should give you the function that will be called on the other iPhone. It worked beautifully. I could just run code over the network as I did locally. I suspected that you did something against the rules, but I was ignorant and trusted you. The autopsy will probably show me the nasty details how you implemented this.

We also had fun, when we tried to hide you from Steve. We obfuscated the Lua scripts and put them inside the binary. Now, even that wouldn’t do the trick anymore, because running strings command on the binary, reveals that Lua library is inside the binary. I don’t know, if Steve’s minions would actually do this, but I am not going to risk it.

You showed me your true powers with closures and metatables. I was amazed, how you could do so many amazing things, when you seemed so simple. I felt that I was cheating, because programming with you was too easy. Most people think that programming for mobile devices is supposed to be hard, but you proved them wrong in your short visit on this planet.

We had our problems also. Remember, when I tried to use timers that were released. I would use pointers after you thought no one was using them, and cause you a lot problems. We talked about fixing this problem in your next version, but we didn’t even get to start working on that; your end was so sudden.

I hear there’s a new kid in town called Android. From what I understood, he doesn’t kill stuff like Steve. I hope I can resurrect you with Android. It might be dangerous, but I have to try it. I grew so fond of you that I cannot see myself coding in Objective-C or Java. They are just too verbose. You were so simple and elegant, but I guess Steve didn’t like the competition advantage you provided for people like me.

I will always remember you and keep a copy of you in my version control close to my heart. I will leave everything the way it is now. If I try to resurrect you with a new device, I promise I will use Git this time. I will let you have your peace in SVN.

  • Share/Bookmark

Why Lua truly rocks

For the uninformed, I am of course talking about the programming language Lua.

Recently I have been working on my Lua-based framework, for creating games and applications for iPhone. Yesterday I submitted my first application which uses the framework to App store. The whole application is written in Lua. You can check the Lua source code for the application here.

I have been working on the framework for weeks, but I wrote the Lua code for the application in just one day. The application is not very complicated, but Lua shows its potential compared to Objective-C, when you need to get iPhone apps done quickly. The more I improve the framework, the easier it is for me to write those application in Lua, because writing the application logic and adding a user interface is quite simple compared to writing everything from scratch with Objective-C.

The reasons for choosing and using Lua for iPhone applications were pretty obvious for me. It is written in ANSI-C, which means that I can just take the Lua source code and compile it with rest of my framework. Lua’s licensing model, MIT license, allows me to do anything with Lua. Lua is also quite compact. The binary size will only grow by a couple of hundred kilobytes, which makes it really light. It is also quite fast compared to other scripting languages and the memory usage is not too bad. I haven’t done any heavy calculations in my applications yet, but if I ever need to that, I can move the heavy processing to the Objective-C side.

Where Lua really shines, and what is was actually designed for, is that it is probably the best language for embedding and extending other parts of the software written in C, C++ or Objective-C. Once you understand the Lua stack model, adding functionality to Lua is very easy. It is almost as easy as calling functions directly. Only thing that you need to do is to get the arguments from the stack, which is very easy.

Lua is actually a very powerful and flexible language, because it has metatables and support for closures. You can do a lot with these features. I am currently working on two-player Tetris clone, and I can forward function calls automatically to the other player with this piece of code:

function create_peer_opponent(page_name)
    local opponent = {}
    local mt = {__index =
                function(table, key)
                    function remote_call(...)
                        local str = page_name .. "." .. key .. "("
                        for k, v in pairs({...}) do
                            str = str .. tostring(v) .. ", "
                        end
                        str = str .. "nil)"
                        ui.rpc_call(head_to_head, str)
                    end
                    return remote_call
                end}
    setmetatable(opponent, mt)
    return opponent
end

After creating the peer opponent, the rest of the code does not need to know if the opponent is a local AI opponent or a remote human opponent. The function calls will end up in the correct place automatically.

Of course, everything I want to do from Lua needs to be wrapped in the framework. Fortunately Objective-C is actually quite dynamic language, and the reflection support is pretty good. I might be able to leverage the reflection mechanisms to automatically create and use Objective-C objects from Lua, but I don’t know yet if it actually is possible or does it make any sense.

Another nice thing about writing the game logic in a scripted language is that I can implement the framework on other platforms, e.g., on Android, and use the same Lua code for the game. The platforms are not limited to mobile platforms; basically anything that has support C supports Lua.

If anyone is interested in using the framework, just let me know, and I might actually open source it.

  • Share/Bookmark