Creating Header-Only Files?

In most langues that use header and implementation files, I’ve often created header files to hold commonly used item. I might, for example, move structs, typedefs, and #defines out of my class and into a separate .h file included by the class. If an enum or struct is likely to be used by more than one class, I like to put it in a separate file.

I’ve had luck adding these types of things to [GameName].h, but every attempt I’ve made to add a new header file to my project causes problems with the build.

ShooterGame does it in a few spots, so I know it’s possible to use headers that aren’t part of a UCLASS() header/implementation pair, but there must be some incantation or magical sauce to doing it.

Does anyone have a step-by-step for adding header files to a project and then getting it to build?

Thanks!

You should be able to just add a new header file in Visual Studio.
And just use “Generate Visual Studio project files”.

And recompile your project that worked for me.
Also make sure to add the line under to the top of your header file.


#pragma once

Hope it helps.

I didn’t get it to work without putting any UCLASS(), USTRUCT() or UENUM() in it. Otherwise the .generated.h does not get generated and it does not get hooked automatically.

Hey there :slight_smile:

If you add the files from within visual studio via the “new class wizard” or the “add new item” wizard, the files will first be created within the Intermediate subfolder of your UE4 project. You will have to move the files from the intermediate folder to the proper desired location within the source directory of your UE4 project and the select “Generate Visual Studio project files” from the context menu of the .uproject file. If you just generate the visual studio project files without moving the files from the intermediate folder, they won’t be detected properly by the UHT or UBT. Personally, I usually manually add new source files directly from within the actual source file via the system “new file” option to save myself the hassle of remembering to relocate files, and then just regenerate the vs project files after adding.

As for the header-only problem, you only need to define the .generated.h includes for headers that define USTRUCT, UCLASS, etc types. If defining a UCLASS in a header file, you MUST include a companion .cpp file for the header. If the header only defines USTRUCT(s) then you do not need to include a .cpp for it (same goes for UENUM types).

Hope this helps.

9 Likes

Can you make a video tutorial about it…

2 Likes

Trying to figure out how to create an USTRUCT in a file separate header file (without being defined in other class header) manually (in Visual Studio) and I came . This solved my problem, somewhat. IntelliSense broke somehow after doing all the stuff, which is annoying.

One possible workaround is to use the class wizard in Unreal Editor to create a new class with no base class selected. With this we don’t need to move the files around and regenerate project file. Once the class is created, we can just remove all the functions we don’t need and make it a struct. Then add all the macros and include the headers an USTRUCT needs. IntelliSense should work just fine.

I don’t know why Epic has not added option to create struct in the class wizard.

Anyway your reply is still informative. Thank you.