Help, I want to write a structure using C++. One of the properties in the structure is an enumeration type, but I want to define the specific enum values in the Blueprint Editor.
To achieve this, I first wrote the following code in the struct declaration: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “My Category”) TEnumAsByte<E_ItemType> Type;
To compile successfully, I also created a temporary enum declaration outside the struct: UENUM(BlueprintType) enum class E_ItemType : uint8 { Weapon, Shield, Clothes };
Then, in the constructor, I attempted a few methods, like:
Using static ConstructorHelpers: static ConstructorHelpers::FObjectFinder ItemTypeEnum(TEXT(“/Game/XXCombat/Enums/Inventory/E_ItemType”));
if (ItemTypeEnum.Succeeded()) {
TEnumAsByte<E_ItemType> Type = static_cast<E_ItemType>(ItemTypeEnum.Object->GetValueByIndex(0));
}
Using FindObject: const UEnum* EnumPtr = FindObject(ANY_PACKAGE, TEXT(“/Game/XXCombat/Enums/Inventory/E_ItemType”), true);
if (EnumPtr) {
Type = (E_ItemType)EnumPtr->GetValueByIndex(0);
}
However, none of these methods worked. The generated Blueprint nodes cannot access the desired enum values. What should I do?"
If you need any further assistance or have any other questions, please let me know.
Thank you for your response. However, I encountered some differences compared to the screenshot you provided, mainly regarding the variable types.
FItemsProperties::FItemsProperties()
{
ConstructorHelpers::FObjectFinder<UEnum> ItemTypeEnum(TEXT("/Game/XXCombat/Enums/Invertory/E_ItemType"));
if (ItemTypeEnum.Succeeded())
{
UEnum* Type = ItemTypeEnum.Object->GetValueByIndex(0);
}
}
And underneath ItemTypeEnum, there is a red underline indicating that “int64” type values cannot be used to initialize entities of type “UEnum*”. Even if I change the type to FText, the error will still be reported.
However, if I change the variable type of Type to int64, it will result in a compilation error stating “declaration of ‘Type’ hides class member.”
ItemsProperties.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Category")
int64 Type;
ItemsProperties.cpp
FItemsProperties::FItemsProperties()
{
ConstructorHelpers::FObjectFinder<UEnum> ItemTypeEnum(TEXT("/Game/XXCombat/Enums/Invertory/E_ItemType"));
if (ItemTypeEnum.Succeeded())
{
int64 Type = ItemTypeEnum.Object->GetValueByIndex(0);
}
}
Actually I want to create a data conversion tool. In Blueprint, it appears as a Blueprint node, and in C++, I’m writing the logic for it. This conversion tool needs to receive some data, and some of this data is of enum type. However, this enum cannot be defined in C++, it can only be defined in Blueprint. So, I don’t expect to receive specific values here. I just want to ensure that the enum property defined in C++ is completely identical to the enum defined in Blueprint.
Well as you can see from my screenshots the display names line up with their index values So yes you can access them and double check that they are the same.
If you want to write up a test of sorts then you could have a string array of expected values and iterate over the enum and double check it.
I used the code from your screenshot to generate a blueprint node called “Create New Data Asset”, but the pins still do not match. This is because the code assigns the value “Weapon” directly to the Type parameter using index (0), whereas I expect the Type on the right-hand side pin to be an enum type defined within the blueprint, specifically the E_ItemType enum.