As per title. I have no tokens remaining and need to do some optimising, so the least number of tokens the better!
You can use the btn() function with no arguments. This returns a bit-field which is a combination of all the buttons pressed.
Piggy-backing on @dw817's solution this, I think, does what you're looking for with a handful less tokens.
Oh yeah ! Button without arguments, that only gets one key. Nice, @jasondelaat.
Of course if you were going for super brevity (and the hell with legibility) you could have this disaster :)
Keep hoping @zep will eventually add the ability to register logic numerically. Maybe with that very "_" character.
FYI, you can save 3 tokens on your _() function.
function _(a) return a and 1 or 0 end
That's still slightly more tokens than mine in total but it depends how you count.
Your _update() is shorter than mine but your update() plus () is slightly longer.
If that _() function gets used often then we can discount those added tokens because their cost is "spread out."
Thanks, @jasondelaat. But yeah, don't wanna get too crafty here.
The main thing is, @timeandspace, do you understand the solutions we posted ? The best being Jason's with the BTN() function that sets bits according to what buttons are pressed.
LEFT = 1 RIGHT = 2 UP = 4 DOWN = 8 (O) = 16 (X) = 32
So for instance you could look for the # 15 in BTN() and that would mean you are holding down all 4 of up, down, left, and right.
hmm these are cool and all but they don't remember the oldest input :o
In my game I had it so that it looks for new presses then adds those to a table, if the newest one isn't being pressed use the one 1 lower in the table, and then the logic for the keystuff.
So that way I can have stick controls or dpad and it would accurately remember which way you should be moving instead of prioritizing certain directions or freezing up.
Though this is about how many tokens can you save, and not about keeping track of the past and using the newest to oldest input.. though now I'm wondering how small that would be
Okay, I'm kind of late here but I've got a pretty low-token solution for you. It's not as straightforward or readable as @jasondelaat's take, but it uses just 20 tokens for the actual movement code instead of 36.
function _init() x,y=60,60 end function _update() cls() local b=btn()%16+1 x+=split"0,-1,1,0,0,0,0,0,0,0,0,0,0,0,0,0"[b] y+=split"0,0,0,0,-1,0,0,0,1,0,0,0,0,0,0,0"[b] spr(0,x,y) end
Basically, it stores the movement speed values for every combination of directional buttons in 2 lookup tables, one for each axis, and uses the btn() bitfield as the lookup address (+1 to account for Lua's 1-based tables). Because the table entries can be any numeric value, it could also be used for customizing movement in other ways, like correcting movement speed for diagonals so you go the same speed in every direction.
[Please log in to post a comment]