+1 vote

Hello everyone,

I try to access a function from my TileMap-Node through another Node2D-Node.
Both Nodes are under a scene/node called "game_window".
In the basic Node2D I wrote

onready var tilesetnode = getnode("/root/game_window/TileMap")

but it get the error message that the node isn't found. I got the path by displaying it with getpath(). The getnode() function only works for me when the node I try to access is a subnode, absolute paths don't seem to work.

Does anyone know why it doesnt work out?

asked Jul 24, 2019 in Engine by obstsalat (24 points)

Two ideas:
Only refer to nodes that are children of the node you have as root for the scene. Don't try to refer to a higher-level node from a leaf. Accessing with an absolute path isn't needed.

If you use setget anywhere in the script, and those setters set things on referenced nodes... you'll probably encounter the exact problem I had. Godot runs setters on export variables before _init and _ready... so this works around that by killing the setter early if _ready has never been called.

var is_ready = false

var setter_thing setget set_setter_thing

var set_setter_thing(setter):
    if not is_ready:
        return
    # whatever you need to do

var _ready():
    # everything needed on ready
    is_ready = true

1 Answer

+1 vote
Best answer

I've solved it by myself. I just start the path with "../" if the Nodes have the same place in the hierarchy.

answered Jul 24, 2019 by obstsalat (24 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.