Hello all, I workshopped some quick code for detecting double clicks(and differentiating them from single clicks) in the playdate squad discord and thought I'd throw my (messy) code on the forum in case anyone wants to use it.
Obviously there are some limitations here. Because the game needs to wait to see if you're gonna do a double click, there's gonna be a 6 frame delay between your single b-press and the action assigned to the b-press. This is okay in an optimized game that runs at the full 20 fps, but is gonna be very very noticeable in a game that has a lot of slowdown. The double click action is gonna be more immediate. Because of this, you're better off using this to get extra input options in a game where the action on your single b click doesn't need to be too immediate. If b swings a sword, it's not gonna be great. If b opens a menu, it'll feel much more smooth.
Player script
if bPressed == 0 then
bPressed = 1
framesSinceBPress = 0
elseif bPressed == 1 then
if framesSinceBPress<5 then
say "good job, you double clicked 'B'"
end
bPressed = 0
framesSinceBPress = 7
end
end
game script
on loop do
if bPressed==1 then
framesSinceBPress++
if framesSinceBPress==5 then
say "that's a single b_press"
bPressed = 0
framesSinceBPress = 0
end
end
end
You can obviously change all the b's and cancels in here to a's and confirms. Replace the say functions with whatever code you want to execute on b click and double b click. And you can change the various frame count if you want bigger or smaller windows for double clicking/detecting single clicks. You should always set framesSinceBPress on line 10 of the player code to something higher than the number of frames it waits after a signel press in the game code.
The code is a bit messy, I might clean it up and edit this post later.