Log In  


Hi, I try to make a one-button game and thought I could extend it to four players , maybe even using one controller (but yes two players on the direction pad might not work).

On a keyboard 4 players could use the keys from both players, even esdf, for movement. Btnp() returns an integer that represents the pressed button/keys. e.g. 512 for player2,-> (right).

The players use the x ("x" and o ("c") buttons and on keybord q,w. I could use btnp(4,0), btnp(5,0) and btnp(4,1),btnp(5,1) but it would use 4 "if-statements" instead of one and I want to use the keyboard and variable keysetting.

I would like to have something like this:

playerbtn		= {32,16,4096,8192}
player[i]		= {btnpressed = playerbtn[i])

for i=1,game.playernumber do
if btnp() == player[i].btnpressed then
some code
end
end

instead of four times this:

if btnp(5,0) then
if player[1].movment < 8 then
player[1].movment += 2
end
end

but the for-statement doesn't catch all four playerkeys at once, the btnp()-version works with 4 keys pressed simultaneous.

What do I miss?

(full chart,but WIP, uncoment line 128/141 and comment line 142-182 to test my problem)


Cart #wodijawuju-0 | 2024-10-27 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1


1


the good news is that pico-8 already provides a way to get info for multiple buttons at once! it’s not a table containing true/false, but a number containing 0/1 bits (see https://www.lexaloffle.com/bbs/?tid=38338), so we can use efficient bitmask checking to determine if some buttons are pressed.

the bad news is that we can’t get one number with the info for 8 players, it doesn’t fit in one number. but it could be a very short loop!

official doc: btn() without params https://www.lexaloffle.com/dl/docs/pico-8_manual.html#Input (ok to check 2 players)

non-official wiki: https://antifandom.com/pico-8/wiki/Memory#Hardware_state sub-section 0x5f4c..0x5f53


This was fast, I tryed the bitmaskthing allready, but got other problems...

I'm not comfortable with this whole poke/peek, but this
peek(0x5f4c) -- 1-32 for player1
peek(0x5f4d) -- 1-32 for player2
(peek(0x5f4e) -- 1-32 for player3 ...)
looks helpfull. But works only for btn(),not btnp(). I don't really want to write some button control if some button allready is pressed and block further pressing. :(

But it worked like a charm with 4 keys pressed.


I forgot to mention peek2/peek4 that may help getting multiple bytes at once!
don’t remember right now how to put these bytes into one number (4 bytes), but that could reduce the loop to an if test with two conditions

for btnp, you will need to do some manual tracking: each frame, store current button numbers in a variable, then next frame they become the previous button state. from this you can determine pressed and released buttons with one bitwise operation!

btw this is how consoles like nes, gameboy, etc worked! our btn and btnp functions are quite convenient after all.


I answered your question without fully understanding what you were asking for.🙏

I think the code below will enable you to press the btn() version simultaneously.

if btn() & spieler[i].btnpressed == spieler[i].btnpressed then
-- some code --
end

The reason it seemed like you could press multiple buttons simultaneously with btnp() is because the timing of when you pressed multiple buttons was off by a few frames, and I speculate that this was because btnp() happened to be 32, 16, 4096, or 8192.
When you press four buttons simultaneously with btn(), the total is 16+32+4096+8192=12336.


1

@shiftalow Ah, thanks a lot. This works in my for-statement.

I got some code with peek/peek2 and tryed some peek2(0x5f4c)& 1000[ lots of zero ]000 and still had trouble with high numbers where still only 3 keys simultaneously worked. With 1,2,256,512 (pl1, left/right and pl2 left/right ) it worked. This & (?Bitmask) is still a magic thing for me.

@merwok Thanks for the short excurse (peek ;)) on peek()


1

Oh, @merwok

There's no reason to ever go to fandom. Simply replace the domain with antifandom.com, it looks the same, but without ads.

For PICO-8, we also have pico8wiki.com, which I believe takes the data from the fandom so it should be up to date.



[Please log in to post a comment]