Just got a question as I’m still pretty new to Unreal C++ and I’m just wondering how you guys handle UStructs that you need to call globally.
Do you guys just put them all into one header file and then call that header file wherever you need it and if so do you guys also do the same with ENUMS?
Also, do you guys use a certain class like a UObject, or blueprint function libraries to do this?
Thoughts would be appreciated!
There is no right or wrong way to handle this. Developers will have different preferences depending on their project requirements.
One option would be to store them in the project’s header file, although I am not a fan of this solution as I like to keep the header of the project fairly light.
Another option, as you mentioned is to keep them in separate header files. This is the approach I usually go for. I have a library imported as a plugin that I use for common structs and enums which are useful across multiple projects. Or I create an empty header file for both global structs and enums which are project-specific.
A third option is to keep the structs and enums defined close to the data they represent. For example, if it’s an enum used to represent different navigation types, then it makes sense to declare it somewhere near your pawn and then just include the appropriate header.
One piece of advice I will give is to keep your implementations consistent. There is nothing worse than working with a project where some enums/structs are declared in a separate header, some in classes and some in the project base. It becomes a nightmare to find data.
I wouldn’t include them in blueprint function libraries as that’s not the purpose of that class.
I often set a project like this:
Again, it’s really down to the project and the design choice of the programmer. As long the implementation makes sense and you’re not including unnecessary header data where it’s not needed then you’re good to go.
Hope this helps.
Thanks for the advice that’s what I was doing so I just wanted to make sure!
Hey, I’m now getting an issue with my Header files not getting the generated include.
I have them in the source file within the public folder yet when I generate project files I get nothing. Wondering if I’m missing a step in this process? They do have the UENUM, USTRUCT so shouldn’t the header tool pick up on that?
Glad you found my answer helpful. If you wouldn’t mind marking it as correct in case other people have similar questions
If you created an empty class (NONE) in the Unreal Editor, you will need to manually add the .generated.h yourself. Close visual studio and restart the editor. Once open, go to File->Generate Visual Studio Files.
If the generated header still isn’t there, delete the “Intermediate” directory inside your project and then restart. Unreal should re-serialise all classes again.
So that doesnt seem to work either. Don’t know whats up with my program lol.
Would it be bad practice to make them with a UObject Parent? That seems to be the only way I can get it to work right and I don’t know why.
Feel like this unreal project is broken lol. Now other files are missing their generated headers…
I will give this a shot in a new project and see how it handles it.
Thanks for the quick replies!
I would not give this a UObject parent as it’s unnecessary. I just tried it out.
- Create a new class of type none
Open the CPP file in VS
Remove the default constructor/destructor implementations
If you added your header into a subdirectory, modify the include path to find the header. Mine is #include “DataObjects/GlobalStructs.h”
Open the Header
Remove the class declaration
Add in the generated header include. My case its #include “GlobalStructs.generated.h”
Add a struct. My example:
// Fill out your copyright notice in the Description page of Project Settings.
Delete Intermediate folder
File->Refresh Visual Studio Project
File->Open Visual Studio (Make sure the project loads)
Open any blueprint (or create a new one)
Create a variable with your struct type.
To use in C++, just include the structs header.
Hope you get this resolved.
Thanks for the awesome response! I will try this when I get home but from reading your guide here I think I know what I did wrong.
Thanks for the help! Everything is now working properly!