GD3 (so _input(event): has been removed?)

RukiriRukiri Posts: 50Member

Can someone please explain the change for input event? Was trying to program a no key event so that my move_speed is reset to default when I double tap run. And yes I have tried !input.is_action_pressed(key) || and so on does not work but works in Unity tho... a lot of my code is being ported from unity.


  • TwistedTwiglegTwistedTwigleg Posts: 294Moderator

    Here's my code for detecting if any key has been pressed:

    func _input(event):
      if (event is InputEventKey):
        # A key has been pressed!

    I only quickly glanced through your post, since I only have a second, but I hope this helps!
    Not sure on why they changed input events though...

  • TwistedTwiglegTwistedTwigleg Posts: 294Moderator
    edited June 2017

    Alright, Now I will do by best to answer. (My apologizes for the delay)

    Can someone please explain the change for input event?

    According to this tweet by Juan, the changes to InputEvent were to help simplify the code base.

    Was trying to program a no key event so that my move_speed is reset to default when I double tap run. And yes I have tried !input.is_action_pressed(key) || and so on does not work but works in Unity tho

    Here are three possible ways I would try, if it were me.

    One way is to use the Input.is_action_pressed. Here's an example:

    var is_left_down = false;
    func _fixed_process(delta):      
      if (Input.is_action_pressed("LEFT")):
        is_left_down = true;
        is_left_down = false;
      if (is_left_down == true):
        # Handle input here!
        # Handle lack of input here!

    Not the 'cleanest' method out there, but it gets the job done, and is relatively simple to implement. A downside with this method is you have to store a boolean for every key you want to track. This can lead to code like this:

    if (is_left_down == false and is_right_down == false and is_up_down == false and is_down_down == false):
      # All of the movement keys are not pressed!

    Which is kinda ugly.

    The other way is using _input. I haven't actually used this, but it would look something like this:

    var action_reference = {};
    func _ready():
      var actions = InputMap.get_actions();
      # For every action, make a blank list
      for action in actions:
        action_reference[action] = [];
      # Add every action's button's scancodes
      for action in actions:
        for action_event in InputMap.get_action_list(action):
          if (action_event is InputEventKey):
    func _input(event):
      if (event is InputEventKey):
        var event_scancode = event.scancode;
        var found = false;
        for actions in action_reference:
          for action_scancode in action_reference[actions]:
            if (event_scancode == action_scancode):
              found = true;
        if (found == false):
          print ("A key was pressed, but not one assigned to a action!");

    The biggest problem with this method, is that some key has to be pressed, so you cannot know if nothing is being pressed unless some other key is pressed.

    The third way is tracking input as accurately as you can. I've tried this before settling on first method and I would say this method has it's flaws, but maybe it's just me. It looks like this:

    var some_key_down = false;
    var key_check_toggle = false;
    func _ready():
    func _fixed_process(delta):
      if (key_check_toggle == true):
        if (some_key_down == false):
          print ("No keys pressed!");
          some_key_down = false;
          print ("Some key(s) is pressed");
      key_check_toggle = !key_check_toggle;
    func _input(event):
      if (event is InputEventKey):
        some_key_down = true;

    The biggest problem with this method is latency and inconsistent results. If you comment out the toggle check code in _fixed_process and watch the console, you see it jitters from pressed and not pressed. Adding the toggle fixes the jitter issue, but introduces latency, which can be seen when you hold down a key and watch the console output.

    I would suggest using the first method. It is a little ugly, but it should be easier to maintain and use.

    That said though, there is probably some way to make the second method work without needing to wait on a key press, I just don't know what it is. The third method is a little bit of a hack, has latency issues, and sometimes gives inconsistent results, but it might just need some more tweaking to make it work.

  • lotregerlotreger Posts: 2Member
    edited July 2017

    Thank you for your answer. I'm working on my code-based game so I had this problem too

  • kow__kow__ Posts: 16Member

    I know this question has already been answered, but you can just use Input.is_action_just_pressed() and Input.is_action_just_released() and check with a timer if they are pressed in a window of time.

Sign In or Register to comment.