Waittime between Scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rayu
func _ready() -> void:
	$ScoreCount.text = str(Globals.Score) 
	set_process(true)
	spawn_Player1(1)
	for i in range(4 + Globals.Score): 
		spawn_Enemy1()

func spawn_Player1(num):
	for i1 in range(num):
		var p1 = player1.instance()
		p1.connect("Player1_pressed", self, "_on_Player1_pressed")
		Player_container1.add_child(p1)


func _on_Player1_pressed():
	if Globals.timeLeft > 0:
		Globals.timeLeft += 5
		if Globals.timeLeft > Globals.maxTime:
			Globals.timeLeft = Globals.maxTime
	Globals.Score += 1
	get_tree().change_scene("res://Game.tscn")
	
func spawn_Enemy1():
	var rand1 = floor(rand_range(0, Enemy1.size()))
	var piece1 = Enemy1[rand1].instance()
	add_child(piece1)

This is my Main Scene Code.

I want that when the on_Player1 pressed event happens that the Scene doesn’t start right away, it should wait like 2 seconds before load to the next Scene (i tried some things with the Timer Node). And if the on_Player1 pressed event happends and the Waittime is set, i want it that the Player Freezes and Enemies are not visible for the duration of the Waittime. And it should not register Score more than 1 time for each Scene.

Thank you in advance for AndyCampbell and jgodfrey :confused: i still wait for the Random Functions solution, i sent the code on a comment there.

:bust_in_silhouette: Reply From: jgodfrey

For the main question (delay scene load for 2 seconds), you can just add something like this to your existing function:

func _on_Player1_pressed():
    if Globals.timeLeft > 0:
        Globals.timeLeft += 5
        if Globals.timeLeft > Globals.maxTime:
            Globals.timeLeft = Globals.maxTime
    Globals.Score += 1
    yield(get_tree().create_timer(2.0), "timeout") // <--- Add this
    get_tree().change_scene("res://Game.tscn")

That’ll dynamically create a timer, set it to run for 2.0 seconds, and wait for its timeout signal to fire before executing the remaining code (the scene change).

For your other questions, I’d recommend you ask them separately rather than include them as part of this one…