How to implement/Ideas for Global UStructs/UEnums?

Hey guys,
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!
Thanks!

Hi MrCodyGaming

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:

Public/DataObjects/<project name>Enums.h

Public/DataObjects/<project name>Structs.h

Public/DataObjects/<project name>Delegates.h

Public/Libraries/<project name>SupportLibrary.h

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.

Alex

1 Like

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?

Hi MrCodyGaming

Glad you found my answer helpful. If you wouldn’t mind marking it as correct in case other people have similar questions :slight_smile:

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.

Good luck!

Alex

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!

Cody

Hi MrCodyGaming.

I would not give this a UObject parent as it’s unnecessary. I just tried it out.

  1. Create a new class of type none

  1. Open the CPP file in VS

  2. Remove the default constructor/destructor implementations

  3. If you added your header into a subdirectory, modify the include path to find the header. Mine is #include “DataObjects/GlobalStructs.h”

  4. Open the Header

  5. Remove the class declaration

  6. Add in the generated header include. My case its #include “GlobalStructs.generated.h”

  7. Add a struct. My example:

    // Fill out your copyright notice in the Description page of Project Settings.

    #pragma once

    #include “CoreMinimal.h”
    #include “GlobalStructs.generated.h”

    USTRUCT(Blueprintable)
    struct FTestStruct
    {
    GENERATED_BODY()
    public:
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    bool testBool;

     UPROPERTY(EditAnywhere, BlueprintReadWrite)
     	int testInt; 
    

    };

  8. Close VS

  9. Close UE4

  10. Delete Intermediate folder

  11. Reopen UE4

  12. File->Refresh Visual Studio Project

  13. Click “Compile”

  14. File->Open Visual Studio (Make sure the project loads)

  15. Open any blueprint (or create a new one)

  16. 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.

Greatly appreciated

Cody

Thanks for the help! Everything is now working properly!

Glad you got it sorted!

Good luck :slight_smile:

Alex