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

22 Comments

  1. bubu says:

    Doesn’t iPhone developer licence forbids to use any scripting language on iPhone?

  2. tuomas says:

    It actually forbids downloading or loading scripts into the application. I don’t know what loading actually means in the agreement, but my scripts are inside the binary, so hopefully Apple does not reject it. I have also heard that people have been able to get applications, written in Lua, to the App Store.

  3. I’m not a big fan of Lua. I looked at it to do a few basic scripts and found it very lacking when it comes to available libraries. I wanted simple things like XML and JSON parsing. Both of which are well supported and easy in languages like Ruby and Python. In Lua you have the choice of unsupported, poorly made and downright nightmarish libraries to choose from. Add to the the absolutely tiny size of the community and its a non-starter. Lua may be fast but its hugely lacking in 2 key areas – libraries and community.

  4. tuomas says:

    @Doug

    I agree that as a general programming language, Lua is indeed lacking in those 2 key areas. I would never use Lua as a standalone language to develop anything. Python and Ruby do a lot better job.

    Lua is very good in what it was designed to do, but you (or I) should not push it too far.

  5. Adam says:

    bubu: It only forbids using a scripting language such that the user can have access to the scripting. It is OK if it is just used within the application itself.

  6. Luis Felipe says:

    Tuomas,

    I’m just getting started into iphone development and I’d really like to test your framework, so if you’re looking for votes in order to make it opensource, here’s a +1 from me. Also, keep us posted if your application gets accepted please.

    Cheers

  7. tuomas says:

    @Luis Felipe

    I will do that if my application gets accepted. I need to do some refactoring for the framework before that though. I’d like to make it more object-oriented with automatic garbage collection of unused objects.

  8. lochii says:

    Ever heard of Corona SDK? , I believe this uses Lua : http://www.anscamobile.com/corona/

  9. tuomas says:

    @lochii

    Yes, I remember reading something about Corona SDK a while ago. A had quick look at the website and it looks really professional, but they also charge $99 for it. My goal it to make a simple framework that allows me to write iPhone applications quickly. I am adding new features to it when I actually need them in my applications, so I am not trying to compete with Corona SDK (at least not yet).

  10. Richard Finegan says:

    Another vote for open-sourcing your framework…I’d really like to see it if you are willing, thanks!

  11. Driphter says:

    I love LUA as a language, so thumbs up from me. :)

    Although, that sample code would be more efficient with table.concat instead of the current concatenation. See http://www.lua.org/pil/11.6.html

  12. Jason Clarke says:

    I agree LUA rocks, it’s ability to be easily extended and embedded make it very powerfull.

    We’ve used it as the scripting language in our Storyboard Suite
    http://cranksoftware.com/products/crank_storyboard_suite.php

    We are planning to release an eclipse LUA debugger integrated in our designer in our next version.

    Jason

  13. Corey says:

    You should check out my open source project called Wax http://github.com/probablycorey/wax

    It sounds like it accomplishes what you are looking for, full Objective-C/Lua bridging. With it you can call any Objective-C method, create Objective-C classes, delegates, etc… all in Lua!

  14. tuomas says:

    @Driphter

    Yes, concatenation is more efficient with table.concat, but so far my strings have been less than 50 characters long.

    @Corey

    I actually checked you project couple of months ago. My goal is not to replicate what you have done. If you check my example code, you can see that I am trying to make a more high level framework, without having the need to use 1-to-1 mapping between Lua and Objective-C.

  15. Marius Ghita says:

    I vote as well for open sourcing it.

  16. Lennie says:

    howzit, where can i get your Lua iPhone tool from?

  17. tuomas says:

    @Lennie

    Sharp. Unfortunately I haven’t made it available yet, so you have to wait for a while.

  18. tuomas says:

    Nice! My application got accepted, now I can continue working on the framework and I have decided to open source it soon.

  19. MadMark says:

    WOW!

    Fastest Apple approval evar! Gratz!

  20. tuomas says:

    Thanks. I was really surprised that it was approved in just 48 hours. It was waiting for a review about 40 hours and reviewed in 8. Now I will work on my next game “Master of Blocks”, a Tetris clone inspired by Tetris God video http://www.youtube.com/watch?v=Alw5hs0chj0

  21. RIP Lua on iPhone… Really sad, although Lua is still an unknown for me, it has stepped from the blankness into my forthcoming language to learn, after I finish a project with Forth.

  22. Ryan says:

    Doug@Straw Dogs

    I’m always relieved when I see morons like you evaluate Lua and decide to stay away from it. The less clueless, knee-jerk developers cluttering up the Lua ecosystem the better.

Leave a Reply