How to declare my player scene

Godot Version

4.3

Question

`how do I declare my player scene in my enemy the player scene is called “Player” im using a lerp and im trying to make the target posistion the players location but its not declared how do I do that here is the code:
extends AnimatableBody2D

var MOVE_SPEED = 200
var TARGET_POSITION = player.posistion

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:
$AnimatedSprite2D.play(“idle”)
position = position.lerp(TARGET_POSITION, delta * MOVE_SPEED)

func _on_area_2d_body_entered(body: Node2D) → void:
print(‘ah’)
`

There are many ways to do that, first:

var player: Node2D  # Declare player variable

func _ready() -> void:
    player = get_node("/root/YourMainScene/Player")  # Adjust path as needed

Or,

func _ready() -> void:
    player = get_tree().current_scene.get_node("Player")

Another way is you can also define a group in the player node, name it player then find the node in enemy, like this:

#Player.gd

func _ready():
    add_to_group("Player") #Defined the group via codes!
#Enemy.gd

func _ready():
    player = get_tree().get_first_node_in_group("Player")

There are more ways, you can use which should fit as of your project.

1 Like

thanks i’ll give it a try

1 Like

sorry I got sick but non of them worked cause I have to do not under a function

Even better is toggling Access as Unique Name on the node and use get_node("%Player")

1 Like