Wait function in "func _process"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Emmanuele

I need to stop this function for 1-2 seconds when I press one specific key.

Like this way:

func _process(delta):
    do something
if Input.is_action_pressed("letter_B"):
    wait 1-2 seconds``
    do other things
:bust_in_silhouette: Reply From: Pinandu

you cannot stop _process for x seconds

but you can add a timer node and use it like

var waitfortimer = false
func _process(delta):
    if Input.is_action_pressed("letter_B"):
        $Timer.wait_time = 1 # wait_time in seconds
        $Timer.start()
         waitfortimer = true

    if !waitfortimer: # checks if waitfortimer is false
        # here comes the code that must wait for your wait_time
        # more code
        # much more code ;-)

    # outside code will not have the time effect

func _on_Timer_timeout():
    $Timer.stop()
    waitfortimer = false

dont forgot to link the timeout signal of the timer to your script :slight_smile:

I would like to program an AnimatedSprite who uses the bicycle when I pressed the ‘B’ key, so the code in _func(delta) is:

var bicycleActivated = false
    
func_(delta):
    if Input.is_action_pressed("B"):
        if bicycleActivate == false:
             bicycleActivated = true
             print("Bicycle activated")
        else:
            bicycleActivated = false
            print("Bicycle disactivated")


   if Input.is_action_pressed("ui_up"):
	some codes that update the Sprite position...
   elif Input.is_action_pressed("ui_down"):
	some codes that update the Sprite position...
   elif Input.is_action_pressed("ui_left"):
	some codes that update the Sprite position...
   elif Input.is_action_pressed("ui_right"):
	some codes that update the Sprite position...
So with this code, when I press the 'B' button, the function is so fast that takes the input 'B' pressed multiple times and shows in output: Activated, Disactivated, activated for example.

I want to press the ‘B’ key and activate the bicycle, for this reason I asked how to stop for some seconds the _func(delta).

I update the code with your hints like this:

var bicycleActivated = false
var waitfortimer = false
    
func_(delta):
    if Input.is_action_pressed("B"):
        $Timer.wait_time = 1 # wait_time in seconds
        $Timer.start()
           waitfortimer = true

    if !waitfortimer:
        if bicycleActivated == false:
             bicycleActivated = true
             print("Bicycle activated")
        else:
            bicycleActivated = false
            print("Bicycle disactivated")


   if Input.is_action_pressed("ui_up"):
	some codes that update the Sprite position...
   elif Input.is_action_pressed("ui_down"):
	some codes that update the Sprite position...
   elif Input.is_action_pressed("ui_left"):
	some codes that update the Sprite position...
   elif Input.is_action_pressed("ui_right"):
	some codes that update the Sprite position...

func _on_Timer_timeout():
    $Timer.stop()
    waitfortimer = false

When I play the Scene the Output is: bicycle activated, bicycle disactivated without an end, if I pressed B the output stops, but if I pressed another time B nothing is happen.

Emmanuele | 2019-04-11 11:26