0 votes

I coded a controls menu that allows users to press a button, which triggers the following code

func _unhandled_input(event):
 if !event.is_action_pressed("ui_cancel"):
  match keyInput:
    (...)
    3:
      if event is InputEventKey and event.pressed:
        var interact = InputMap.get_action_list("interact")
        InputMap.action_erase_event("interact",interact[0])
        InputMap.action_add_event("interact", event)
        get_node("Node2D/Node2D/Button3").text = InputMap.get_action_list("interact")[0].as_text()
      keyInput = 0

This works great. I press, say, the move up button, press a key, and that key is my new move up input command.

The issue is for assigning mouse button inputs. The below code, which executes when match keyInput == 1 or 2, only picks up scroll wheel movement, and even that not always:

if event is InputEventMouseButton and event.pressed:
  var fire = InputMap.get_action_list("fire")
  InputMap.action_erase_event("fire",fire[0])
  InputMap.action_add_event("fire", event)
  get_node("Node2D/Node2D/Button").text = buttonNames[event.button_index]
keyInput = 0

I tried moving the mouse input section to _input(event), but then it doesn't even pick up scroll wheel movement, which makes me think the issue resides in this executing in the event loop. What am I missing here?

in Engine by (44 points)
edited by

1 Answer

0 votes
Best answer

Solved! After two days of staring at this, it seems to be working. Here's the current code:

(in func _input(event))
...
    if event is InputEventMouseButton and event.button_index != 4 and event.button_index != 5:
        match keyInput:
            1:
                get_tree().set_input_as_handled()
                if event.pressed:
                    var fire = InputMap.get_action_list("fire")
                    InputMap.action_erase_event("fire",fire[0])
                    InputMap.action_add_event("fire", event)
                    get_node("Node2D/Node2D/Button").text = buttonNames[event.button_index]
                keyInput = 0

Upon button pressed, keyInput is set to the appropriate variable and waits for new button press. set_input_as_handled() stops LMB from registering as another button push and takes LMB simple as the new Input to be mapped. I'll probably add something so it won't accept scroll wheel movement, but the basic thing works.

by (44 points)
edited by
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 Frequently asked questions and 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 [email protected] with your username.