Creating Cheat Codes

Ever want to put a cheat code in your game but wasn’t sure how? Maybe you want to put a debug menu in for your playtesters so they can skip a level or enable infinite health? Or perhaps you have a secret buried in the game you want players to discover? It’s not too challenging to implement a cheat code system within GB Studio, so let’s see how we can do that.

For this guide we’ll be building a button sequence that unlocks a secret message. What better sequence to implement than the classic Konami Code: “Up, Up, Down, Down, Left, Right, Left, Right, B, A, Start”.

For simplicity, we’ll create a project with only one scene and a “Press Start” prompt that’s part of the background. We will simply tell the player whether they entered the code correctly or not. To track the player’s button presses, we’ll use a Local Variable.

To understand how one variable can track a button sequence, let’s write out each button press in the Konami Code and assign a number to the order in which it appears in the sequence:

Up

1

Up

2

Down

3

Down

4

Left

5

Right

6

Left

7

Right

8

B

9

A

10

Start

End

Laying it out like this helps to see how one variable can keep track of the sequence. If the variable starts at 0 and the player presses Up, we can increment the variable to 1. If they press Up again, it should increment to 2, and if they then press Down next it increments again to 3 and so on. By checking the value of the variable every time the player presses a button, you can determine if the button they pressed was the correct one in the sequence. If the player presses all the buttons in the right order, the value of the variable will be 10, which we can then check when the player presses the Start button.

Let’s build this out. We’ll follow the principles laid out in the Basics: Trigger and Positional Menus article by using a combination of [Joypad Input: Pause Script Until Pressed] and [If Joypad Input Pressed] events to track the buttons. It is possible to use the [Joypad Input: Attach Script To Button] event to implement this feature, but that means you have to reset the scripts when you exit the scene. Using this combination allows for easier management within menus.

First, create a scene and add a [Actor: Hide] event to the “On Init” tab. Hide the player sprite since it is not needed in this menu. You will then need to add a [Label: Define] event for this to work. Labels work like markers for a script allowing you to return to it later with a [Label: Goto] event. For our purposes we’ll enter “Loop” in the text field, as we will be looping through our button checks and always returning to this point.

Next add a [Joypad Input: Pause Script Until Pressed] event and select every button. Even if you don’t use a button in the sequence, you’d want to check for wrong inputs too. Let’s start with our first input check.

Add an [If Joypad Input Pressed] event. By default, both A and B buttons should be active. Click on the Up cursor to activate it and click the A and B button to deactivate them. This will now run a check when the Up button is pressed. In the Konami Code, the first two button presses are Up, so we will use a [Switch] event to track this (to brush up on how switches work, read this article). Add a Switch and have it set to Local Variable 0. To make things easier, rename this variable “Cheat Code Counter”. Recall that Variables start at 0, so our first case in the switch should be that: When: 0. Add a [Variable: Increment By 1] event to the first case and set it to the same local variable, “Cheat Code Counter”. The next case, When: 1, should increment the same value. You can manually add another increment event or you can click the arrow on the right of the existing Increment event, select “Copy Event”, then click it again and select “Paste Event”, then drag it down to the next “When:” statement.

If it isn’t clear what is happening, we are asking to see if this Up press is the first input (value 0 on the Cheat Code Counter) and then add one to the counter if it is. The second check does the same, but checking if the Cheat Code Counter is at “1”, or the second input, and then increment it again if it is. If a player presses Up at any other point in the sequence, for instance if they press Up for a third time, then the Cheat Code Counter should reset since it is an incorrect input. So in the Switch’s Else case add a [Variable: Set To Value] event and then set the Cheat Code Counter to 0.

This part of the script is now complete. We have to return to the top of the loop to pause the script once more to await input, so add a [Label: Goto] event and set it to “Loop”.

We now have to check the next input. The easiest way to do this is to copy the entire [If Joypad Input Pressed] event and paste it. To make things easier to navigate, it’s a good idea to rename the first check we created as “Up”, and we’ll rename this new one to “Down”. Make sure to correctly set this new event to the down cursor and deactivate the up cursor, then turn your attention to the switch event in it. Again there are two inputs for Down, number 3 and number 4 in the sequence, so the values should be When: 2 and When: 3, with everything else staying the same.

Copy and Paste this entire Joypad Input Pressed event and set it to Left (again renaming it to make it easier to track). In this case, Left is number 5 and number 7 in the sequence, so set the switch to When: 4 and When: 6.

By now this method should make some sense. You’ll need to create checks for the Right Button as well as A, B, Select and Start, and can do that by copying and pasting the [If Joypad Input Pressed] event, making sure to rename each one and set it to the corresponding button. In each check, count the number of times a button is pressed in the sequence, set your Switch to have that number of options, then set each “When:” case to the number in the sequence minus 1 (since it’s a 0 index) and increment the counter. For button presses that only appear once, you could use a [If Variable Compare with Value] event. Since I’ve recommended copying and pasting events, in this example it makes more sense to simply reduce the number of switch options to 1.

To help keep track if you’ve been setting everything up correctly, you can set the Select check to be a “debug” of sorts. Under the [If Joypad Input Pressed] event for Select, remove any scripts if you copy and pasted them and add a [Text: Display Dialogue] event. Type in “Counter is $L0$” (it should show up as $Cheat Code Counter in the latest beta release), this will print the value of the counter for you to check as you enter the code if you wish to see it by pressing Select in game. In the final game, this would be expected to simply reset the counter to 0 by using a [Variable Set To Value] event.

Finally, let’s configure the [If Joypad Input Pressed] for the Start Button. Add a [If Variable Compare with Value] event and set it to check if the Cheat Code Counter is “Equal To: 10”, since the final button in the sequence would increment it to that value. If it’s successful, add a [Text: Display Dialogue] event and type in “Code Activated!”, followed by a [Variable: Set To Value] which should set the Cheat Code Counter back to 0 (to reset it). Add another dialogue box to the Else portion that says “Code Not Active” and once again reset the Cheat Code Counter after. Make sure this event also performs a [Label: Goto] event to return it to “Loop”, and this project should be working as expected now.

For this demo, we simply showed the player if the code was active or not. For your game, you may want to have a successful code entry warp the player to another scene (I did this for my game, The Warp Coin Catastrophe – enter the Konami Code at the “Press Start” screen to get an art gallery). You could also set a global variable to something that has an effect in game. Perhaps the player is awarded with many lives, just like how the original Konami Code worked, or they are given a lot of money in game.

Try it yourself with a different code. See how you can map out different combinations in a single script (hint: each sequence will have to be a different number of buttons). It’s also possible to add this to a more traditional menu, think about how that could be accomplished. We may explore that in a later article.

You can also download this entire project over on our itch page.

Liked it? Take a second to support GB Studio Central on Patreon!
Become a patron at Patreon!