Godot Version
v4.2.1.stable.official [b09f793f5]
Question
Hello guyz, I’m just exploring godot and I’m following this:
In this lesson, we will look at signals. They are messages that nodes emit when something specific happens to them, like a button being pressed. Other nodes can connect to that signal and call a fu...
I’ve my sprite2d node with a Timer.
With the sprite sprite2d I’ve the following script, like the tutorial said:
func _ready():
var timer = get_node("Timer")
timer.timeout.connect(_on_timer_timeout)
The problem is that get_node(“Timer”) returns null. Probably it’s my fault, but can you guess where I’m wrong?
I’ve added 2 debug lines:
func _ready():
print_debug("node '%s' tree follows:" % name) # debug
print_tree() # debug
var timer = get_node("Timer")
timer.timeout.connect(_on_timer_timeout)
And that’s the result:
node ‘Sprite2D’ tree follows:
At: res://sprite_2D.gd:6:_ready()
.
This is the project file:
https://www.file.io/fm6k/download/faf396jImLL2
Instead of get_node, create @onready declaration with the correct node path. Just drag the node Timer to your GDScript while holding the Ctrl key. It should generate something like this:
@onready var timer = $Sprite2D/Timer
Thx for the reply.
Dragging the Timer generate this:
@onready var timer = $Timer
But still problem with this code:
@onready var timer = $Timer
func _ready():
print_debug("node '%s' tree follows:" % name) # debug
print_tree() # debug
#var timer = get_node("Timer")
timer.timeout.connect(_on_timer_timeout)
E 0:00:00:0624 sprite_2D.gd:4 @ _ready(): Node not found: “Timer” (relative to “/root/Sprite2D”).
<Errore C++> Method/function failed. Returning: nullptr
<Sorgente C++> scene/main/node.cpp:1638 @ get_node()
sprite_2D.gd:4 @ _ready()
News about the issue:
My Sprite2D Node is a scene (sprite_2d.tscn)
If i had the timer in the scene the code works.
But if i had the timer in the istance of the scene the script dosen’t see it.
Is this normal?
te1ny
March 17, 2024, 3:19pm
6
hmm, to be honest I don’t see the error, but I can suggest another way. create a timer through the code.
In GDscript it should look like this:
var timer = Timer.new()
timer.autostart = ...
timer.waittime = ...
timer.oneshot = ...
after that, in the _ready function add this timer to the scene.
func _ready():
add_child(timer)
timer.timeout.connect(...)
I found the “stupid” error.
The project just run the sprite_2d.tscn scene as default. In that scene no Timer exist.
The part the fooled me was that when you edit the script in a scene istance you edit the script even in the main scene. To solve i’ve just changed the the main scene in the project setting.
Sorry for the waste of time and thanks to all you
1 Like
system
Closed
April 16, 2024, 3:29pm
8
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.