How do I make an InputEvent in gdscript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lavaduder

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)`

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

lavaduder | 2019-01-02 13:54

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")

p7f | 2019-01-02 14:08

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

lavaduder | 2019-01-02 14:12

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)`

p7f | 2019-01-02 14:49

Yes. _on_create_preset_input_pressed()

lavaduder | 2019-01-02 15:00

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

p7f | 2019-01-02 15:17

Oh, i see you already found the answer.

p7f | 2019-01-02 15:45

:bust_in_silhouette: Reply From: lavaduder

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 BUTTON_WHEEL_(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)

:bust_in_silhouette: Reply From: p7f

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)
:bust_in_silhouette: Reply From: The Seahorse

For people using the answers here:

I tried doing it with one line:
InputEventKey.new().set_scancode(KEY_W)
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.set_scancode(KEY_W)

So be careful