Godot Version
4.3.stable
Question
Hi, I’m trying to reference the player’s position in the main scene from an enemy scene, so when the enemy is instantiated it will know where to move to find the player. For testing purposes, I have not yet written any movement code, I am just trying to make the enemies look at the player’s postion and track it in real time so I can verify I am referencing the correct position before moving working on the movement. I have tried a few methods I thought would work, and none of them have come close. I’ll outline some of m attempts below.
The player is a pilot, so I named the node “pilot”, and the enemy is called “creep”. The main scene is simply “main.tscn”.
Attempt 1:
var main_scene = preload("res://main.tscn").instantiate()
var target = main_scene.find_child("pilot").position
func _process(delta: float) -> void:
look_at(target)
Failed due to the main scene returning null when instantiated, I tested using .can_instantiate
with this and other scenes, and determined the scene assigned as “main” cannot be instantiated.
Attempt 2:
var player = preload("res://pilot.tscn").instantiate()
var target = player.find_child("CharacterBody2D").position
func _process(delta: float) -> void:
look_at(target)
Similar to first attempt, but now referencing a node in the player scene directly. This one successfully instantiated, but I determined I was using .instantiate()
incorrectly as this did not reference an existing player, it created new pilot scene every time an enemy spawned. However, they did successfully track the pilot that was created in this way, though the movement script attached to the new pilot scenes would not work.
Attempt 3:
var player = NodePath("/root/Pilot")
var target = player.position
func _process(delta: float) -> void:
look_at(target)
This one fails due to NodePath()
returning as a string and not referencing the “pilot” node itself. I also replaced the path with NodePath("/root/Pilot:position")
and tested it with print_debug()
, it seems I misunderstood how nodepaths are intended to be used, they are just the path as a string.
Attempt 4:
@onready var player = get_node("/root/Pilot")
@onready var target = player.position
func _process(delta: float) -> void:
look_at(target)
“node not found” error. Godot simply cannot find “/root/Pilot”.I checked the capitalization, “pilot” is capitalized in the scene tree when it shouldn’t be, but that’s also how it’s referenced in the code so that’s not the problem. I will make the capitalization of nodes consistent after getting the player tracking working.
Attempt 5:
@export var player = Node2D
var target = player.position
func _process(delta: float) -> void:
look_at(target)
Attempted to manually place a creep into the main scene, and use @export
to manually select a node for it to look at. This failed due to “Invalid access to property or key ‘position’ on a base object of type ‘Node2D’.” var target = player.transform.position
resulted in a similar error, except it couldn’t access the transform.
At this point I am stumped and don’t know what else to try, all the tutorials I’ve found are either from earlier versions of Godot and do not apply to this project, or do not reference nodes between scenes. I’ve looked everywhere and can’t find any up to date information on how to do this, though it’s likely I just don’t know the correct keywords to use to find what I’m looking for. I have also asked friends who have used earlier versions of Godot, they all recommended I use the method I tried in attempt 1, and were stumped when I explained that doesn’t work.
To reiterate, what I’m trying to do is track the player’s location in the main scene. The enemy that needs to do this starts in a separate scene, and is instantiated in. So the enemy needs to instantiate with a script that returns the player’s position. So far, I have tried methods to reference the nodes in the main scene directly, and reference nodes in the player scene. Neither option has returned a valid position for the player node in the main scene.
What am I missing? I feel like doing this shouldn’t be this hard, so I must be missing an important detail.
Any help you can provide would be greatly appreciated, thank you.