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 (260 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

+2 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,378 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 (260 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 (22 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 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 webmaster@godotengine.org with your username.