The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Okay what I am doing is setting up a controls menu. Where the player can change the InputMap's actions. Example:

func _input(event):
    InputMap.action_add_event("ui_accept",event)
    if event.is_action("ui_accept"):
          print(InputMap.get_action_list("ui_accept"))

I also want a preset menu where gdscript manually creates InputEvents. How ever I can't find out how to do that in gdscript. I've tried doing this, but KEY_W is only an int not an actually InputEvent.

func _on_create_preset_input_pressed():
    var new_input = KEY_W
    InputMap.add_action("move_up")
    InputMap.action_add_event("move_up",new_input)`
in Engine by (272 points)
edited by

(SIDE NOTE) What is up with my formatting? i used /`/ why are tabs not showing properly?

for formatting multiline code correctly you have to leave a blank line and write everything with 4 blank spaces indentation extra. Something like this:

(blank line)
(four spaces)func _process(delta):
(four spaces)(four spaces) print("hola")

will render like this

func _process(delta):
    print("hola")

Thank you. Now If only my real question was solved.

hi, i have a question... is only the seccond part what is not working? i mean, this?

func _on_create_preset_input_pressed():
    var new_input = KEY_W
    InputMap.add_action("move_up")
    InputMap.action_add_event("move_up",new_input)`

Yes. _on_create_preset_input_pressed()

I posted an answer with the code you should use. It works for me.

Oh, i see you already found the answer.

3 Answers

+3 votes
Best answer

You can do this:

func _on_create_preset_input_pressed():
    var new_input = InputEventKey.new()
    new_input.set_scancode(KEY_W)
    InputMap.add_action("move_up")
    InputMap.action_add_event("move_up",new_input)
by (3,505 points)
selected by
0 votes

Okay so Godot Has different ways of setting info on InputEvents.
For Input Event Key it's set_scancode and KEY_(WHATEVER)
InputEventKey.new().set_scancode(KEY_W)

For InputEventMouseButton it's set_button_index and BUTTON(WHATEVER) OR BUTTONWHEEL_(WHATEVER)
InputEventMouseButton.new().set_button_index(BUTTON_LEFT)

(For the complete list you can check the source code it's /core/os/input_event.cpp)

by (272 points)
0 votes

For people using the answers here:

I tried doing it with one line:
InputEventKey.new().setscancode(KEYW)
but my game kept crashing and I found out that I had to assign it to a variable first otherwise it doesn't work. Like so:
var event = InputEventKey.new()
event.setscancode(KEYW)

So be careful

by (26 points)
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.