Animation replaying, even though I haven't told it to do that

Godot Version

4.5.1

Question

I don’t understand why, but for one of my scenes, (It’s a boss fight), Whenever I start the take hit animation, the animation just keeps playing, like as if it was in a loop. I checked my code many times, and I only call the take hit animation once, there is nothing in my code that is telling it to repeat whatsoever! Is this a bug with Godot, because I am out of ideas on what to do right now.

The animation does not loop. It says mode 0 which means not looping.

The following is console output, I print out the details from animation player signals, and I specifically printed out all of the times I called animation_player.play(take_hit) To make sure this wasnt me replaying the same animation

take hit animation loop mode: 0

Playing take hit animation...
Started animation: hit
Finished animation: hit
Started animation: hit
Finished animation: hit
Started animation: hit
Finished animation: hit
Started animation: hit
...

Please share the code where you start playing the animation.

Here are the relavant parts.

  • single_drill and finish_drill are only called when the flashcard is submitted
func _ready():
	_3d_flashcard.signal_flashcard_finished_drill.connect(_finish_drill)
	_3d_flashcard.signal_flashcard_single_drill.connect(_single_drill)
	
	_animation_player.play(idle_animation,0.2,idle_animation_speed)
	_animation_player.animation_finished.connect(_animation_finished)
	_animation_player.animation_started.connect(_animation_started)
	_animation_player.animation_changed.connect(_animation_changed)
	
	if(take_hit_animation != null):
		var anim = _animation_player.get_animation(take_hit_animation)
		print("take hit animation loop mode: ", anim.loop_mode)

func _animation_started(animName: StringName):
	print("Started animation: ", animName)

func _animation_changed(animName:StringName):
	#print("Changed animation: ",animName)
	pass

func _animation_finished(animName:StringName):
	print("Finished animation: ",animName)
	if animName == death_animation:
		death_animation_finished = true
	
	if !isDead and animName != idle_animation and animName != fight_idle_animation:
		play_idle_animation()

func play_idle_animation():
	if(fighting and fight_idle_animation != null):
		_animation_player.play(fight_idle_animation,0.4,idle_animation_speed)
	else:
		_animation_player.play(idle_animation,0.4,idle_animation_speed)

func _single_drill(success):
	if(success):
		player.change_health(success_health_add)
		if(!isDead && take_hit_animation != null):
			print("Playing take hit animation...")
			_animation_player.play(take_hit_animation,0.2,take_hit_animation_speed,false)
			_animation_player.seek(0, true)
	else:
		player.change_health(fail_health_add)

func _finish_drill(results:FlashcardDrillResults):
	...

Where do you store these animation names that you recall here?

take_hit_animation
idle_animation
fight_idle_animation
... etc.

Can you check if maybe these aren’t being set to “hit” by accident?
You can also put a breakpoint in your _animation_finished() function and track down the exact path what happens after the hit animation is finished and how it happens to restart itself.

1 Like

what wchc said!

also I would try to leverage Animation Trees in Godot as they can clean up your codebase and reduce the amount of signals needed to just everything into a node!

Also while debugging don’t be afraid to add print statements everywhere. this will help know exactly where the code is going, if you don’t want to use breakpoints

1 Like

The animations are stored within the node itself. All animation code for the entity is executed in the code I have given here. I could track down the cause like you said, However it was very surprising to me because I don’t call hit animation anywhere but here.

Yes, but this snippet doesn’t have the variables, which store the animation names. But nonetheless, even though you set it in code, it could be accidentally changed at runtime, so I suggest you stepping into the code with breakpoints to see the current state.

1 Like