can't stop timer in Autoloaded .gd file

Godot Version

v4.2.2

Question

Hey!

I am brand new to Godot working on my first concept, I have created a timer in a global/Autoload script, and want it to run throughout the entirety of my game until the 15th scene, although I can’t seem to stop the timer once I reach this scene. If I load the 15th scene on it’s own the timer does not start, and any other scene starts the timer, although going from another scene to 15, the timer just doesn’t seem to stop.

var glob_time = 0

func global_timer():
	var timer : Timer = Timer.new()
	
	
	
	if get_tree().current_scene.name == "15":
		timer.stop()
		timer.queue_free()
		pass
		

	else:
		
		if get_child_count() == 0:
			add_child(timer)
		timer.one_shot = false
		timer.autostart = false
		timer.wait_time = 1
		timer.timeout.connect(_timer_Timeout)
		timer.start()
	
func _timer_Timeout():
	glob_time += 1

The above is the code for the autoload, and in every scene 1-15 I basically run the global_timer() function on ready. Unsure what I’m doing wrong if anyone can help? Thanks heaps

Since the autoload keeps it’s children between scene you have to store a reference to the timer in global scope.

var timer: Timer
func global_timer():	
	if get_tree().current_scene.name == "15":
		if timer:
			timer.queue_free()
			timer = null
		return

	if timer: # already created timer
		return

	timer = Timer.new()
	add_child(timer)
	
	timer.one_shot = false
	timer.autostart = false
	timer.wait_time = 1
	timer.timeout.connect(_timer_Timeout)
	timer.start()