How do you animate a combo on hit button?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nighttraveler
:warning: Old Version Published before Godot 3 was released.

Hi folks! I’m always have this doubt. for example some games have diferents animations pressing the same button, like left punch the when you press againg right punch, etc.
How do you accomplish this behaviour?

:bust_in_silhouette: Reply From: kakoeimon

I have done this, this way:

var anim = get_node("AnimationPlayer")
var next_anim = "stand" #this is the animation if there is no combo

func flush_attack():
    next_attack = "stand"

func do_combo():
    anim.play(next_attack)

then inside the place where I check for inputs I get the actions like this

if Input.is_action_pressed("attack_1"):
	var animation = anim.get_current_animation()
	if animation == "attack_1":
		next_attack = "attack_2"
	elif animation == "attack_2":
		next_attack = "attack_3"
	elif animation == "attack_3":
		next_attack = "attack_1"
	else:
		anim.play("attack_1")

Then inside the attack animations I call the function flash_attack near the beginning of the animation and at the end of the animation I call the function do_combo.

P.S. Cause my code is a little bit more complicated I wrote the answer from scratch. For example this code do not take care if you can attack.