I’m implementing a battle cry animation for characters using the “AnimationPlayer” node, but when the character loses, I need to stop the animation from playing and play a losing animation instead.
Knight script for battle cry:
@ onready var can_animation_play = true
func Cry():
if can_animation_play == true:
animation_play(“Battle cry”)
else:
pass
Game script:
@ onready var game_over = false
if score == 100:
knight.Cry()
func game_over():
if game_over == false:
knight.can_animation_play = false
However, this script doesn’t work, and the character first plays the “battle cry” animation and only then plays the losing animation and only then disables animation playback, but I need to immediately interrupt the animation after losing, even if it has already started playing.
How can I implement this? Thanks a lot for your help! If anything is unclear or you have any solutions to this problem, feel free to write in the comments.
(I use Godot 4.)
@export var animation_player
emun AnimationState {
Idle,
Walking,
Attacking,
Crying,
Killed,
}
var animation_state : AnimationState
func attack():
if animation_state == AnimationState.Cry or animation_state == AnimationState.Killed or animation_state == AnimationState.Attacking:
return
animation_state = AnimationState.Attacking
animation_player.play("attack")
await animation_player.animation_finished
if animation_state == AnimationState.Attacking:
animation_state = AnimationState.Idle
func cry():
if animation_state == AnimationState.Cry or animation_state == AnimationState.Killed or animation_state == AnimationState.Attacking:
return
animation_state = AnimationState.Cry
animation_player.play("cry")
await animation_player.animation_finished
if animation_state == AnimationState.Cry:
animation_state = AnimationState.Idle
func on_killed():
if animation_state == AnimationState.Killed:
return
animation_state = AnimationState.Killed
animation_player.play("die")
I am no expert in state machines (I think it is intended to use a node based state machine, but in my opinion that’s a bit messy with so many scripts for such few functionality)