Topic was automatically imported from the old Question2Answer platform.
Asked By
Mentiras
func spawn():
rng = rng.randi_range(15, 0) + press
print(rng)
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”?
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)