How would I create and parse an InputEventAction in GdNative C++ ?

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

In gdscript when writing a bot to control a character I use to do

var _up = InputEventAction.new()

func _ready():
    _up.action = "left_move_up"
    _up.pressed = true

func move_up():
    Input.parse_input_event(_up)

In GdNative I tried many variations around

class Example : public Node2D {
    GODOT_CLASS(Bot, Node2D);
private:
    Input *_input;
    InputEventAction *_up;
public:
    void _ready();
}
void Example::_ready() {
    _input = Input::get_singleton();
    _up = InputEventAction::_new();

    _up->set_action(godot::String("left_move_up"));
    _up->set_pressed(true);
}
void Example::_process(float _delta) {
    InputEvent *ev = Object::cast_to<InputEvent>(_go_up);
    if (ev) {
        _input.parse_input_event(ev);
    }
}

(registering function and such eluded). They mostly segfault, apparently on the call to _input.parse_input_event(ev). How should I proceed in GdNative C++ ?

:bust_in_silhouette: Reply From: lafleur

Well, some hours later I found out that _input->parse_input_event() takes a Ref<godot::InputEvent> as argument, which is (if I’m not wrong) a Godot Reference to an Object. Anyway, if you declare it in the private fields :

private:
    InputEventAction *_up;
    Ref<InputEvent> *_up_ref;

you can then define it eg in _ready() :

_up = InputEventAction::_new();
 _up_ref = Ref(_up);

and then modify _up as you wish ; the reference, of course, will direct to the modified content. When you use it, no need to recast it, simply do

_input.parse_input_event(_up);

typically inside _process().

Comments to this answer very welcome !