Basic UE4 C++ question: #includes, modules, and lack of namespaces

(My C++ is very rusty in general, so sorry in advance if there’s a basic concept I got entirely wrong)

I know namespaces apparently don’t work in UE4. So does that mean I absolutely have to individually include every single header I’ll use in every single class I make?

Is there no way to create myself a “bundle” of #includes in order to replicate what namespaces do? Is there a way to tell every class of a certain module to include X headers?

Also, how do I know what goes in a privatePCH file, and what to include normally?

I remember reading once that you can put them into your MyProject.h since that is included in all generated files.
You can try that.

In C++ namespaces and #include are entirely unrelated. I’m guessing you maybe have a C# background, since I think in C# the two concepts are somewhat merged.

First off, you can use namespaces, they just don’t mix well with UE4’s reflection macros, which renders them usable only with code that doesn’t need to be directly exposed through the reflection system.

There’s nothing stopping you from creating a .h file which is nothing more than a collection of nested #includes of other files, but in UE4 you should probably just add the #includes to the pch instead. Strictly speaking it’s better practice to still add the required includes to every file that depends on them, even though just adding them to the pch would be sufficient. That said, it’s pretty hard to stick to that in practice given the size of the engine.

One thing you shouldn’t do is include a file which you will change frequently in the pch, since that will require you to rebuild the whole project every time it changes. So usually that means only engine and third party includes go in the pch, plus perhaps one or two core project headers that will rarely need altering.

You should take care when designing your classes. If you have a habit of including everything and the kitchen sink in the preprocessor of every class, you are going to develop circular dependencies eventually. By the time the error pops up you may find you’ve dug yourself a pretty deep hole, and you will have to spend a lot of time unwinding and disentangling classes to solve it. You will likely have a header that contains most of your classes that is included at the top level of your project, but including it in every class just to avoid having to write a few extra lines is a bad habit. Be selective about what you include.

Namespaces are about preventing name collisions generally between libraries. If your code base consists of Unreal and classes you write, and you aren’t rolling in a bunch of third party libraries, name collisions are not something to be concerned about.