Yes, it is far easier to simply right-click the content browser and select a new enumeration (enum) or structure (struct) for use in what could be a mostly blueprint game…
You pay back that simplicity in the long run almost every. single. time. While it may scare a beginner to get there hands dirty in converting their project to c++ for the use of the highly-useful enums and structs, you will thank yourself down the track that you did.
Okay, what are enums and structs anyway?
Where a Boolean sits as either a 0 (false) or 1 (true), think of an enumerator as a longer Boolean where we can have from 0 to 255 (as it inherits from uint8), where each value corresponds with a name you’ve given:
This snippet has been stolen from another post on better definitions for enums in UE C++.
In blueprint it would look like this:
Think of structures as a collection of variables that sit in one big variable. This is very useful for holding data that is only going to be accessed at the same time as the rest of the data. Normally if you want the name of the sword, you are going to be soon using its damage and rarity. So those can go together like so:
In blueprint it would look like this:
If everything else is blueprint then that is okay, just ensure that your structs and enums are in C++, it is a better practice and if you ever end up moving some blueprint code to C++ you’ll be thankful that everything already references the C++ structs and enums. Otherwise you will be due for a ton of replacing references.
From the level view, navigate to the Tools menu and select New C++ Class:
Then go to All Classes and select Object, then click Next:
Name it what you want, we are only interested in the header file (YourName.h).
Ensure you select Public for ‘Class Type’ and click Create Class. You should be greeted with this:
To build from your IDE you will need Visual Studio Community 2022 (for use in 5.3), follow this link by Epic for that setup.
Navigate to your project folder and double-click the .sln that should now be created:
If it isn’t there, right-click your .uproject and select Generate Visual Studio project files:
Once your solution is open and loaded, go ahead and open up your NewFile.h from the solution explorer on either your left or right. I am using JetBrains Rider here, the process is still the same.
You can go ahead and delete the:
As we won’t be needing it, now we can get started.
Go ahead and add a new enum below the #include "YourFile.generated.h" like so:
Ensure you specify BlueprintType in the UENUM() braces, otherwise your enum will not be exposed to blueprint!
Any text in between the ‘/** */’ above any of the enum types will be converted into a description for that type, just like the description in blueprint:
To define a description for the whole type, simply put the same kind of comment above the UENUM(BlueprintType) declaration.
Go ahead and add a new struct below the #include "YourFile.generated.h" and below the ERarityType enum like so:
Once again, we can define descriptions using the ‘/** Your Description Here */’. An important note is to include the:
This is your default constructor and allows blueprint to make the Break Struct and Make Struct nodes like so:
It is also important to include:
Above each variable so that it can be used in blueprint!
The second constructer is not strictly necessary, but is good practice:
This allows for better use in C++ where we can then create an FEquipmentData like this:
Good practice is great idea whether you’ll ever use the type in C++ or not!
NOTE: If you wish to use a struct as a variable in another struct, ensure that the first struct is defined above the one that is trying to reference it (this also goes for enums).
Now, hit Run or Build Solution, and open up your project!
When you go to add a variable to any blueprint class you’ll see your structure name without the ‘F’ prefix and it should show your custom description as per your code!
And for your enum, the engine will show prefix ‘E’ in front of the variable type like so:
With our descriptions showing on each selected type for the enum:
There, you have setup a better practice for these datatypes in your development!
Say goodbye to blueprint enums and structs! Gone are the future headaches of not using C++ enums and structs!
1. Add a Object C++ class to your blueprint project
2. Install Visual Studio Community 2022
3. Generate Visual Studio Project Files
4. Open up your new C++ class header file (.h)
5. Delete the class definition, and under the #include "YourFile.generated.h" begin adding the enum and struct definitions of your dreams like so:
6. Build and open your project to have these new types just as accessible as their bad practice blueprint counterparts!