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

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

Thank you for answering, I have already seen it before. 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);
``` 

``` ruby
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 ..

I think the problem is that "enum" is not a real data type. GDScript converts it to an "int" at compile time.

For example, you can't specify that a variable is of type "enum". You have to use "int".

I've read that in 4.0, that will be fixed.

@DaveTheCoder said: I think the problem is that "enum" is not a real data type. GDScript converts it to an "int" at compile time.

For example, you can't specify that a variable is of type "enum". You have to use "int".

I've read that in 4.0, that will be fixed.

Ok .. Thanks for the clarification .. at least it can be used in the meantime. Don't have the link to the github issue? If you don't have it, that's fine, thank you very much anyway.

2 months later
a year later