Need help with connecting my player location to my enemy (tracking)

Godot Version

4.4

Question

I’m very new to GDScript and currently trying to make an enemy in my game chase the player, I’ve figured out most of it but I keep getting errors with getTree and trying to access the player. What am I supposed to do? here’s the line for reference:
var player = getTree.getRoot().getNode(“res://player.tscn”)

Okay, first, it looks like you are mixing C# and GDScript syntax. GDScript functions use snake_case. (You are also missing the parentheses of get_tree().)

So the syntax should be:

var player = get_tree().get_root().get_node( ... )

Secondly, get_node()'s argument is the NodePath inside the current scene tree, not the file path of a scene file. So, depending on your scene, it could be something like "Main/Player" or so.

It might be better to use find_child() instead. This function takes the node’s name as argument to search for it. Assuming your node is named Player, it would be:

var player = get_tree().get_root().find_child("Player")

(Depending on when this line is executed, there might also be timing related issues. If it is outside of a function, you should add @onready in front of the line to avoid at least some of these issues.)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.