Gogot 4.4.1 Button

Godot Version

Gogot 4.4.1

Question

I create a Button
I connect the Pressed signal
I add a button sound and a delay to end playback.
After that, I go to another scene.
Everything works
But if you quickly press the button several times, an error occurs.
If you remove the delay on the sound, then there is no error.
How can I solve this problem?

extends Node
@onready var audio_menu_buttons = $AudioMenuButtons

func _on_button_play_pressed():
audio_menu_buttons.play()
await audio_menu_buttons.finished
get_tree().change_scene_to_file(“res://Levels/scene_level_choice.tscn”)

error:
E 0:00:01:702 scene_start.gd:10 @ _on_button_play_pressed(): Parameter “data.tree” is null.

scene/main/node.h:485 @ get_tree() scene_start.gd:10 @ _on_button_play_pressed()

Might be rookie of me, but I believe awaits can build up and continue to run despite the scene or script not being loaded, and when your finished signal is met while the scene/script is not loaded i think it breaks its access to the tree or the rest of that function after the await.

To fix i would add a bool “play_pressed” that gets set to true when you press play, and all the lines of the _on_button_play_pressed() function need to be under if !play_pressed:

var button_pressed:bool
func _on_button_play_pressed():
    if !button_pressed:
        button_pressed = true
        audio_menu_buttons.play()
        await audio_menu_buttons.finished
        get_tree().change_scene_to_file(“res://Levels/scene_level_choice.tscn”)

get_tree().change_scene_to_file - this code should automatically delete the current scene and load a new one. But when you press the button multiple times, because of Await, he doesn’t have time to do it. Interestingly, there are no such problems with TouchScreenButton

extends Node
@onready var audio_menu_buttons = $AudioMenuButtons
@onready var button_play: Button = $UI/ButtonPlay

func _on_button_play_pressed():
# A simple way to prevent this error is to ensure that the user cannot call
# the scene change function again until the current process has finished.
button_play.disabled = true # insert this line
audio_menu_buttons.play()
await audio_menu_buttons.finished

get_tree().change_scene_to_file("res://Levels/scene_level_choice.tscn")