Godot Engine C++ and Gdnative. How to use enumerations? "Data type enum problem with GDnative C++"

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

I am testing with Godot engine and C ++ but I cannot use enums.

I mean the enumeration declares it in the following way.

 public:
    enum EnumerationType
    {
        UNIT_NEUTRAL,
        UNIT_ENEMY,
        UNIT_ALLY
    };

Enum data type in variable.

MyClass::EnumerationType variableTest ;

The error comes out.

error C2440: 'return': unable to convert from 'godot :: Variant' to 'T'
             with
             [
                  T = godot :: MyClass :: EnumerationType
             ]

Does anyone have any small example projects where they use enums with Godot Engine C ++ and GDnative?

Does anything need to be done to make the C ++ enum data types work inside godot?

Could GDscript’s enum datatype be used in C ++?

:bust_in_silhouette: Reply From: sash-rc
  1. You can use C++ enums in C++ part without any limitations
  2. Godot’s enums aren’t actually a type, but like a set of const literals.
  3. If you want to pass it in and out of Godot, use int

For a nice property in inspector, you could use

enum MyEnum { E1 = 0, E2, E3};
// CClass
MyEnum myEnum;
void setEnum(int newValue) { myEnum = (MyEnum) newValue; }
int getEnum() { return (int) myEnum; }

register_property<CClass, int>("MyEnum", &CClass::setEnum, &CClass::getEnum, (int) E1,
  GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT,
  GODOT_PROPERTY_HINT_ENUM,  "E1, E2, E3");

When I want to register the property

in .h

public:
    enum MyEnum { E1 = 0, E2, E3};
    MyEnum myEnum;
    void setEnum(MyEnum newValue) { myEnum = newValue; }
    int getEnum() { return myEnum; }

in .cpp when registering the property

register_property<PruebaEnumeracion, int>(
        "MyEnum",
        &PruebaEnumeracion::setEnum,
        &PruebaEnumeracion::getEnum,
        E1,
        GODOT_METHOD_RPC_MODE_DISABLED,
        GODOT_PROPERTY_USAGE_DEFAULT,
        GODOT_PROPERTY_HINT_ENUM,
        "E1, E2, E3"
    );

There is an error and it says this I don’t understand why it

no instance of overloaded function "godot::register_property" matches the argument list -- argument types are: (const char [7], void (godot::PruebaEnumeracion::*)(godot::PruebaEnumeracion::MyEnum newValue), int (godot::PruebaEnumeracion::*)(), godot::PruebaEnumeracion::MyEnum, godot_method_rpc_mode, godot_property_usage_flags, godot_property_hint, const char [11])C/C++(304)
register_property

ariel | 2021-09-02 17:44

I imagine that there must be problems on many sides if we use the enum as the data type, in my case the problem was a parameter with the data type that is enum

ariel | 2021-09-02 19:11

There was an typo (fixed): of course you need to cast enum to int and vice versa: data types of setter/getter should match declaration in register_property

sash-rc | 2021-09-02 19:53

:bust_in_silhouette: Reply From: ariel

Unfortunately the datatype does not seem to work when using an enum with GDnative.
The solution I found for now is to convert everything of the data type from “enum” to int … Although I would like to use the enum data type in the variables to have the best organized code.

public:
    enum TestTipeEnum 
    {
        UNIT_NEUTRAL,
        UNIT_ENEMY,
        UNIT_ALLY,
        NONE
    };

    int _TestTipeEnum;

private:
    void SelectNumberType(int _TestTipeEnum);





void PruebaEnumeracion::SelectNumberType(int _TestTipeEnum) 
{
    switch (_TestTipeEnum)
    {
        case PruebaEnumeracion::TestTipeEnum::UNIT_ALLY:
            Godot::print("UNIT_ALLY");
            break;
        case PruebaEnumeracion::TestTipeEnum::UNIT_ENEMY:
            Godot::print("UNIT_ENEMY");
            break;
        case PruebaEnumeracion::TestTipeEnum::UNIT_NEUTRAL:
            Godot::print("UNIT_NEUTRAL");
            break;
        case PruebaEnumeracion::TestTipeEnum::NONE:
            Godot::print("NONE");
            break;
        default:
            Godot::print("Incorrect data type.");
            break;
    }
}





void PruebaEnumeracion::_ready() 
{
    Godot::print("Now it will show the data types");
    SelectNumberType(TestTipeEnum::UNIT_NEUTRAL);
    SelectNumberType(TestTipeEnum::UNIT_ENEMY);
    SelectNumberType(TestTipeEnum::UNIT_ALLY);
    SelectNumberType(TestTipeEnum::NONE);
}

Specifically, what you cannot do is declare the variables with the “enum” data type. I don’t know if to report it as a godot problem. But this may be a not very good solution, especially when the code starts to grow …