Thursday, June 11, 2015

A little optimization trick for Corona SDK/lua using first-class function

Many times during development of  my games using Corona SDK I found that I have a function that gets called a lot, and at the beginning of the function I need to check for state of the game.  Something like this:


function listener(e)
     if state == S_PAUSED then return end
    
    -- OK, do real stuff
end


Every time this function gets called, the if statement gets executed, and in this case it is a wasteful since the game is not paused most of the time.  To get rid of this overhead, I often use a feature known as first-class function, which basically means you can treat a function as if it's variable so that you can pass it around or return it from other functions.

So, with first-class function, we can reconstruct our program like this


local listener

function listener_pause(e)
   -- do nothing
end


function listener_ready(e)
   -- OK, do real stuff
end


function setPause(v)
    if v then
        listener = listener_pause
    else
        listener = listener_ready
    end
end

listener = listener_ready


When the program is paused, the function listener_pause() gets called and do nothing, just like the original function.  However, if program is not paused, the function listner_ready() is the one that gets executed, and there is no overhead.

The downside of this method is that your program will be a bit longer, but it might worth it if the function get called a lot, like maybe the one that responds to 'enterFrame' event.

Saturday, June 6, 2015

Puzzle 4 design evolution

I want to do this puzzle for a while.  It's based on my previous Android game called Diamong Drift (no longer on play store since it crashes on newer devices and I can't afford to port it to newer game engine).  I call this new puzzle Puzzle 4.  The coding is going OK, but the design has not been settled yet.  This is what it looks like during the past few weeks.


Diamond Drift was somewhat successful.  Almost 100K download with minimal marketing.  Hopefully people will like Puzzle 4 as well. Puzzle 4 is developed using Corona SDK so it will be available on both Android and iOS devices.