0 votes

Hello all, how can I create a loop (while or for) this way:

while (input != Input.is_action_just_pressed("confirm")):
>   do something until the key is pressed

I tried some ways, but no one works, like that:

func start_blink(event):
while event != InputEventKey:
    $titulo/aperte.modulate = Color(1,1,1,1)
    yield(get_tree().create_timer(.3), "timeout")
    $titulo/aperte.modulate = Color(1,1,1,0)
    yield(get_tree().create_timer(.3), "timeout")

P.S.: I'm new on godot

in Engine by (171 points)

1 Answer

0 votes
Best answer

You cannot "wait" for an Input.
the builtin functions like _ready or _process need to run without endless loops.

Depending on your need, you can try this:

func _process(delta : float) -> void:
    if not Input.is_action_pressed("confirm"):
        # do something if the key is pressed
        # do something else if key is not pressed

or declare a variable already_pressed and do it like this:

var already_pressed : bool = false

func _process(delta : float) -> void:
    if Input.is_action_just_pressed("confirm"):
        already_pressed = true
    if already_pressed:
        # do something if the key was pressed
        # do something else, if key was not pressed yet

(Code not tested, maybe i mistyped somewhere!)

removed some typos

by (1,520 points)
edited by
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 Frequently asked questions and 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 [email protected] with your username.