0 votes

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
in Engine by (14 points)

1 Answer

+1 vote

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 :)

by (26 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories