how do load enemy reference in player script

Godot Version

4.2

Question

i am trying to make a successful refence to my enemy inside of my player script for the last 3 days and it seem like it should be some thing simple but i cant figure it out. I need the refence to work so i can use a signal

The parent wont be ready until it’s children are ready, an enemy could be instantiated after the player as a sibling so using @onready will not work.


I would recommend @export if you truely have just one enemy.

If not you could put enemies in a group and use get_tree().get_nodes_in_group("enemies") to iterate through all enemies.

It’s a good idea to add a class_name to the Enemy script so you can tell if a node is or is not an enemy, for example in Area3D collisions. With a class name you can use the is keyword to check the type of a node.

func _on_area_3d_entered(body) -> void:
    if body is Enemy:
        body.take_damage(10)