How to return the player to idle animation?

Trying to get the player to return to an idle animation when no d-pad input is being pressed or held. The issue is trying to sort out the logic so the player doesn't swap to the idle quickly while the d-pad is being held down.

Edit: Sorry, I didn't see this question was about Pulp! My bad


I did go for a state machine, there's a great implementation here: GitHub - kyleconroy/lua-state-machine: A finite state machine lua micro framework, or if you're using GitHub - Whitebrim/AnimatedSprite: Sprite class extension with imagetable animation and finite state machine support for @Playdate it's actually also built-in. Examples below use the first package:

You define your states idle and walking and the transitions between them like walk and stop.

Then in your update loop you listen for input and call :walk() on your state machine when d-pad is pressed and :stop() in the else case.

This now gives you specific events only on state transitions, i.e. your animation swap is only fired once. On your state machine, you can listen for the onenteridle event which is automatically derived from your states.

I fear you're in for some bad news. Pulp does not detect buttons being held. When you press and hold a button, it just repeat presses quickly, though you can set how quickly or turn it off (and it just presses once when held)

So how can we work around that? It does detect the start of a button being presses but not when it's been released. If you set config.inputRepeat to 1, you'll get a rapid calls to player update (if we're taking about dpad). If we have player update start a timer that matches the repeat timer and have player draw check that timer, you could have it switch to your idle only after that timer is expired. just tapping the dpad once will turn off the idle state for longer than you're pressing it though, but that's the only way I can think to do it.

on update do
  idle_timer = 15 // run some tests and adjust this to get it to match your config.inputRepeatDelay and config.inputRepeatBetween length (which you may want to make the same and small)  this number is in frames and the configs are in seconds so you'll just have to guess and test it until you like it
end

on draw do
  if idle_timer>0 then
    idle_timer--
  else
    tell event.player to
      swap "playerIdle" // or whatever you name the idle animation tile
    end
  ene
end

you could make this better by making config.inputRepeatDelay and config.inputRepeatBetween as small as possible, but be careful with that as it will make the game run more input cycles per second as you press buttons and could easily overload things or cause slowdowns