Log In  

As per title. I have no tokens remaining and need to do some optimising, so the least number of tokens the better!

P#100545 2021-11-21 12:38 ( Edited 2021-11-21 12:38)

Just to clarify, do you want all directional inputs canceled if diagonals are pressed?

P#100554 2021-11-21 17:12

Yes.

P#100555 2021-11-21 17:15
1

The simplest way I know of, @timeandspace would be:

Cart #jisuhemabu-0 | 2021-11-21 | Code ▽ | Embed ▽ | No License
1

But yeah this is a good idea. There should be a special mode for it, maybe an extra flag for BTNP() and BTN().

P#100556 2021-11-21 17:24

Thanks, but that doesn't disable all directions if a diagonal is registered. E.g. If I input up+left the cursor still moves upwards.

P#100561 2021-11-21 18:03 ( Edited 2021-11-21 18:03)
1

You can achieve that by checking buttons with 'elseif' instead of 'if'!
if left pressed ... else if up pressed ... etc

P#100563 2021-11-21 18:10 ( Edited 2021-11-21 18:10)
1

Oh, you want total shutdown, @timeandspace ?

OK, lemme change that slightly.

Cart #gabehuwibo-0 | 2021-11-21 | Code ▽ | Embed ▽ | No License
1

P#100568 2021-11-21 18:35
1

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.

Cart #rigoyutahe-0 | 2021-11-21 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

P#100569 2021-11-21 18:39
1

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.

Cart #busegobiji-0 | 2021-11-21 | Code ▽ | Embed ▽ | No License
1

P#100571 2021-11-21 19:02 ( Edited 2021-11-21 19:18)

@dw817
Oh, nice!

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."

P#100575 2021-11-21 19:49

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.

P#100580 2021-11-21 20:58 ( Edited 2021-11-21 20:58)

Yes, I understand. Those look good to me, I don't care about legibility in this case as I have so few tokens to play around with.

P#100590 2021-11-21 23:39

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

P#100591 2021-11-21 23:46 ( Edited 2021-11-21 23:48)
P#100598 2021-11-22 00:37 ( Edited 2021-11-22 03:48)
1

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.

P#100610 2021-11-22 04:25 ( Edited 2021-11-22 23:57)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 21:54:45 | 0.064s | Q:48