Is there a way to wait?

hello, im pretty new to GDScript and i just moved from using luau. Usually in luau i use “task.wait(seconds)” to wait, i cant find any way to wait in GDScript, so can anyone help?

Have a look at the SceneTreeTimer:

1 Like

Timers are a good way of waiting, but the _process() function also takes a delta argument which is the time that has passed in seconds (or fractions of seconds) since the last time _process() was called for this object. So you can do something like:

const JACK_IN_THE_BOX_TIME: float = 5.0 # 5 seconds

var countdown: float = 0.0

func start_turning_crank() -> void:
    countdown = JACK_IN_THE_BOX_TIME

func _process(delta: float) -> void:
    if countdown > 0.0:
        countdown = minf(countdown - delta, 0.0) # Subtract delta, clamp at zero.
        if is_zero_approx(countdown):
            pop()
3 Likes

i figured it out, thank you so much!

Hey, @Quinn, when you figure out a solution to your help topic problem, make sure to mark it as the solution via clicking the green checkbox on the reply that contains it.

Edit: You can also make a new reply stating what you did and how you did it. Then, mark that reply as the solution.

I also see you’re new here. Welcome to the Godot Forum!

1 Like

thanks mate, im to the forum so yeah i preciate it

1 Like

This is creative. I would prefer this method however.

const JACK_IN_THE_BOX_TIME: float = 5.0 # 5 seconds

func start_turning_crank() -> void:
	await get_tree().create_timer(JACK_IN_THE_BOX_TIME).timeout
	pop()

First, it’s less code. Second, that _process() function is going to run until queue_free() is called. If I were going to use that process method, I’d add a few lines to save on processing cycles:

const JACK_IN_THE_BOX_TIME: float = 5.0 # 5 seconds

var countdown: float = 0.0


func ready() -> void:
	set_process_input(false)


func start_turning_crank() -> void:
	countdown = JACK_IN_THE_BOX_TIME
	set_process_input(true)


func _process(delta: float) -> void:
	if countdown > 0.0:
		countdown = minf(countdown - delta, 0.0) # Subtract delta, clamp at zero.
	if is_zero_approx(countdown):
		pop()
		set_process_input(false)

Then at least you’re not wasting time in your game processing something you shouldn’t. I do this a lot in state management.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.