{"id":455729,"date":"2025-04-12T03:00:13","date_gmt":"2025-04-12T03:00:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=455729"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=455729","title":{"rendered":"<span>UENUM iteration in Unreal Engine<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>Preface<\/h3>\n<p>I needed to create a category panel for placeable items in the UI \u2014 like the ones you see in city-building games. As a legacy, I inherited a ready-made <code>UENUM<\/code>, which may be modified in the future.<\/p>\n<p>Naturally, I didn\u2019t want to manually create and tweak every single widget \u2014 especially knowing that the list of categories might change later. I wanted something simple and reusable: just run a <code>For each loop<\/code>, generate everything on the fly, and ideally make it work for any enum, not just this one.<\/p>\n<p>And there <em>was<\/em> a way! When you define a <code>UENUM<\/code>, Unreal automatically generates all the metadata and creates a corresponding <code>UEnum<\/code> object, which is a <code>UObject<\/code>. All you have to do is use that data properly.<\/p>\n<hr\/>\n<h3>Creating uenum iterator<\/h3>\n<p>For convenience, we will use <code>UBlueprintAsyncActionBase<\/code>, although it is not the only way.<\/p>\n<p>Let&#8217;s create an inheritor class <code>UEnumIterateAction<\/code>.<\/p>\n<details class=\"spoiler\">\n<summary>.h file<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"cpp\">#pragma once  #include \"CoreMinimal.h\" #include \"Kismet\/BlueprintAsyncActionBase.h\" #include \"EnumIterateAction.generated.h\"  DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnEnumIteration, const uint8, EnumValue);  \/**  *   *\/ UCLASS() class YOUR_MODULE_API UEnumIterateAction : public UBlueprintAsyncActionBase { GENERATED_BODY()  public:     \/** Our execution output at each iteration.*\/ UPROPERTY(BlueprintAssignable) FOnEnumIteration OnEnumIteration;      \/** The function itself, which we will call(almost).*\/ UFUNCTION(BlueprintCallable, BlueprintInternalUseOnly) static UEnumIterateAction* IterateByEnum(UEnum* Enum, const bool SkipMax = true);  protected: virtual void Activate() override;  private: TWeakObjectPtr EnumToIterate; uint8 bSkipMax : 1; }; <\/code><\/pre>\n<\/div>\n<\/details>\n<p><strong> What are we doing here?<\/strong><\/p>\n<p>I won\u2019t dive deep into <code>UBlueprintAsyncActionBase<\/code> \u2014 this article isn\u2019t really about that. I\u2019ll just cover the relevant parts.<\/p>\n<p>First, we create the <code>IterateByEnum<\/code> function, which acts as our constructor. The first argument is the <code>UEnum<\/code> we want to iterate through. Since enums created in Blueprints automatically get a hidden <code>MAX<\/code> value at the end, it\u2019s a good idea to optionally skip it \u2014 so we\u2019ll add a flag for that as the second argument.<\/p>\n<p>The <code>OnEnumIteration<\/code> delegate will be triggered on each iteration. Think of it as the \u201casync\u201d execution pin. Just make sure to use a <code>uint8<\/code> for the value, since that\u2019s how we\u2019ll represent enum entries and cast them back in Blueprints.<\/p>\n<p>Next, we override the <code>Activate<\/code> function from the base class. That\u2019s where the actual logic goes \u2014 the body of our callable node.<\/p>\n<p>And because <code>IterateByEnum<\/code> works like a constructor, we need to store the input arguments for use in <code>Activate<\/code>. So, we\u2019ll add two member fields: <code>EnumToIterate<\/code> and <code>bSkipMax<\/code>.<\/p>\n<p>That\u2019s all for setup \u2014 let\u2019s move on to the implementation.<\/p>\n<h3>Iterator implementation<\/h3>\n<details class=\"spoiler\">\n<summary>.cpp file<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"cpp\">#include \"AsyncActions\/EnumIterateAction.h\"  UEnumIterateAction* UEnumIterateAction::IterateByEnum(UEnum* Enum, const bool SkipMax) { const auto Action = NewObject&lt;UEnumIterateAction&gt;(); if (!IsValid(Action)) return nullptr;  Action-&gt;EnumToIterate = Enum; Action-&gt;bSkipMax = SkipMax;  return Action; }  void UEnumIterateAction::Activate() { Super::Activate();  if (!EnumToIterate.IsValid()) return;  for (int32 Index = 0; Index &lt; EnumToIterate-&gt;NumEnums(); ++Index) {         \/\/ Get the current enum value and check if it is maximal         const int64 EnumValue = EnumToIterate-&gt;GetValueByIndex(Index);         if (bSkipMax &amp;&amp; EnumValue == EnumToIterate-&gt;GetMaxEnumValue()) continue;          \/\/ Convert the enum value to byte, for conversion into blueprints         const uint8 EnumValueAsByte = static_cast&lt;uint8&gt;(EnumValue);          OnEnumIteration.Broadcast(EnumValueAsByte); } } <\/code><\/pre>\n<\/div>\n<\/details>\n<p><strong> What\u2019s going on here?<\/strong><\/p>\n<p>The implementation of our <code>IterateByEnum<\/code> function is pretty straightforward. As mentioned earlier, it basically works like a constructor, so we won\u2019t spend time on it here.<\/p>\n<p>Inside the <code>Activate<\/code> function, we access the stored enum and loop through it using a standard <code>for<\/code> loop \u2014 just like you would with an array.<\/p>\n<p>Unreal stores enum values as <code>int64<\/code>, so that\u2019s the type we get when accessing them by index. But for our purposes, we need a <code>Byte<\/code>, since that\u2019s the only type Blueprints can convert back into an enum value.<\/p>\n<p>It\u2019s also important to skip hidden enum entries \u2014 they\u2019re usually not meant to be shown anyway.<\/p>\n<p>We\u2019ve also added a flag to optionally skip the auto-generated <code>MAX<\/code> entry, which often doesn\u2019t hold any meaningful value either. Since <code>MAX<\/code> is also stored as an <code>int64<\/code>, we compare the raw <code>EnumValue<\/code> with the result of <code>GetMaxEnumValue<\/code> before converting it to a <code>Byte<\/code>.<\/p>\n<hr\/>\n<h3>Result<\/h3>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d6e\/b07\/901\/d6eb0790178f12776b1a1a4714a7cbff.png\" alt=\"Call of the iterator function\" title=\"Call of the iterator function\" width=\"833\" height=\"493\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/d6e\/b07\/901\/d6eb0790178f12776b1a1a4714a7cbff.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d6e\/b07\/901\/d6eb0790178f12776b1a1a4714a7cbff.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>Call of the iterator function<\/figcaption><\/div>\n<\/figure>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/815\/78f\/08d\/81578f08d721d707414e810d28e5b812.png\" alt=\"Output of enum values\" title=\"Output of enum values\" width=\"98\" height=\"80\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/815\/78f\/08d\/81578f08d721d707414e810d28e5b812.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/815\/78f\/08d\/81578f08d721d707414e810d28e5b812.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>Output of enum values<\/figcaption><\/div>\n<\/figure>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eee\/4c4\/d4f\/eee4c4d4f6a7ac2f94909d9bf69cb12e.png\" alt=\"Initial enum values\" title=\"Initial enum values\" width=\"399\" height=\"138\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/eee\/4c4\/d4f\/eee4c4d4f6a7ac2f94909d9bf69cb12e.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eee\/4c4\/d4f\/eee4c4d4f6a7ac2f94909d9bf69cb12e.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>Initial enum values<\/figcaption><\/div>\n<\/figure>\n<p><strong>As you can see, everything works great!<\/strong><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/900138\/\"> https:\/\/habr.com\/ru\/articles\/900138\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>Preface<\/h3>\n<p>I needed to create a category panel for placeable items in the UI \u2014 like the ones you see in city-building games. As a legacy, I inherited a ready-made <code>UENUM<\/code>, which may be modified in the future.<\/p>\n<p>Naturally, I didn\u2019t want to manually create and tweak every single widget \u2014 especially knowing that the list of categories might change later. I wanted something simple and reusable: just run a <code>For each loop<\/code>, generate everything on the fly, and ideally make it work for any enum, not just this one.<\/p>\n<p>And there <em>was<\/em> a way! When you define a <code>UENUM<\/code>, Unreal automatically generates all the metadata and creates a corresponding <code>UEnum<\/code> object, which is a <code>UObject<\/code>. All you have to do is use that data properly.<\/p>\n<hr\/>\n<h3>Creating uenum iterator<\/h3>\n<p>For convenience, we will use <code>UBlueprintAsyncActionBase<\/code>, although it is not the only way.<\/p>\n<p>Let&#8217;s create an inheritor class <code>UEnumIterateAction<\/code>.<\/p>\n<details class=\"spoiler\">\n<summary>.h file<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"cpp\">#pragma once  #include \"CoreMinimal.h\" #include \"Kismet\/BlueprintAsyncActionBase.h\" #include \"EnumIterateAction.generated.h\"  DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnEnumIteration, const uint8, EnumValue);  \/**  *   *\/ UCLASS() class YOUR_MODULE_API UEnumIterateAction : public UBlueprintAsyncActionBase { GENERATED_BODY()  public:     \/** Our execution output at each iteration.*\/ UPROPERTY(BlueprintAssignable) FOnEnumIteration OnEnumIteration;      \/** The function itself, which we will call(almost).*\/ UFUNCTION(BlueprintCallable, BlueprintInternalUseOnly) static UEnumIterateAction* IterateByEnum(UEnum* Enum, const bool SkipMax = true);  protected: virtual void Activate() override;  private: TWeakObjectPtr EnumToIterate; uint8 bSkipMax : 1; }; <\/code><\/pre>\n<\/div>\n<\/details>\n<p><strong> What are we doing here?<\/strong><\/p>\n<p>I won\u2019t dive deep into <code>UBlueprintAsyncActionBase<\/code> \u2014 this article isn\u2019t really about that. I\u2019ll just cover the relevant parts.<\/p>\n<p>First, we create the <code>IterateByEnum<\/code> function, which acts as our constructor. The first argument is the <code>UEnum<\/code> we want to iterate through. Since enums created in Blueprints automatically get a hidden <code>MAX<\/code> value at the end, it\u2019s a good idea to optionally skip it \u2014 so we\u2019ll add a flag for that as the second argument.<\/p>\n<p>The <code>OnEnumIteration<\/code> delegate will be triggered on each iteration. Think of it as the \u201casync\u201d execution pin. Just make sure to use a <code>uint8<\/code> for the value, since that\u2019s how we\u2019ll represent enum entries and cast them back in Blueprints.<\/p>\n<p>Next, we override the <code>Activate<\/code> function from the base class. That\u2019s where the actual logic goes \u2014 the body of our callable node.<\/p>\n<p>And because <code>IterateByEnum<\/code> works like a constructor, we need to store the input arguments for use in <code>Activate<\/code>. So, we\u2019ll add two member fields: <code>EnumToIterate<\/code> and <code>bSkipMax<\/code>.<\/p>\n<p>That\u2019s all for setup \u2014 let\u2019s move on to the implementation.<\/p>\n<h3>Iterator implementation<\/h3>\n<details class=\"spoiler\">\n<summary>.cpp file<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"cpp\">#include \"AsyncActions\/EnumIterateAction.h\"  UEnumIterateAction* UEnumIterateAction::IterateByEnum(UEnum* Enum, const bool SkipMax) { const auto Action = NewObject&lt;UEnumIterateAction&gt;(); if (!IsValid(Action)) return nullptr;  Action-&gt;EnumToIterate = Enum; Action-&gt;bSkipMax = SkipMax;  return Action; }  void UEnumIterateAction::Activate() { Super::Activate();  if (!EnumToIterate.IsValid()) return;  for (int32 Index = 0; Index &lt; EnumToIterate-&gt;NumEnums(); ++Index) {         \/\/ Get the current enum value and check if it is maximal         const int64 EnumValue = EnumToIterate-&gt;GetValueByIndex(Index);         if (bSkipMax &amp;&amp; EnumValue == EnumToIterate-&gt;GetMaxEnumValue()) continue;          \/\/ Convert the enum value to byte, for conversion into blueprints         const uint8 EnumValueAsByte = static_cast&lt;uint8&gt;(EnumValue);          OnEnumIteration.Broadcast(EnumValueAsByte); } } <\/code><\/pre>\n<\/div>\n<\/details>\n<p><strong> What\u2019s going on here?<\/strong><\/p>\n<p>The implementation of our <code>IterateByEnum<\/code> function is pretty straightforward. As mentioned earlier, it basically works like a constructor, so we won\u2019t spend time on it here.<\/p>\n<p>Inside the <code>Activate<\/code> function, we access the stored enum and loop through it using a standard <code>for<\/code> loop \u2014 just like you would with an array.<\/p>\n<p>Unreal stores enum values as <code>int64<\/code>, so that\u2019s the type we get when accessing them by index. But for our purposes, we need a <code>Byte<\/code>, since that\u2019s the only type Blueprints can convert back into an enum value.<\/p>\n<p>It\u2019s also important to skip hidden enum entries \u2014 they\u2019re usually not meant to be shown anyway.<\/p>\n<p>We\u2019ve also added a flag to optionally skip the auto-generated <code>MAX<\/code> entry, which often doesn\u2019t hold any meaningful value either. Since <code>MAX<\/code> is also stored as an <code>int64<\/code>, we compare the raw <code>EnumValue<\/code> with the result of <code>GetMaxEnumValue<\/code> before converting it to a <code>Byte<\/code>.<\/p>\n<hr\/>\n<h3>Result<\/h3>\n<figure class=\"full-width\">\n<div><figcaption>Call of the iterator function<\/figcaption><\/div>\n<\/figure>\n<figure class=\"\">\n<div><figcaption>Output of enum values<\/figcaption><\/div>\n<\/figure>\n<figure class=\"\">\n<div><figcaption>Initial enum values<\/figcaption><\/div>\n<\/figure>\n<p><strong>As you can see, everything works great!<\/strong><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/900138\/\"> https:\/\/habr.com\/ru\/articles\/900138\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-455729","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/455729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=455729"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/455729\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=455729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=455729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=455729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}