input.is_action_pressed is retriggering when other buttons pressed

thistofthistof Posts: 2Member

I'm tweaking GDquest's platform tutorial as a starting point for a platform test. In my test, I'm trying to make it so that tapping right and left to builds speed as if on a skateboard. Essentially something like this:

func _input(event):
if Input.is_action_pressed("move_right"):
speed.x += acceleration
print("move_right")
elif Input.is_action_pressed("move_left"):
speed.x -= acceleration
print("move_left")

So far it's working pretty well (there's a little more to it, but this is the basic idea to demonstrate the problem I'm running into) but with one minor glitch:

I'm using the event input because I don't want it to keep building speed if you hold the D-pad (to which move_left and move_right are mapped) I want the player to have to actually tap the D-pad left or right to manually adjust speed. The problem I'm having is, when any other button is pressed while the D-pad is held in either direction, it retriggers the event input of the D-pad. For example, if I'm already holding right on the D-pad and I press the X button, the console prints "move right" and the player's x speed increases, which should not happen, since I have not retapped the D-pad. Is there perhaps a more relevant input function that I should be using in this case? Or a way to prevent that retriggering?


Tags :

Best Answers

  • kow__kow__ Posts: 24
    edited October 2017 Accepted Answer

    I don't know why this is happening, but in this case it's better if you just use event.is_action_pressed("action"), see if that solves the problem.

  • SomnivoreSomnivore Posts: 102
    Accepted Answer

    Input is a singleton, and it's unrelated to the _input callback. _input is fired every time there's any input, including mouse movement, keyboard presses, gamepad joysticks, and so forth; in your code, every time this happens, the Input singleton is asked to see if a specific action is down, and so long as the player is holding down that action, it will return true, thus, it fires even when other input is detected.

    You would want to use Input in, for example, _process, for continuous action detection; otherwise you would follow the advice above, to use _input's provided event variable to check for input only when it happens; event is particular to the specific kind of input that occurred, e.g. if you didn't press "move_right" that moment, event will just report false if you ask for it.

    "Hey event, are you this action?" "Noooo, try again!"
    vs.
    "Hey Input, can you check this action for me?" "Sure thing boss!"

Answers

  • kow__kow__ Posts: 24Member
    edited October 2017 Accepted Answer

    I don't know why this is happening, but in this case it's better if you just use event.is_action_pressed("action"), see if that solves the problem.

  • jsenajsena Posts: 7Member

    I'm using this and so far it's working perfectly:
    if ev.is_action("move_right") and ev.is_pressed() and !ev.is_echo():

  • SomnivoreSomnivore Posts: 102Member
    Accepted Answer

    Input is a singleton, and it's unrelated to the _input callback. _input is fired every time there's any input, including mouse movement, keyboard presses, gamepad joysticks, and so forth; in your code, every time this happens, the Input singleton is asked to see if a specific action is down, and so long as the player is holding down that action, it will return true, thus, it fires even when other input is detected.

    You would want to use Input in, for example, _process, for continuous action detection; otherwise you would follow the advice above, to use _input's provided event variable to check for input only when it happens; event is particular to the specific kind of input that occurred, e.g. if you didn't press "move_right" that moment, event will just report false if you ask for it.

    "Hey event, are you this action?" "Noooo, try again!"
    vs.
    "Hey Input, can you check this action for me?" "Sure thing boss!"

  • thistofthistof Posts: 2Member

    Thank you for clarifying the difference between input and event, this solved the problem nicely!

Sign In or Register to comment.