Hi, how can I reference an Enum created in Blueprints into C++?
I’m writing my code inside UCharacterMovementComponent (which is an Inherited component) and the Enum variable is created inside the parent Character Blueprint.
The Enum itself is stored on the root /Content/ folder.
Appreciate your help.
This is on available from 's Victory Plug-in, from what I can see here. Did you download and install this plugin? Can you post your code if possible?
Based on what I have found in this thread: Refer blueprint enum in C++ - Blueprint - Unreal Engine Forums
You cannot reference an Enum created in Blueprints within C++. Your best option here is to create this Enum in C++ and reference it in your Blueprint instead.
in .h:
UCLASS()
class MYPROJECT3_API UNewCharacterMovementComponent : public UCharacterMovementComponent
{
GENERATED_BODY()
public:
UNewCharacterMovementComponent();
//Get BP Enum
static FORCEINLINE UEnum* GetBPEnum(UObject* Obj, const FName& Name, uint8& Value)
{
Value = 255; //invalid
if (!Obj) return NULL;
if (!Obj->IsValidLowLevel()) return NULL;
if (Name == NAME_None) return NULL;
//~~~~~~~~~~~~~~~
FByteProperty* ByteProp = FindFProperty<FByteProperty>(Obj->GetClass(), Name);
if (ByteProp != NULL)
{
void* ValuePtr = ByteProp->ContainerPtrToValuePtr<void>(Obj);
Value = ByteProp->GetUnsignedIntPropertyValue(ValuePtr);
return ByteProp->GetIntPropertyEnum();
}
return NULL;
}
}
in .cpp:
UEnum* GetBPEnum(UObject* Obj, const FName& Name, uint8& Value)
{
uint8 ValueFound = 255;
UEnum* TheEnum = GetBPEnum(MyActor, FName("ItemType"),ValueFound);
if (TheEnum && ValueFound != 255)
{
UE_LOG(YourLog,Warning,TEXT("Item saved to hard disk ~ ", TheEnum->GetEnumName(ValueFound));
}
}
But I’m not sure if I’m putting the code in the right place. This is inside UCharacterMovementComponent and I’m not using any plugin.
This is the output error:
Simply declere Enum in C++, it will save you a lot of hassle and let you direcly refrence enum in C++ and use it normally in Blueprints (if you use UENUM).
Same goes with Structs. Blueprints structs and enums was made primerly for blueprint only projects in mind, if you got C++ there no point of using them, you only making things difficult for yourself.
1 Like