0 votes

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

Godot version 3.3.3 stable
in Engine by (143 points)
edited by

2 Answers

–1 vote
Best answer

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

by (143 points)
selected by
+1 vote
  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");
by (1,438 points)
edited by

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

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
Screenshot_5

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

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 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 webmaster@godotengine.org with your username.