Iterating over enums with TFieldIterator

Hi Guys,
I am making quite heavy use of the reflection system in the engine. I am successfully iterating over floats, bools and ints using

for (TFieldIterator<UIntProperty> it(module->GetClass()); it; ++it)

for example. I thought I could achive the same thing for enums using

for (TFieldIterator<UEnum> it(this->GetClass()); it; ++it)

however, this simply does not work for me. The iterator doesnt hit my enum, which is defined like this:

UENUM()
enum class ETestEnum : uint8{
	A1,
	A2,
	A3
};

I declared the property like this

UPROPERTY(EditAnywhere)
		ETestEnum TestEnum = ETestEnum::A1;

and this

UPROPERTY(EditAnywhere)
		TEnumAsByte<ETestEnum> TestEnum = ETestEnum::A1;

neither worked.

Some Context:

What I want to achive in the end, is to display combo boxes for arbritary enums classes, that are members of arbitrary objects, and modify the enum value in the object based on the combo box.

My Approach right now is to find every enum property of an object, find the names for all enum values of these enums, and display a comboBox with them in it. I can’t really do this in blueprint, since I am working with pointers here.

I am basically trying to copy the functionality of the “ForEach EnumType” Blueprint Node in C++, but with a “EnumType” input instead of hard coded.

I would really appreciate your help!

Simon

UEnum represets enum type not enum property like UIntProperty, enum type can’t be assined as a part of the class. TFieldIterator will only work with UProperty fields, the non-property reflection system objects like UClass, UStruct or UEnum require longer and heavier TObjectIterator, if you gonna do it do it once and collect those objects you need.

So where is enum property then? There seem to not be one :stuck_out_tongue: reflection system see them as UByteProperty (Byte is also other name for uint8) and UByteProperty and UNumericProperty (base of all number property types) itself got Enum function which includes IsEnum which let you detect if it enum property

https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UNumericProperty/IsEnum/index.html

And you can get UEnum of property from

https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UNumericProperty/GetIntPropertyEnum/index.html

Hi! I havent’t tried this for run-time selected enums, but I’m guessing templates should be the way to go.

https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Core/Public/Misc/EnumRange.h

Take a look here and implement the ENUM_RANGE_BY_*** macro for the enums you want to be able to do this with.

Then can use the templated Iterator-range function like this (where EnumClass is your enum class)

for (EnumClass AnEnum : TEnumRange<EnumClass>) {}

If you put this for into a function templated on your enum classes, I think your goal is achievable :slight_smile:

Cheers, Tommy.

Close! It seems TEnumRange needs invoking because it’s a function, like so: for (TEnum value : TEnumRange<TEnum>()){}