x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: UE 4.20
Tags:
more ▼

asked Oct 02 '18 at 09:28 AM in C++ Programming

avatar image

Mercus
138 9 12 163

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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);
             {

             }
         }
more ▼

answered Oct 02 '18 at 10:54 AM

avatar image

Firefly74
1.6k 15 18 34

avatar image Mercus Oct 03 '18 at 08:28 AM

Thanks i'll give it a go.

avatar image Mercus Oct 03 '18 at 09:40 AM

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.

avatar image Firefly74 Oct 03 '18 at 01:20 PM

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

avatar image Mercus Oct 04 '18 at 08:07 AM

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: https://docs.unrealengine.com/en-us/Programming/Slate/DetailsCustomization

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.

avatar image Firefly74 Oct 08 '18 at 05:30 PM

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

avatar image Mercus Oct 10 '18 at 12:07 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question