0 votes
func spawn():
    rng = rng.randi_range(15, 0) + press
    if rng >= 12:
        print("coin", press)
    if rng < 2:
        print("gem", press)

func _input(_event: InputEvent) -> void:
    if Input.is_key_pressed(KEY_LEFT):
        press += 1
        if press == 5:
            press = 0

What I am trying to do is add a little more random to the random generator. So is there a way to say "if input is any key pressed, add 1 to my pressed total"?

in Engine by (20 points)
edited by

1 Answer

+2 votes
Best answer

This is a wrong use of _input. _input is called once for any kind of event happening (mouse moved, key pressed, key released, joystick moved, MIDI key pressed...). Input.is_key_pressed(KEY_LEFT) checks if the left key is currently held.

In _input, the input information is given to you with the event argument (which you were warned to not ignore I believe, judging by _).
However, turns out _input might allow you to do what you want, if you check event in this way:

func _input(event: InputEvent) -> void:
    if event is InputEventKey and event.pressed and not event.is_echo():
        # A key just got pressed (will be called just once)
by (27,902 points)
selected by

Thank you very much.

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.