Animation Tree and signals

Godot Version

Question

I am working on a project that has a few enemies, and each enemy has a state machine. One of these states is the chase state, and it get’s triggered with an Area2D node’s body entered signal. Once triggered, the base class of the enemy gets the triggering body added into an array that I can access within the chase state, with that I calculate the direction and move the enemy towards the target.

One of the nodes that all enemies share is an AnimationTree node. I thought that this would be the best solution for animations in general and that it would simplify my work. However, I was just attempting to implement an aggro animation just before I send off the enemy to chase down it’s target, and it seemed simple enough before I started. I used the entered_state() function to play the animation, wait for it to be finished and then set a can_chase bool variable to true so that the movement in the physics_process function is allowed to start.

Even though it is working, it feels like the AnimationTree is just adding extra steps in this case. If I had only an animation player I could use a aggro signal on the enemy class and just play that animation with that signal directly on the AnimationPlayer node. It seems like I’m using the AnimationTree poorly… Could anyone help me out with a suggestion for better modularity and just cleaner code?

Here is some print screens of nodes and some relevant code:

The current conditions used to change animations:
image

My current AnimationTree setup for Enemy:

Chase state’s enter_state():

func enter_state():
	if self_body:
		self_body.velocity = Vector2.ZERO
		var aggro_dir : Vector2 = (self_body.hostile_targets[-1].global_position - self_body.global_position).normalized()
		self_body.animation_tree["parameters/aggro/blend_position"] = aggro_dir
		self_body.animation_tree["parameters/conditions/has_new_target"] = true
		await self_body.animation_tree.animation_finished
		self_body.animation_tree["parameters/conditions/has_new_target"] = false
		can_chase = true
		self_body.current_direction = aggro_dir

your animation tree is just another state machine, you can control all animations with your original state machine.

1 Like

I don’t have any suggestions regarding your code but you don’t need to use an AnimationTree if it feels unnecessary. I’m working on a 2D game with very basic animations (no different body parts etc) and I think that AnimationTree just adds more complexity. I find it very awkward to use, editing blend spaces for each animation takes so much time when it all can be done with a simple script instead. Of course, some people like the visual aspect of it but if you are like me and prefer code, maybe you should try removing it and see if it makes things easier? I think it mostly depends on how you like to work and how complex your animations and states are. I can see that AnimationTree is probably very powerful and great tool for more complex animations but you can definitely survive without it if your game is simple.

1 Like

Yea I feel exactly the same way as yourself. I thought that it would be smart and I would thank myself down the line but so far I’m just wondering if at the end of that line it was even worth it. I never had the chance to understand the AnimationTree node that well.

But since the comment above stated that I can control the animations the same way, I guess I just have to call them like I did within my code snippet instead of a simple:

animation_player.play("something_left")
await animation_player.animation_finished

It feels weird and clunky accessing the tree’s parameters…

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.