How do I call to a node in another scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jaspev

I have a global singleton script that I’m trying to call to a timer in. That timer is in another scene (that isn’t my main scene). In this global singleton script, I want a function that uses this timer. The scene that has the timer that I want is called “World”. The global singleton script is just called “Global”.

I found this post from 2017 and they had the same issue I have, but I cannot get any of the examples from the top answer on that post to work on my project.

This is my code (in my global script):

onready var AttackTimer = get_tree().get_root().get_node("/root/World/TimerAttack")

func stopattacktimer():
    AttackTimer.stop()
    AttackTimer.wait_time = 0.1
    AttackTimer.start()

I’ve also tried

onready var AttackTimer = get_tree().get_root().get_node("/root/Global/TimerAttack")

and

onready var AttackTimer = get_node("TimerAttack")

When running all of these, they always return “Attempt to call function ‘stop’ in base ‘null instance’ on a null instance”.

I know I can right click a node and copy it’s node path but it just copies the name of the node for me.

Does it’s path depend on the file system path?
If so, the file path to the scene with the timer I want is res://scenes/World.tscn
The file path to the global singleton is res://scripts/Singletons/Global.gd

I hope someone can help, thank you for reading.

Edit: I don’t know if this is important, but when I copy the node path of the parent scene of the Timer I want to call to, it says the node path is just “.” and I was wondering if that’s normal? Also when I type “get_node(” in my global script, the only auto fill answer is “/root/Global”. And when I run the game and go to the point where I would want to run the function, and I look at the scene structure, I can see World right next to Global, but it just is not recognizing World as a node.
img of scene structure when running the game

:bust_in_silhouette: Reply From: LeslieS

Try using preload:

var AttackTimer = preload("/root/World/TimerAttack").instance()    

(I think onready is not necessary in this context)

When I try that, I get an error:
“Can’t preload resource at path: /root/World/TimerAttack”
“Resource file not found: /root/World/TimerAttack.”

As far as I know, preloading only works with file system paths, but the timer I want to call to is not a scene, it’s a child node of my “World” scene. So I can’t preload the timer, because it’s not in the file system (unless I’m misunderstanding how preloading works, but I replaced my get_root() code with your preload() code and it didn’t work either way).

It didn’t work but thank you for the reply.

Jaspev | 2022-12-21 01:14

You preload the scene to get access to the node.
The scene will be prefixed with “res://”.
It should come up in the autocomplete when you type in preload.
So something like:

var timer_scene = preload("res://Scene_with_timer").instance() 

So when you need the timer from it:

var timer = timer_scene.get_node("TimerAttack")

Again, it should come up in the autocomplete when you type get_node.

LeslieS | 2022-12-21 01:59

It worked! Thank you, sorry I was misunderstanding your implementation of preload but that cleared it up and I understand now.

Jaspev | 2022-12-21 06:50