'short-forming' functions =)
Script Newbie question:
I like 'short-forms' and at the moment I try to create some for my keyboard checks.
This is the basic idea:
keyPress= function(key, todo)
if keyboard.press[key] then todo() end
end
update = function()
keyPress("SPACE", function() x+=1 end) // works
end
That works as shown, but since I'm a lazy guy I would like to replace the function call with this:
keyPress("SPACE", "x+=1")
Is there any (easy) way to do that?
Live Long And Tinker
P.S. this is a double up of a question I posted on the discord channel, but I thought it might be of general interest
That actually works? I had no idea, that sounds pretty useful! (especially since I am lazy as well)
Update:
I took the liberty to forward @gilles reply from the discord channel:
This would require to have some sort of eval
function, which microScript doesn't have. Your working way looks very fine to me.
Not really shorter, but I would probably like to do it this way:
bindings = object end // just create an empty object
bindings.SPACE = function() x += 1 end
bindings.UP = function() y += 1 end
(...) // more bindings
update = function()
for b in bindings
if keyboard.press[b] then bindings[b]() end
end
end
(Note: for ... in
when applied to an object, iterates over the object's defined properties)