Get UEnumProperty Value in C++

I have an actor which holds a struct array which is something like this:

pseudocode

MyActor.h

UENUM()
enum class eMyEnum : uint8
{
    VAL_1,
    VAL_2,
    VAL_3,
    MAX
};

USTRUCT(BlueprintType)
struct FMyStruct
{
    eMyEnum EnumValue = eMyEnum::MAX;
};

TestActor
{
    UPROPERTY()
    TArray<FMyStruct> Values;

bool CanEditChange(const UProperty* InProperty) const override; //editor only

}

I then detect through the CanEditChange function in the code when the struct is drawn and i get it in as the property so i do the following in the .cpp file:

if (InProperty->GetFName() == FName(TEXT("Values")))
{
	const UStructProperty* StructProperty = Cast<UStructProperty>(InProperty);
	if (StructProperty)
	{
		UScriptStruct* ScriptStruct = StructProperty->Struct;
		if (ScriptStruct)
		{
			UProperty* ChildProp = ScriptStruct->FindPropertyByName(FName("EnumValue"));
			if (ChildProp)
			{
				UEnumProperty* EnumProp = Cast<UEnumProperty>(ChildProp);
			
				if (EnumProp)
				{
					//HOW DO I RETRIEVE DATA FROM THE ENUM PROPERTY?
				}
			}
		}
	}

All the casts succeed but I simply can’t get the actual value of enum ouf of it. I’ve tried casting it to UByteProperty (cast successful) but none of the values I retrieved from any of the “Get” functions in that were correct.

Any ideas?

that how i do for bools, so it should work for other values types

		UObject* ownerObject; // in your case 'this' you need to get the pointer to the struct
		UEnumProperty* EnumProp;
		eMyEnum * ptrValue = EnumProp->ContainerPtrToValuePtr<eMyEnum >(ownerObject);
		if (ptrValue)
		{
			if ((*ptrValue) == eMyEnum::VAL_1);
		    {

		    }
	    }

Thanks i’ll give it a go.

I think this is the correct way, but I can’t get ownerObject reference correctly:
EnumProp->ContainerPtrToValuePtr(ownerObject);

Assertion failed: GetOuter()->IsA(UClass::StaticClass()) -- UObject/UnrealType.h] -- [Line: 352] 

I’ve tried using the actor itself, the StructProperty pointer, the StructScript, and every other value i could think of as the ownerObject but it always crashes on the same spot.

I can’t seem to access the struct directly to plug it in like I could in previous cases where I used the Properties in this manner.

I think I will have to find an alternative solution to this as I can’t really wrap my head around it at the moment - but I think the approach is correct.

I think the difficultiy is because you have the struct in an array.
What are you trying to do/ check in this data ?

I wanted to check the value of the enum to dynamically disable fields that shouldn’t be edited based on enum choice. I already do a similar thing (but without array involvement) in another place - just wanted to see if I could get that same customization working through in an array too.

Reality is the correct way to go about it would be through would likely be:

But I just wanted a quick temporary solution and not go into slate level customization but I guess that will likely be the way I’ll have to do it.

thing is “CanEditChange” is called for “Values” so you can only disable the full field, not subfields of the struct or anything…

And if you “know” the “Values” field is being processed, better just iterate on it directly and not use the reflection

This is true, I had a “hacky” workaround where I actually managed to get the project to compile with where I ‘saved’ the value even though it shouldn’t be possible due to const function. And I wanted to use that value no the underlying EditChanges for the specific sub properties of the struct.

But alas I never got that far since I could never get the right value from the enum.

uint8 Value = *EnumProp->ContainerPtrToValuePtr(this);
like this