0 votes

I'd like to emit InputActions via GDScript -- I want certain logic to control UI events and I don't want to have to add code to every UI-aware component in Godot.

But most of the answers to this question suggest calling get_tree().input_event(InputEvent), which is now deprecated. Should not be called manually, override _input_event() instead. Will be removed in Godot 4.0. But it looks like overriding that will only allow me to react to these script-generated Actions in the main loop's code. My goal is to emit Actions which are picked up by all _input(InputEvent) functions, as well as the built-in UI elements.

What's the recommended means of generating Actions via GDScript in Godot 3.2+?

Godot version 3.2.4beta2
in Engine by (103 points)

1 Answer

+1 vote
Best answer

extend InputEventAction in your custom class then use Input.parse_input_event(my_custom_action)

class custom_action extends InputEventAction:
    _init():
        pass

var my_custom_action = custom_action.new()
Input.parse_input_event(my_custom_action)

or

#used to send
Input.action_press("my_custom_action")
Input.action_release("my_custom_action")

#used to receive
if Input.is_action_pressed("my_custom_action"):
    foo()
by (4,478 points)
selected by

Fantastic, Input.parse_input_event(InputEvent) and InputEventAction were what I was missing. You don't even have to create a custom class, you can just do InputEventAction.new() and specify its properties.

While True the custom class allows you to add additional content like for example a twist gesture would have the direction of rotation which is not present in the base InputEventAction

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.