Is the timer node bugged or what am I doing wrong?

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

Hi,

I’m making a second attempt at learning the engine and GDScript with a simple project but I can’t seem to find a solution to my problem anywhere.

I have a Node Board with the child Delay (the timer). I’m trying to access the timer from board.gd which is attached to Board.

Scene

I reference the timer as, the intellisense recognizes the name $Delay. Added onready as well as specify the type but have tried without as well.

Declaration

I call for the timer to start when the player clicks an object in the scene.

attempting to start the timer

When I run the game and click on said object I get a null error:

error 1
error 2

I’ve tried exporting the Timer var and dragging it in to the inspector field. I get the "Trying to call a function on a null value. I’ve tried recreating the timer in case I messed up any settings and I’ve also dragged in the timer node into the script for the correct path. I’ve tried this in Beta 10,11 and 12 but wasn’t able to open the project in Godot 3.4.

Hopefully the solution is something incredibly silly.

Thanks for your help!

I think the error is elsewhere.

I made a scene in beta 11 with three nodes:

Node2D
Node2D/Node2D_2
Node2D/Node2D_2/Timer

I attached a script to Node2D_2 with the following code

extends Node2D

@onready var timer : Timer = $Timer

func _input(event):
    if event.is_action("ui_accept"):
	    timer.start()
	
func _on_timer_timeout():
    print("Timer timed out")

I started the game and pressed enter and after 1 second the “Timer timed out” message started appearing in the console once per second.

So it’s still a mystery why in your project that reference isn’t getting set up.

haydenv | 2023-01-16 11:19

Hey, thanks for you response. I might copy my assets and scripts to a new project and see if that makes a difference. I might have just twiddled with the wrong setting or broke something somewhere while experimenting.

spaceboi | 2023-01-16 14:50

Agreed. I don’t think the issue is in the posted code. As shown, and as described, that should work as expected…

jgodfrey | 2023-01-16 15:17

Hi again,

It’s solved!

After a lot of trial and error, new projects and in the midst of writing a response with my findings I noticed something in the detailed error message:

enter image description here

In the process of removing anything and all that could cause an issue I removed one of the Node2D nodes and just used the root node (Memory). Yet, the error message refers to “Board” which I thought was an odd reference to the script. But, once again, no other nodes existed in the project, especially with the name Board, since I removed it.

It then dawned on me that I had created a Global variable for the script in the project settings which had that name. Once I removed it, the timer worked! I also realized that my code was running twice as a bonus.

Firstly I didn’t realize that the global was actually running the script automatically (yes, despite the Autoload label), I just presumed it made the script accessible throughout the project. So, for the sake of learning, was it running a separate board.gd script and couldn’t find the node because globals don’t have a connection the the node tree?

Either way, thanks for the help - off to finish this game for my 3 year old !

spaceboi | 2023-01-16 17:11

Ah, that’s great news.

Regarding autoloaded scripts: they are on the scene tree. A bit of explanation is in order. The SceneTree has a root node (of type Window). The main scene of a Godot project is loaded as a child of the SceneTree’s root node. The autoloaded scripts are also loaded as children of the SceneTree’s root node. If you want to see that visually, then while the game is running you can press “Remote” (see the first image in your question). That will show the actual SceneTree, as far as I know, which will include the autoloads.

haydenv | 2023-01-16 17:59

Excellent, thanks! That makes sense, also I’ve wondered if I can see what loads in a scene during runtime so two thumbs up for the Remote view tip! :slight_smile:

spaceboi | 2023-01-16 18:08