How to use Enums in Blueprint?

I am not a programmer, but I hear many references to enums and how great they are.

Can someone explain how I can put them to use in Blueprint?

(yes this is a mirror of my question about structs :slight_smile: )

Thanks to our experienced member Rama there is this wiki entry: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

So Enum is really just a more readable version of switch on int?

And you switch it with a Byte? What is the advantage of using a byte over say, an Int?

Yeah one advantage is that the switch on an Enum type is more readable.

In short it provides you readability and context-information in the Blueprint by providing the mentioned switch statement and by providing the ability to distinguish between a VehiceType and a WeaponType on the context-layer. That means when you drag a new edge and you will only see methods that work on the type VehicleType for example and not all BP methods that work on an integer.

So if I create an Enum, how do the outputs get tied into the context menu, or do user(blueprint user) created Enums not do that?

Does each Enum output reference a class or blueprint?

Can you shortly clarify what you mean by (blueprint user) create Enums? I did not find a way yet to create a new Enum in BP. Only how to add an Enum Variable in BP.

I’m coming from the C++ corner and there we have to use the UENUM(BlueprintType) annotations which tells the unreal build code to automatically generate C++ code that represents the Enum in Blueprint.

I’m not as deep in UE to tell you how exactly the context-menu works but I think they store the types (enums, classes, structs) and the input/output parameters for every method and use this kind of data structure to show the context-menu, but well that’s more imagination than knowing the facts. ^^

In the Content Browser, click New, then Miscellaneous, then Enumeration. You can then set the outputs of the Enum.

The context stuff may not be as relevant without tying it into C++, but I shouldn’t need it for what I am doing.

The clarification you have provided has been very useful, thanks!

I see. Using less memory seems more than sufficient to justify an Enum! :slight_smile:

Yes they are more readable ints.
In regular C++, an enum value is int but UE4 seems to use TEnumAsByte container for enum variables, thats why it switches on a byte.

A byte can hold values between 0 and 255. If you try to make it 256 it’ll loop around and start back on 0. An int can usually hold values between 0 and 4.3 billion. If you go over the max for an int they too loop around. The difference comes from byte only using a fourth of the memory that an int uses.

Apart from just being easier to read it’s easier for someone else to use your code since all the valid values of your enum is defined. In Ramas example you know you can set it to VE_Dance, VE_Rain, VE_Song and VE_Max. If you only have an int there’s no telling what valid values there are.

So How do you set the value of a custom Enum to actually equal anything?
For example you create an Enum with the Enumerators “Small”, “Medium” and “Large”. If I wanted to make these into Float values with the numbers 50, 100 and 500 set to Small, Medium and Large respectively, how do you do this? I’d have thought you could do that inside the Enumerator itself.

No, an enum is basically using names instead of integers. For your example, you could probably just do a switch on the enum, then do your own multiplication.

What you’re describing is more like an array of floats: [50, 100, 500]. Small is the first element, medium the second, and large the third.

You can create enums from the Content Browser with Add New → Blueprints → Enumeration. You can then add enumerators to it in the BP editor.

1 Like