How to Implement Cheats

Posted April 4, 2022

Ninja Time!

If you like 90s platformers, check out my new game Chibi Ninja Shino-kun!

I miss cheats. Modern games might have open worlds, hair physics and ray-traced pimples, but the humble push-button cheat, once a staple of video games, has pretty much disappeared. So, out of spite, I added cheats to Go! Go! PogoGirl. Also, now I’m gonna show you how I did it, so you can add cheats to your game as well! Let’s usher in a new era of level selects, 30 lives and big head modes! Fight the power!

Okay, so, first some theory. We’ll make it so you have to input a string of buttons at a certain speed to trigger the cheat. For that we’ll need three basic ingredients. First, we’ll assign each button input a letter: Left will be L, Up will be U, and so on. Then we’ll set up two strings. The first will contain the inputs for our cheat, the second will be empty. This one will save the inputs the player makes, so we can compare it to the cheat string. Finally, we need a timer that makes sure each input is made within a certain timeframe. Let’s do it!

First, let’s set up the variables we need.

private var _timer:Float = 0;
private var _cheatTime:Float = 0.7;

private var _inputString:String = "";

private var _cheat1:String = "UDLR1";
private var _cheat2:String = "UUDD12";

private var _output:FlxText = null;

_cheatTime is the timeframe in which the next button must be pressed. _output will just print a message when a cheat has been entered successfully. And, as you can see, we’re specifying two different cheats that the player can enter.

Now, let’s set up the output in create() real quick so that we can see what’s happening:

_output = new FlxText(0, 0, 0, "Enter Cheat", 16);
_output.screenCenter();
add(_output);

Now we create a function that will check whether a cheat has been entered and trigger an effect. For our example we’ll just print a message to the screen.

private function handleCheats()
{
	_timer = _cheatTime;

	if (_inputString == _cheat1)
	{
		_output.text = "Cheat 1";
	}
	else if (_inputString == _cheat2)
	{
		_output.text = "Cheat 2";
	}
}

First, we reset the timer so that players have time to input the next button. Then we compare what the player has entered so far and check if it’s a valid cheat. If so, we update the output line. In a real game, this is of course where you’d apply the effects of the cheat.

With all the pieces now in place, it’s time to actually grab the input of the player! We’ll do that in the update loop and with a big-ass if-construction.

if (FlxG.keys.justPressed.UP)
{
	_inputString += "U";
	handleCheats();
}
else if (FlxG.keys.justPressed.DOWN)
{
	_inputString += "D";
	handleCheats();
}
else if (FlxG.keys.justPressed.LEFT)
{
	_inputString += "L";
	handleCheats();
}
else if (FlxG.keys.justPressed.RIGHT)
{
	_inputString += "R";
	handleCheats();
}
else if (FlxG.keys.justPressed.C)
{
	_inputString += "1";
	handleCheats();
}
else if (FlxG.keys.justPressed.X)
{
	_inputString += "2";
	handleCheats();
}

As you can see, we append a new value to the _inputString depending on what button the player pressed. Then we always call handleCheats to see if a valid cheat has already been entered.

One thing is still missing, however! Right now, players have all the time in the world to enter a cheat…and should they press a wrong button, they’ll never be able to trigger a cheat, because the _inputString is never cleared and so will never match a valid cheat. That’s why we need this final chunk of code in the update loop, right after the if-construction we just added:

if (_timer > 0)
{
	_timer -= elapsed;
	if (_timer <= 0)
	{
		_inputString = "";
	}
}

This will clear the input string if no input has been made for a specific time (in our case 0.7 seconds). This forces players to enter the cheat quickly, and also allows for retries.

And that’s it, really! If you compile the project, you should be able to enter cheats. If you enter random stuff, nothing will happen; but if you enter one of the cheats we specified, you should be greeted with a message!

images/cheats.gif

If you have any questions, comments or criticism, post them in the comments below or reach out to me on Twitter @ohsat_games!

Join my mailing list!

You'll get notified whenever cool stuff happens!

Take It to the Next Level!

Become an excellent patron on Patreon and snatch yourself some kickass perks such as early access, early builds, exclusive updates and more!

Want To Buy Me a Coffee?

Coffee rules, and it keeps me going! I'll take beer too, though.

Check out the rest of this tutorial series!

  • HaxeFlixel Crash Course: Make a Pong Game in Under 1 Hour
  • Pixel-Perfect 2D Water Shader
  • Collision and Overlap
  • Using Finite State Machines in HaxeFlixel
  • One-Way Collisions
  • Z-Sorting in HaxeFlixel
  • Making an Underwater Music Effect in HaxeFlixel
  • Quick Snow Effect
  • How to Implement Cheats
  • Using LDtk with FlxTilemap
  • Using a Texture Atlas
  • How To Make Your Own Haxe Library
  • By using the Disqus service you confirm that you have read and agreed to the privacy policy.

    comments powered by Disqus