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 <>:
> 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()
           state.goto "pause"

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

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.