How do I trigger a (secret) animation with a sequence of keys, as in a cheat code/ easter egg?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sexychickens

I’m working on a 2D side-scroller. I want to include a little easter egg, where after the player types out “dance” the character does a little dancing animation. I’m an absolute beginner and know next to nothing about code or Godot still.

I have the animation on a seperate spreadsheet, how do I go about achieving this goal?

:bust_in_silhouette: Reply From: samjmiller

Create the animation (use an Animated Sprite node, generate new frames from a sprite sheet, grab the right frames from the sheet). Give it a name - let’s say “dance” for now.

Then it’s easy enough to play that specific animation only once your criteria has been met.

Here’s a simplified version, so that it would play if the “D” key is pressed:

   func _input(event):
	   if event is InputEventKey: 
		   if event.scancode == KEY_D:
                  $AnimatedSprite.play("dance")

Hope this helps!!

:bust_in_silhouette: Reply From: stormreaver

There are various ways. I implement keyboard cheat codes by having my keyboard class maintain a string that accumulates the most recently pressed keys. The keyboard class erases this string if there have been no keys pressed in the last half-second (I use a Timer for this), or if the string gets too long (mine maxes out at 50 characters).

This same keyboard class has a list of special strings (and corresponding names) that are the cheat codes. If the accumulation string matches any of the cheat codes, it fires off a signal that says which cheat code was matched. Then the signal handler in the calling class does its stuff.