How Do I Map the Same Input to Multiple Animations?

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

Hello! We’re trying to do a menu screen so that players can press the down button (will eventually be any button, but one thing at a time), and an animation called “shaky scissors” will play the FIRST time the button is pressed. But the second time the down button is pressed, I want it to play the animation Shaky_02, and the third time the down button is pressed, I want it to play the animation Shaky_03.

I tried to code it so that a variable increases by one on each button press, and put an if/and statement in… but for some reason, it plays the shaky scissors animation every time I press the down button. (Note: I haven’t made the Shaky_03 animation yet, but Shaky_02 isn’t working… and I would think that on a third button press, nothing should happen, if it’s coded like this. But apparently I’m wrong)

We’re fairly new to Godot, so this might look messy or something… but I feel like it should work?

Here’s our code:

extends Node2D

var button_press = 0

func _process(delta):
	
	if Input.is_action_pressed("ui_down") and button_press == 0:
		$AnimationPlayer.play("Shaky_Scissors")
		button_press += 1

	if Input.is_action_pressed("ui_down") and button_press == 1:
		$AnimationPlayer.play("Shaky_02")
		button_press += 1
		
	if Input.is_action_pressed("ui_down") and button_press == 2:
		button_press += 1
		
func shake():
	Shake.shake(10,3)
:bust_in_silhouette: Reply From: LordBoots

To clean up the code a little bit:

if Input.is_button_pressed('ui_down'):
    if button_press == 0:
        $AnimationPlayer.play("Shaky_Scissors")
        
    if button_press == 1:
         $AnimationPlayer.play("Shaky_02")
    button_press += 1

Are you sure the second animation is different from the first?

Thank you! The second animation IS different from the first (the first has an audio cue, and the second is the same with no audio cue). Per your suggestion, I just updated the code to look like what I’ve attached below. Now, when I press any button, the first animation plays just fine, but the second animation and third animation do nothing. At the very least, if my second animation was wonky, I’d expect the third button press to play Shaky Scissors correctly, but that’s not happening either. Thoughts?

extends Node2D

var event_trigger = 0

func _process(delta):
	
	if Input.is_action_pressed("ui_select"):
		
		if event_trigger == 0:
			$AnimationPlayer.play("Shaky_Scissors")
			event_trigger += 1

		if event_trigger == 1:
			$AnimationPlayer.play("Shaky_02")
			event_trigger += 1
			
		if event_trigger == 2:
			$AnimationPlayer.play("Shaky_Scissors")
			event_trigger += 1
		
func shake():
	Shake.shake(10,3)

indolent_gamemakers | 2022-12-03 21:50

can you remove the event_trigger from all of them and just place it at the bottom of that function, same place I put the button_pressed in my code.
The reason for this is that I assume this is how the function is working:

event_trigger == 0 so we enter the first statement,
at the end of that statement we are advancing event_trigger by 1
-This means we instantly enter the second if statement.
At the end of this statement we advance event_trigger by 1.
-This means we instantly enter the third statement.
So in a single key press we are advancing through every if statement.

by moving the 'event_trigger` to the bottom of the function we ensure it only advances once the statement checks are complete.

if that doesn’t work (which I assume it will) can you please add
print($AnimationPlayer.current_animation, event_trigger) to each statement so that we can see what is happening in every statement and if they are even being reached.

LordBoots | 2022-12-03 22:05

If you want to keep your code the way it is then you need to reverse the order of the statement so that if event_trigger == 3 is at the top and if event trigger == 1: is at the bottom.

LordBoots | 2022-12-03 22:16

Thank you! The reason the event_trigger needs to go on the bottom makes sense, but it’s still not working. I don’t know how to print the animation player, but I took screenshots of the two animations we’re working with for you.

Now, for some reason that makes zero sense to us, the Shaky_Scissors animation (which brings the sound down while the screen shakes, and then brings the sound back up) is bringing the sound down but NOT bringing the sound back up. And it’s still not playing Shaky_02 with a subsequent button press.

Here’s our current code:

extends Node2D

var event_trigger = 0

func _process(delta):
	
	if Input.is_action_pressed("ui_select"):
				
		if event_trigger == 0:
			$AnimationPlayer.play("Shaky_Scissors")
			
		if event_trigger == 1:
			$AnimationPlayer.play("Shaky_02")
		
		event_trigger += 1
	
func shake():
	Shake.shake(10,3)

We also tried it with the event trigger out by one tab, but then neither animation played. Note that prior to moving the event_trigger to the bottom, Shaky_Scissors played fine and the audio came back appropriately. We’ve also tested Shaky_02, and it works as expected; it shakes the camera without a change to the audio.

indolent_gamemakers | 2022-12-03 22:28

Sounds like one of the animations was acting fine and we don’t know which one.
Are you able to post your animation player?

Can I get you to add this line of code right under the event_trigger advance?
print($AnimationPlayer.current_animation, event_trigger)
Can you copy paste the output from the output terminal at the bottom of the Godot editor after running and pressing the key a few times.

Should give me a little more data to work with.
All that will do is print the current animation and the event trigger values every key press.

LordBoots | 2022-12-03 22:37

I’m also on discord if you’d like to add me. Same username as I use on here.

Should be able to help you out much faster there as we can share screenshots or you could even share your screen with me. Notifications are easier to catch too.

LordBoots | 2022-12-03 22:43

Sure thing. Just to double-check, Code currently looks like this:

extends Node2D

var event_trigger = 0

func _process(delta):
	
	if Input.is_action_pressed("ui_select"):
		
		if event_trigger == 0:
			$AnimationPlayer.play("Shaky_Scissors")

		if event_trigger == 1:
			$AnimationPlayer.play("Shaky_02")
		
		event_trigger += 1 
		print($AnimationPlayer.current_animation, event_trigger)
		
func shake():
	Shake.shake(10,3)

Here’s what we got!

— Debugging process started —
Godot Engine v3.5.1.stable.official.6fed1ffa3 - https://godotengine.org
OpenGL ES 3.0 Renderer: GeForce GT 730/PCIe/SSE2
Async. shader compilation: OFF

Shaky_Scissors1
Shaky_022
Shaky_023
Shaky_024
Shaky_025
Shaky_026
Shaky_027
Shaky_028
Shaky_029
Shaky_0210
Shaky_0211
Shaky_0212
Shaky_0213
Shaky_0214
Shaky_0215
Shaky_0216
Shaky_0217
Shaky_0218
Shaky_0219
Shaky_0220
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
— Debugging process stopped —

indolent_gamemakers | 2022-12-03 22:45

I just tried to add you on Discord; it says I need your 4-digit code. You can add us on Discord @ indolentgamemakers#9843

indolent_gamemakers | 2022-12-03 22:53

Does seem to be working as intended, that is if you pressed the key 72 times?

swap is_action_pressed("ui_select") to is_action_just_pressed("ui_select")

is_action_pressed() runs the every frame for as long as the button is pressed.
is_action_just_pressed() runs function only once per press.

I think this is your second issue and should sort out the entire issue.

LordBoots | 2022-12-03 22:54

You’re a godsend. That worked. Thank you SO MUCH!!!

indolent_gamemakers | 2022-12-03 22:56

Hurrah! Glad to be of service. Feel free to add me on discord or facebook for any further issues.

Happy coding!

Hogan Dromgool

LordBoots | 2022-12-03 22:59

Completely forgot about the #code for discord. I’ve sent you a request now anyway.

LordBoots | 2022-12-03 23:04