Potential Load order issue with Animation

Godot Version

4.6.1 Stable

Question

Hello I’m having an issue with an animation not playing when instanced on my enemy as an on hit effect of a bullet. Here is the flow of events.

  • Bullet is created with an array of item resources

  • Bullet collides with enemy’s hit component

  • hit function is called passing the bullets item resource array as a parameter

  • function navigates through item resource array calling the items on hit function on the entity that was hit

  • on hit function instantiates a scene containing an animation and adds to tree under the entity it was called on

  • animation is played

  • bullet is removed from tree via queue free

I believe there might a load order issue as the animation player is NULL when defined with onready or within the _ready function it only is not NULL if called later like in a function.

Whats weird is it works when trying it on the player https://imgur.com/a/l8KdqSz
However when the enemy is hit nothing happens https://imgur.com/a/NJuyaHh

I’ve confirmed the function to play the animation is being called through debug statements its just that there are no visuals appearing for when an enemy is hit.

Relevant Code
hit component

extends Area3D
class_name Hit_Component

@export var healthComponent:Health_Component


func hit(damage:int,items:Inventory):
	healthComponent.take_damage(damage)
	for i in items.items.size():
		if items.items[i].item:
			items.items[i].item.apply_hit(healthComponent.entity)
	

#body is something like a bullet or grenade
#todo redo inheritance for these types of bodies to unify type for static casting
func _on_body_entered(body):
	hit(body.shotDamage,body.inventory)
	body.Done.emit()

Item Being Tested

extends ItemTemplate
class_name FloppyDisk

func _init():
name = “Floppy_Disk”
description = “Wait its hard”
texture = preload(“res://Assets/Images/disk.png”)
effect = preload(“res://Scenes/Items/FloppyDisk/lightning.tscn”)

func apply_hit(entity):
var lightning:lightning_strike = effect.instantiate().duplicate()
entity.call_deferred(“add_child”,lightning)
lightning.play_effect()

Lightning Effect Script

extends MeshInstance3D
class_name lightning_strike

var anim:AnimationPlayer

func play_effect():
anim = $AnimationPlayer
anim.play(“lightning_strike”)

func _on_animation_player_animation_finished(_anim_name):
queue_free()

Lightning Effect Tree

Runtime Scene Tree

Let me know if anymore information is needed and thank you for any assistance.

Don’t defer adding the child, when you call lightning.play_effect() it’s before it’s added to the scene, _ready and @onready is only called when you use add_child; so your lightning isn’t ready until the end of the frame after you called play_effect

2 Likes

Thanks that fixed getting the animation player with onready. Unfortunately still unable to get the animation to play on the enemy, but thank you for the info it’s much appreciated.

Issue has been fixed. The sprite was not rotated the right way thus the player never saw it. Thanks @gertkeno for your help with the load order.