# How to properly loop through enumerations

**URL:** <https://forums.unrealengine.com/t/how-to-properly-loop-through-enumerations/333671>\
**Category:** Blueprint\
**Tags:** enumeration, unreal-engine, Blueprint, question\
**Created:** [September 28, 2015, 4:58pm UTC](https://forums.unrealengine.com/t/how-to-properly-loop-through-enumerations/333671 "2015-09-28T16:58:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_f0c60e70](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f0c60e70](https://forums.unrealengine.com/u/anonymous_user_f0c60e70)\
**Post date:** [September 28, 2015, 4:58pm UTC](https://forums.unrealengine.com/t/how-to-properly-loop-through-enumerations/333671/1 "2015-09-28T16:58:05Z")

</div>

I created a enum and added entries. if I loop through them using “ForEach myEnum” node in a blueprint, some of the enum values will come out as none. Converting the byte to int, the values range from 0 to 1 minus the number of entries. I then notice that the some of the enum values are outside that range.

Is this the correct behavior I should expect?

How should I loop through my enumerations and have all the values come out correct?

---

<div class="post-metadata">

**Author:** ![Slayemin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Slayemin](https://forums.unrealengine.com/u/Slayemin)\
**Post date:** [September 28, 2015, 7:13pm UTC](https://forums.unrealengine.com/t/how-to-properly-loop-through-enumerations/333671/2 "2015-09-28T19:13:04Z")

</div>

An enumeration is just a series of integers. There is no guarantee that those series of integers will be in sequential order – in fact, don’t ever count on that! enums can often be used for bitmask flags. Enums are really just a more “people friendly” way to associate meaning to numbers.

Here’s a good example:

Enum CoinValue  
{  
Penny = 1,  
Nickle = 5,  
Dime = 10,  
Quarter = 25,  
HalfDollar = 50,  
Dollar = 100  
};

Within UE4, all enum values are going to be unsigned integers with 8 bits, so the range of values is 0 → 255.

As far as looping through the enums, I’d continue using the foreach enum node as you’re doing. Just be aware that enum values won’t be guaranteed to be sequential. You can also cast an enum value to an integer value. I’m not sure what happens to a existing enum byte values when you delete an enum from the list – I think the byte values stay the same? And then adding a new enum continues where the last value left off? I’d have to test this more (I personally use C++ enums).

---

<div class="post-metadata">

**Author:** ![Socke81](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/socke81/32/1194535_2.png) [@Socke81](https://forums.unrealengine.com/u/Socke81)\
**Post date:** [May 16, 2022, 1:40pm UTC](https://forums.unrealengine.com/t/how-to-properly-loop-through-enumerations/333671/3 "2022-05-16T13:40:33Z")

</div>

Since I searched for it myself and found a good solution in the UE4 code I post my solution here. Even if this thread is old.

```auto
const UEnum* hardwareInfoEnum = StaticEnum<EHardwareInfo>();

	for (int32 i = 0; i < hardwareInfoEnum->NumEnums() - 1; i++){
		UE_LOG(LogTemp, Warning, TEXT("A: %s"), *hardwareInfoEnum->GetDisplayNameTextByIndex(i).ToString());
	}

```

---

<div class="post-metadata">

**Author:** ![clait](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@clait](https://forums.unrealengine.com/u/clait)\
**Post date:** [November 14, 2022, 9:09am UTC](https://forums.unrealengine.com/t/how-to-properly-loop-through-enumerations/333671/4 "2022-11-14T09:09:02Z")

</div>

Thanks - works like a charm!

---

<div class="post-metadata">

**Author:** ![RadicalGratitud](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RadicalGratitud](https://forums.unrealengine.com/u/RadicalGratitud)\
**Post date:** [April 6, 2024, 9:17pm UTC](https://forums.unrealengine.com/t/how-to-properly-loop-through-enumerations/333671/5 "2024-04-06T21:17:01Z")

</div>

Liked [ben ui’s examples](https://benui.ca/unreal/iterate-over-enum-tenumrange/) for looping through enums:  
Add a hidden Count enum value as your last enum, and add an ENUM RANGE macro right after your enum declaration:

```auto
UENUM()
enum class EAnimal : uint8
{
	Dog,
	Narwahl,
	Count UMETA(Hidden)
};
ENUM_RANGE_BY_COUNT(EAnimal, EAnimal::Count);

```

Then you can use a for loop with your enum:

```auto
for (EAnimal Animal : TEnumRange<EAnimal>())
{

}

```

---

<div class="post-metadata">

**Author:** ![Spinker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/spinker/32/1087255_2.png) [@Spinker](https://forums.unrealengine.com/u/Spinker)\
**Post date:** [April 6, 2024, 9:47pm UTC](https://forums.unrealengine.com/t/how-to-properly-loop-through-enumerations/333671/6 "2024-04-06T21:47:40Z")

</div>

![UnrealEditor_pHHzndPPhr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/7/6/f76a5e96e958a0cdc9c47230f3d15524f95a6505.png)  
I’m used to do that in BP, works well 🙂
