lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2010/11/23 Pierre-Yves Gérardy <pygy79@gmail.com>:
> In my case, it is about tying callbacks to keypresses and events in games.

String lambdas to the rescue!

state "menu" :keyconfig
   "%arrows" :held 'k,dt => hero:move(k,dt)'
   " " :pressed '|=> hero:shoot()'
       :held    '_,dt => hero:accumulateEnergy(dt)'
       :released '=> hero:releaseBigBlow()'
   "escape" :pressed(function()
           makesound()
           state.goto "pause"
       end)
()

Very doable in existing Lua, efficient (especially if you keep a
string-function cache), and straightforward. Your handlers just have
to check if they're passed a string and compile that string for later
calling.

The irritating thing is that 'hero' would have to be a global or at
least a field in the environment you use to compile the lambdas; they
cannot be _closures_.

steve d.