Statemachine and animations

Godot Version

4.2

Question

So i have made a really basic finite state machine for my enemy, right now it only got idle(walk around) follow(the player) and attack. But im not sure how to make it interact with the rest of the enemy scripts. I have made a health component( node3D) that holds a targets HP and the take dmg function inside of it. But when my enemy dies i wanna stop the state machine and play a death animation. I tried to queue_free() the statemachine and then play the animation but it does not seem to work and would really appreciate some advice.

This is my HealthComponent that holds the health and damage / death function.

class_name HealthComponent
extends Node3D
@onready var animation = $“…/CollisionShape3D/Slime/AnimationPlayer”
@onready var collision_shape = $“…/CollisionShape3D”
@onready var timer = $“…/Timer”
@onready var state_machine = $“…/State Machine”

@export var MAX_HEALTH := 10
var current_health : float

func _ready():
current_health = MAX_HEALTH

func damage(attack: Attack):
current_health -= attack.attack_damage
if current_health <= 0:
state_machine.queue_free()
timer.start()
collision_shape.set_deferred(“disabled”, true)
animation.play(“Dead”)

print(“HealthComponent → Current Health:”, current_health)

func _on_timer_timeout():
get_parent().queue_free()

And this is one of my states.

func Enter():
player = get_tree().get_first_node_in_group(“Player”)

func Physics_Update(delta: float):
if player == null:
print(“Player not found”)
return

var direction = player.global_position - enemy.global_position

if direction.length() < 2:
animation.play(“Attack”)
elif direction.length() > 10:
Transitioned.emit(self, “enemyidle”)
else:
Transitioned.emit(self, “enemyfollow”)

Please write the codes in code format like:

print("Hello World!")
# I used ``` for it like
``` Your codes... Just copy paste ```

Use boolean variable with if statement to check whether they’re active. Surround the entire _process so they cant do anything when set to false.

Dont really understand how i would implement it, got any examples?

Look where the If statement is placed.

class_name GameState

static var CurrentState : BaseState = TestState.new()
static var Active : bool = true

static func _ready():
	CurrentState.Enter()

static func _physics_process(delta):
	if Active == true:
		CurrentState.Update(delta)

static func ChangeState(newstate):
	CurrentState.Exit()
	CurrentState = newstate.new()
	CurrentState.Enter()