How to connect signals to separate scenes (Godot 4, 2d)

Godot Version

4.1.1 Stable


Hello everybody, very new to Godot here. I’m trying pass data from a main script in the main/level scene to instances within the game, specifically a village, in particular its name and population, but more on that later.

Right now, I want to make each instance receive the signals emitted from a single, central timer node located in the main scene, while the instances are an entirely separate scene. The timer itself works fine within its own scene, but when I try to connect it to the village scene (the script for receiving the signal is in the root node btw), I’m only met with trouble. Here’s the script. I’m doing this based on how the documentation describes this to work.

extends Node2D

# Called when the node enters the scene tree for the first time.
func _ready():
	var Timer = get_node("Timer")
	var a = 1
var a = 1
func on_timer_timeout():
	a += 1
	print(a) #var a is just what I'm using to test if the timer works, but at the moment I couldn't even run the program to test /:

The error I get is:

Identifier “_on_timer_timeout” not declared in the current scope

Now, from what I can tell, the example provided in the documentation (Using signals — Godot Engine (stable) documentation in English)
most only work within a single scene, so then the issue ultimately stems from these two scenes being separate. It must not know what Timer refers to then. How can it be solved?

Pretty much the same issue with setting name and population, but additionally that will use a custom signal which I have a decent understanding of. Learning to use the timer should be enough to make all this doable.

Thanks for any help!

UPDATE: Took coder’s advice below, new error happens upon startup of the program, reads, "Invalid get index “timeout” (on base: ‘null instance’).

I believe on_timer_timeout is declared, while _on_timer_timeout isn’t. Shouldn’t it be Timer.timeout.connect(on_timer_timeout)?


Looks like that helped. However, now it’s giving me another error when I run the program, saying, "Invalid get index “timeout” (on base: ‘null instance’).

i suggest to just use an EventBus AutoLoad, it made signals simpler and easier to access

Where is the Node2D being inserted relative to the Timer? The path to the timer may be wrong. get_node is returning a null instance.

The Node2D that the script is contained in is the root node of a separate scene.

As an update, I for lack of a better term “cloned” the village scene as a node and added it into the main scene/level while still keeping the separate scene version of it. If I delete either one of them then the code does not work. However, it seems that I can only instantiate the village once. Multiple attempts don’t do anything.

It’s hard to help without seeing screenshot of your scene tree related to the script file where the code is from.