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"?

asked Apr 7 in Engine by Mentiras (20 points)
edited Apr 7 by Mentiras

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)
answered Apr 7 by Zylann (26,045 points)
selected Apr 7 by Mentiras

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.