Download

Unreal confuses me - Many questions about includes and source files

I work with C++ sometimes and I have no problems with it. But Unreal is a real p* in the a* sometimes. VS doesn’t help either.

I need some guidance from the community here to find my way out of this mess that is Unreal

This is an example of my daily struggle. I have this code.
Anything compiles here. My class is a very basic Actor class and this is in the constructor.


   
    static ConstructorHelpers::FObjectFinder<UStaticMesh> particleMeshAux(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
    if (particleMeshAux.Object != nullptr)
        particleMesh = particleMeshAux.Object;
    else
        UE_LOG(ObiLog, Error, TEXT("Failed loading Sphere Static Mesh."));


**QUESTION 1) **Should I include StaticMesh.h?. Or should I include a higher .h that contains all the basic stuff? That’s something I always doubt about.

So I look in the docs and find the class page. Then I see the last line about the file to include:

[TABLE=“cellspacing: 0”]

Module
Engine

Header
Runtime/Engine/Classes/Engine/StaticMesh.h

I guess I don’t have to include Engine module… in the module list of the project. Right?

QUESTION 2) What do I have to include? some engine superior .h file? I’ve seen sometimes a engine.h include or something like that.
**QUESTION 3) **Why some people include just “StaticMesh.h”, sometimes “Engine/StaticMesh.h” or where should I start?

Then I put: #include “Engine/StaticMesh.h” . I have no idea if that’s the right way.

Then I see ConstructionHelpers is not detected. I find the official page:
http://api.unrealengine.com/INT/API/Runtime/CoreUObject/UObject/ConstructorHelpers/index.html
[TABLE=“cellspacing: 0”]

Module
CoreUObject

Header
Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h

Should I include CoreUObject somewhere in the project list of modules? I guess not… because it’s a Core … module?..

QUESTION 4) Again, should I include this header or not? or it comes with some superior .h file that I should Include?
QUESTION 5) how do I include this?. As before, “UObject/ConstructorHelpers.h” ?? “Runtime/ConstructorHelpers.h”?? “CoreUObject/ConstructorHelpers.h” or just “ConstructorHelpers.h”??

I just have no idea how this works. I read the documents about the build system, and everything that I got, and still don’t get this mess. Sometimes I include something directly and it works, sometimes I have no idea which of the path items I should include.

After this, I will definitely write a wiki page or article about this, I’ve been working with Unreal this past year and it’s been a really crazy experience (coming from Unity, Qt, and other C++ frameworks).

Thanks a lot.

Here we agreed internally to specify the whole path after “Runtime/…”;
But it’s an internal ‘political’ rule.
You can just specify path after any “Public/” directory and it should work fine.

It should be #include “UObject/ConstructorHelpers.h” then ?

What about the Module? How do I know if I need to include ir or not in the project?

Yes. that should work;

I usually only include a module to Build.cs if compiler says it can’t find the header… That usually means a Module is missing in Build.cs file.

As of 4.20 UE4 changed its requirements for include paths a little. Currently the change is only enforced for engine/plugin builds, but it makes sense to adopt the same approach in all cases. Any header you’re using should be at a path …/Public/Path/To/Header.h or …/Classes/Path/To/Header.h. You should use #include “Path/To/Header.h” - in other words, the path relative to the module’s Public or Classes folder. So yes, your guesses above are good.

As for including specific stuff or more general headers. Good C++ practice would say use the most specific header for what you need, that way you in theory minimize compile times and also reduce the risk of your code stopping compiling if Epic refactor something and remove an include directive from one of their headers. Practicality though says just find a reasonable balance. Lean towards headers for what you need, but don’t take it to the extreme otherwise you’ll end up with 100 include directives at the top of every file. I guess there’s one thing to avoid though - don’t rely on refactoring tools like VAX to add headers for you, they will tend to result in includes of files for totally unrelated things, just because they happen to indirectly include what you need.

You do always need the module references, but they’re added in the .Build.cs file rather than the .uproject file, and the common ones (Core, Engine, etc) are generally already there by default.

If you want to dig deeper, I wrote an article a while back on some reasons why UE4 includes can be confusing in how they behave: UE4 #includes, Precompiled Headers and IWYU (Include What You Use) — kantan

Thanks. I’ll think about all these!