My stun animation is not working as in not firing when i attack the enemy

class_name EnemyStateStun extends EnemyState


@export var anim_name : String = "stun"
@export var knockback_speed : float = 200.0
@export var decelerate_speed : float = 10.0

@export_category("AI")
@export var next_state : EnemyState

var _direction : Vector2
var _animation_finished : bool = false


# What happens when we initialize this state?
func init() -> void:
	enemy.enemy_damaged.connect( _on_enemy_damaged )
	print("Enemy State Stun")
	pass

# What happens when the enemy enters this state?
func enter() -> void:
	_animation_finished = false
	#_direction = enemy.DIR_4[ rand ]
	enemy.set_direction( _direction )
	enemy.velocity = _direction * -knockback_speed
	enemy.animation_player.animation_finished.connect( _on_animation_finished )
	enemy.update_animation( anim_name )
	print("EnemyStunEnter")
	pass
	

# What happens when the enemy exits this Stat?
func exit() -> void:
	pass


# What happens during the _process update in this State?
func process( _delta : float) -> EnemyState:
	if _animation_finished == true:
		return next_state
	enemy.velocity -= enemy.velocity * decelerate_speed * _delta
	print("EnemyStunProcess")
	return null
	
	
# What happens during the _physics_process update in this State?
func physics( _delta : float) -> EnemyState:
	return null


# What happens with input events in this State?
func _on_enemy_damaged() -> void: 
	state_machine.change_state( self )
	print("enemy_damaged")


func _on_animation_finished( _a : String ) -> void:
	_animation_finished = true
	print("EnemyAnimationFinished")

My code works fine but the stun animation isn’t happening, i spent like 3 days trying to find the issue. Its when my player attacks the enemy and the enemy isn’t going into stun animation. The rest of my code works but not the stun code. The stun code prints the “Enemy State Stun”, this tells me that Godot know the stun code is there but not when i attack the enemy.

Make a Slime Enemy (part II) // E10 // Make a 2D Action & Adventure RPG in Godot 4

I’m using Godot 4.5.1 and i need the help because I’m stuck and new to Godot

You need to format your code by pasting it between three ticks like so

```gdscript
print("your code here")
```

results in a much more legible

print("your code here")

It would also help if you explained how your code works, including where you think the issue is.

Are you sure the enemy.enemy_damaged signal is emitting? Can you print before/after emitting it?

up until the “print_debug(“Enemy State Stun”)” part of my code works only. It’s when i try to attack the enemy and the enemy doesn’t want to be attacked by my sword. I have a tree that i can attack but not my enemy. The enemy and player cant walk into each other and that is a good thing. Only the attack/damage of the enemy doesn’t want to work. So enemy.enemy_damaged in my opinion isn’t emitting.

Do you have any code that calls enemy_damaged.emit()? Otherwise it will not emit on it’s own.

signal enemy_damaged()

is in my enemy.gd

	if invulnerable == true:
		return
	hp -= damage
	if hp > 0:
		enemy_damaged.emit()
		print_debug( "Enemy Damage")
	else:
		enemy_destroyed.emit()
		print_debug( "EnemyDestroyed")

is at the end of the enemy.gd

Does this print? Do you have any errors?

I have zero errors and zero warnings. Godot 4.5.1 knows that i have the EneyStateStun script that I built from a copy of my enemy wonder/walk script. I just cant get the collision to detect when I attack the enemy with my player’s sword.

print_debug( "Enemy Damage")```

Doesn't print/work

So it doesn’t print "Enemy Damage"? What type is your enemy, and what signal are you using from the Area2D: body_entered or area_entered?

class_name HurtBox extends Area2D

@export var damage : int = 1

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	area_entered.connect(AreaEntered)
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process( _delta: float) -> void:
	pass
	
	
func AreaEntered( a : Area2D ) -> void:
	if a is HitBox:
		a.TakeDamage(damage)
		print_debug("Area Entered")
	pass
class_name HitBox extends Area2D

signal Damaged(damage : int)

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process( _delta: float) -> void:
	pass


func TakeDamage(damage : int) -> void:
	print_debug("TakeDamage: ", damage)
	Damaged.emit(damage)

My Enemy is a characterbody2d

I didn’t set the monitoring and monitorble for the hitbox and hurtbox. Im saying i fixed it on my own.

1 Like