+2 votes

I was able to receive _ready and _process in my GDNative C++ class already and was trying to capture InputEvent in _unhandled_input. The method is declared as:

void _unhandled_input(InputEvent event);

And the compilation failed with errors. Compiler (MSVC) seems to complain about not being able to implicitly convert Variant to InputEvent. I assume this is because GDNative script try to pass every parameter of the function using Variant.

So, what is the correct way to define _unhandled_input method inside GDNative C++?

in Engine by (31 points)

Try pointer?

void _unhandled_input(InputEvent* event)

That method is part of Node.

I recommend that you look at the Node class where you have the input (), _ready () _process () among others.

_input

void _input(godot::Ref<godot::InputEvent> event);

unhandledinput

void _unhandled_input(const Ref<InputEvent> event);

unhandledkey_input

 void _unhandled_key_input(const Ref<InputEventKey> event);

.................................................................
.................................................................
.................................................................

To define it _input.

void YouClassName::_input(godot::Ref<godot::InputEvent> event) 
{
}

And don't forget to register the method, otherwise it won't work.

 void YouClassName::_register_methods() 
 {
    register_method("_input",&YouClassName::_input);
 }

.................................................................
.................................................................
.................................................................

An important issue, if you want to use the input event, you have to import the input.hpp library and implement it as follows.

#include "Input.hpp"

void YouClassName::_input(godot::Ref<godot::InputEvent> event) 
{
    Input* _input = Input::get_singleton();
    if(_input == nullptr) return;   

    if(_input->is_action_pressed("a"))
    {

    }
    if(_input->is_action_pressed("d"))
    {

    }
    if(_input->is_action_just_released("a") || _input->is_action_just_released("d"))
    {

    }

}

1 Answer

0 votes

Hello logicor,
Haven't tested this myself, so take it with a grain of salt. Usually those "can't implicitly convert Variant to X" errors come from the fact that Godot passes everything as reference behind the scenes.

Try changing the prototype of the function to :

void _unhandled_input(godot::Ref<godot::InputEvent> event);
by (343 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 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.