My Timeout Function is not working

GODOT 4 Latest Version
My Timeout Function is not working

here’s my code:

extends Control

var timer = Timer
var button = Button

func _ready() -> void:
	$cut_scene.visible = false
	for child in self.get_children():
		if child is Button:
			child.pressed.connect(_on_button_pressed.bind(child))

func _on_button_pressed(button: Button):
	button = button
	timer = Timer.new()
	add_child(timer)
	timer.wait_time = 1.2
	timer.one_shot = true
	timer.timeout.connect(_on_timer_timeout)
	timer.start()
	$cut_scene.visible = true

func _on_timer_timeout() -> void:
	$cut_scene.visible = false
	if button.name == "level1":
		get_tree().change_scene_to_file("res://node.tscn")
	if button.name == "level12":
		get_tree().change_scene_to_file("res://level_2.tscn")

and here’s my tree:
image

I also get the error: E 0:00:01:0911 level_s.gd:19 @ _on_button_pressed(): Timer was not added to the SceneTree. Either add it or set autostart to true.
<C++ Error> Condition “!is_inside_tree()” is true.
<C++ Source> scene/main/timer.cpp:109 @ start()
level_s.gd:19 @ _on_button_pressed()

try this:

timer.call_deferred("start")

1 Like

i replaced timer.start() with that and if thats what i was supposed to do it didnt work

Sorry actually it did work

I have The error invalid access to property or key name on base object of

Another error I saw is that you have two variables with the same name shading. This part:

func _on_button_pressed(button: Button):
	button = button...

change it to this:

func _on_button_pressed(_button: Button):
	button = _button...
1 Like

Thank you so much it works perfectly