Implementing cheat codes in Pulp

If you haven’t heard, RUBDUBDUB (right-up-b-down-up-b-down-up-b) is a secret cheat code hidden in multiple Playdate games. You can read more about it on the Playdate Community Wiki!

Do you want to include RUBDUBDUB (or any other cheat code) in your Pulp game, but don’t know how? Then this tutorial is for you!

The basic approach we’re going to take is to save the last nine button presses in variables, one per button press. Let’s name those variables like ch1, ch2, ch3 ... ch9 where ch1 is the most recent button press (and if you want to add cheat codes longer than 9 buttons you can do that too, just add more variables following the same pattern).

Before we get to capturing the button presses let’s start with the other logic we need.

Every time a button is pressed we need to update all of our variables by shifting their values by one position. What was the most recent button press becomes the second most recent i.e. the value of ch1shifts to the value of ch2. We can make an event that does that (in the player script, as that’s where the button input events get called):

on inputCode do
  ch9 = ch8
  ch8 = ch7
  ch7 = ch6
  ch6 = ch5
  ch5 = ch4
  ch4 = ch3
  ch3 = ch2
  ch2 = ch1
end

We also need an event that checks to see if the saved button presses match a cheat code. A neat way of doing that is by using string formatting to combine all of the variables into a single string which we can compare against a code, like this:

on checkCode do
  code9 = "{ch9}{ch8}{ch7}{ch6}{ch5}{ch4}{ch3}{ch2}{ch1}"
  if code9=="rubdubdub" then
    // code entered succesfully, do something!
  end
end

Notice how the variables are formatted into the string “backwards” (starting with ch9) so that the string matches the code as you would enter it. If you want to check for codes of varying lengths you can do that too:

on checkCode do
  code9 = "{ch9}{ch8}{ch7}{ch6}{ch5}{ch4}{ch3}{ch2}{ch1}"
  code4 = "{ch4}{ch3}{ch2}{ch1}"
  if code9=="rubdubdub" then
    // 9 button code entered successfully, do something!
  elseif code4=="udlr" then
    // 4 button code entered successfully, do something else!
  end
end

Now we just need to handle the button presses themselves!

Handling the “A” button is easy as we just use the built-in confirm event:

on confirm do
  call "inputCode"
  ch1 = "a"
  call "checkCode"
end

Note how we have to first call our inputCode event to shift the previously saved button presses, then we set ch1 to the string “a” (meaning the “A” button), and finally we call our checkCode event to check if a code has been successfully entered.

Handling the “B” button works very similarly, using the cancel event:

on cancel do
  call "inputCode"
  ch1 = "b"
  call "checkCode"
end

Handling the D-pad is slightly more involved and it depends on where you are trying to add cheat codes. If the player is trapped between solid world tiles (this is a relatively common way of making a “title screen” room where the player is hidden) then we can use the built-in bump event:

on bump do
  call "inputCode"
  if event.dy==-1 then
    ch1 = "u"
  elseif event.dy==1 then
    ch1 = "d"
  elseif event.dx==-1 then
    ch1 = "l"
  elseif event.dx==1 then
    ch1 = "r"
  end
  call "checkCode"
end

If the player is free to move around we can use the update event instead - the code is otherwise identical:

on update do
  call "inputCode"
  if event.dy==-1 then
    ch1 = "u"
  elseif event.dy==1 then
    ch1 = "d"
  elseif event.dx==-1 then
    ch1 = "l"
  elseif event.dx==1 then
    ch1 = "r"
  end
  call "checkCode"
end

It’s also possible to detect D-pad input in other ways such as from the interact event on sprite tiles, but the above two approaches are simplest.

Put the above together and you have cheat code entry in your Pulp game! Of course you might want to make it conditional so that cheats are only entered from a title screen or menu, and you could also try detecting the crank and making cheat codes involving cranking. Hopefully this was helpful and I’d love to see more Pulp games using RUBDUBDUB!

6 Likes