+2 votes

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++ ?

Godot version v3.4.2.stable.archlinux
in Engine by (20 points)

1 Answer

+1 vote

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 !

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