Hello, in learning to make better use of modules, I’ve noticed that when you add a class from the unreal editor, to a Non-Game Module (one you’ve manually added to your project) the generated header files doesn’t include a necessary header. The Engine.h header appears to be absolutely vital in class headers that are not within a game module. I presume this is because in loading the classes that are in the game module, Engine.H is already included, so when we use unreal to add a new class to the project, it presumes that including Engine.h is not necessary, when it may be.
Is this behavior to be expected, am I doing something wrong? Or should this be a bug to be fixed?
The reason we don’t include Engine.h in those generated headers is because we typically include Engine.h in the pre-compiled header (PCH) for each module, rather than in each file that needs it.
When you generate a new project, we automatically add that header to the PCH for your game module, however you’ll need to add it manually to the PCH of any other modules you create that need to use the Engine.
I’ve had to reproduce the sequence of events to ensure I have it right for your consideration, and you can tell me what I’m doing wrong, or can see why some people are having a difficult time with things the way they are.
I generate a module by making a plugin, moving the plugin’s module into my project, and deleting the plugin. Then I modify target, project build, and project file manually to make sure everything loads. Then I reload my project (building the new module), I add a class to the new module. I get an error about how I should be including the module’s header to classes instead of the PCH… so I may throw out the PCH, which only included the module header anyway, make all my cpp files include the module header file. Once that is done, every time I add a new class, I get an error about my header being modified and how Unreal can’t rebuild, so I restart, and I get a TONNE of esoteric errors about the internal workings of Unreal because none of my classes have a Engine.h include. My issues are effectively two fold. I don’t have #include Engine.h where I need it, and there is nothing indicating that I need it. I don’t have a PCH so now is crashes whenever I add a new class.
To be utterly frank, The problem is how new classes are generated. They aren’t generated with an include to the PCH, they have an include to the module header file instead. That ends up triggering an error, and if it’s your first class added, the error seems to suggest throwing out the PCH. On top of this, generating a class, especially one that extends UObject should always include the Engine.h file. there’s essentially no reason not to, such an include is under Pragma Once even. The only cost might be a very slight reduction in compilation speed, but you can tweak your includes manually to fix that. The automatic process should ensure that it doesn’t introduce a problem. I don’t feel like the present behavior is right, when it causes such issues.
Addendum: You’re also wrong. A game module doesn’t actually have a PCH by default, and classes you add to the game module through the unreal editor include a reference to the game module’s .h file, which includes engine.h by default. The purpose of putting this up was to draw attention to a fault in the automatic processes that are supposed to make this easier, but instead actually lead to more confusion.