How to create a pause for a function "_process"?

Godot Version

4.2.2

Question

I need that when certain conditions are met, “_process” in the script paused for about 2 seconds. I am making a switch for the light, and the problem arose that without this pause The state of the switch changes several times per press. And I want to “recharge” the switch.

cant you add a boolean variable with and test at the beginning of the process-method if its true?

func _process(delta):
    if stop_process:
        return
    ...

Then you just have to set stop_process to false after 2 seconds are over

You can also use set_process(false) to disable the _process(delta) function.

The problem is that I don’t know how to tell the code to count down two seconds! It’s not hard to create logical variables. But I don’t know any time-related functions. And how do I use the “timer” object I still don’t understand.

You can do it with a timer like this:

func pause_process(pause_time):
	set_process(false)
	await get_tree().create_timer(pause_time).timeout # create a timer and wait for it to time out
	set_process(true)

Or with a callback tween like this:

func pause_process(pause_time):
	set_process(false)
	create_tween().tween_callback(set_process.bind(true)).set_delay(pause_time) # call set_process(true) after a certain amount of delay
1 Like