If I want to create an enum for changing difficulty in a header file, It does no recognize the code and I get an error because of *.generated.h
Even if I generate the Unreal C++ project again, I still get the same error. However, if I build the project, it builds successfully, and I can run the game through Visual Studio.
I should mention that I created this header file in a folder named “Difficulty” and didn’t place it in the Public folder. In this case, when I enter the game, it doesn’t recognize the Difficulty enum.
If I use Unreal’s tool in Visual Studio, it creates both a header file and a C++ file.
What should I do to create an enum in a header file that is readable in Blueprints and avoid getting this error from Visual Studio?
an enum does not need to be in its own file at all, and realistically the only thing that matters about where you put a type class/struct/enum is visibility of includes and weight of include chains.
for example I have an EntityType enum that everything that deals with entities needs to know about, so I defined it in MyEntityComponent.h, because “if you know about what an entity is then you should know about what that type variable is”.
both the engine and Visual Studios Template tool will insert a cpp file even for structs, but I find any project that gets so granular to have each struct in its own file is being very… “eccentric”. though I have also been in code bases that have the “AllEnumsHere.h” which is like having the Scissors drawer such that anything resembling scissors or there functionality goes in the drawer
(I was able to “fix this” with ;would you put knives in the Scissors drawer because scissors are 2 knives and a hinge’.)
for the engine to recongize an enum regardless of where you put it:
UENUM(BlueprintType)
enum class EMyEnum : public uint8
{
none = 0, // sometimes the engine will let you get away with nothing having 'none'
Value1=1,
Value2=2,
}
for your given example I would say put it either in a place that everything that “needs to know what a difficulty is” is going to grab anyways, or in the core place it is more needed to be known.