Is there a 4.22 C++ guide best practices somewhere?

Hi all. I’m going through a tutorial up on Pluralsight’s website. It’s C++ and Blueprints. The video series was done a couple years ago. Even when I follow his instructions step by step I’m getting lots of errors. I think it’s probably a combination of him using an older version of both UE4 and older VS. I’m running UE4.22 and VS2019.

Some of the problems I’m experiencing is the compiler not finding header files with Paper2D. I had to manually go enter in the path to the header file. But, even after that, I got compile errors with .generated. files.

I’m extremely new to UE development, however, I’ve been writing software for many-a-moon. :). Having said that, my primary language has been C# and my C++ is pretty crusty. But hey. I’ll dust it off.

So, yeah, I was hoping to find out if there is a doc that really shows the best practices for setting up VS and C++ to help rid of compile errors dealing with paths and what not.

Thanks!!!

I am in the process of writing a decent guide for setting up QT Creator for the Engine in Linux. But the basic methodology is invariant of the platform. What you have to recognize is existence two very important files

  1. UnrealBuildTool (present in Engine/Binaries/DotNet/)
  2. UnrealHeaderTool (present in Engine/Binaries/WhateverPlatformYouAreUsing/)

These files are the essence of any Unreal Engine project, beginning from generating IDE specific files and compiling/debugging the code on platform. For their commandline parameters, read the platform specific scripts (or look into the source). Good luck!

When you’re done with that, you can write one for Windows/Visual Studio. :smiley:

Let us just say that writing about proprietary software is not my “cup of coffee”!

The “.generated.h” file needs to be the last include file, anything else you include should happen before that. I’m not sure what other help I can give without seeing the build errors.

Why is so? And what is the utility of “.generated.h” files?

Open one up and look (after you compile a class successfully). It’s all the Blueprint / Replication / UProperty / UFunction glue that is generated by the UnrealHeaderTool.

Could you be more specific about the glue part? For instance the Party.generated.h looks like


// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
/*===========================================================================
    Generated code exported from UnrealHeaderTool.
    DO NOT modify this manually! Edit the corresponding .h files instead!
===========================================================================*/

#include "UObject/ObjectMacros.h"
#include "UObject/ScriptMacros.h"

PRAGMA_DISABLE_DEPRECATION_WARNINGS
#ifdef UNREALTOURNAMENT_Party_generated_h
#error "Party.generated.h already included, missing '#pragma once' in Party.h"
#endif
#define UNREALTOURNAMENT_Party_generated_h

#define UnrealTournament_Source_UnrealTournament_Public_Online_Party_h_116_RPC_WRAPPERS
#define UnrealTournament_Source_UnrealTournament_Public_Online_Party_h_116_RPC_WRAPPERS_NO_PURE_DECLS
#define UnrealTournament_Source_UnrealTournament_Public_Online_Party_h_116_INCLASS_NO_PURE_DECLS \
private: \
    static void StaticRegisterNativesUParty(); \
    friend struct Z_Construct_UClass_UParty_Statics; \
public: \
    DECLARE_CLASS(UParty, UObject, COMPILED_IN_FLAGS(0 | CLASS_Config), CASTCLASS_None, TEXT("/Script/UnrealTournament"), NO_API) \
    DECLARE_SERIALIZER(UParty) \
    static const TCHAR* StaticConfigName() {return TEXT("Game");}
 ....

PRAGMA_ENABLE_DEPRECATION_WARNINGS

In other words, what new structure is provided by this file that cannot be achieved by traditional C++ compilation process (which don’t involve .generated.h). On the local scale I understand that it supports the error spitting mechanism and all that, but what exactly does it stand for on global scale?

I don’t know the specifics but I do know that I’ve been watching video tutorials and they have said the same thing. Nothing below the .generated. file.

There’s more in there for more complicated things, like Replicated variables or Blueprint callable functions. For an empty class, it should basically be nothing except reflection info.

Ok, I should take that for face value I guess. Things get even more weird. Has anyone noticed the thing about white spaces? For instance the practice of writing



#pragma once

#include "UTWeaponAttachment.h"

#include "UTWeapAttachment_ImpactHammer.generated.h"



is good, but the same code, without spaces as follows



#pragma once

#include "UTWeaponAttachment.h"
#include "UTWeapAttachment_ImpactHammer.generated.h"


is spitting out errors. Testing with QT Creator.

Can you share the errors? Because code-wise that should be the same, however know that UE4 has a feature called Unity Build (not to be confused with Unity engine) that combines source files before compiling. That reduces compilation times for larger projects, but it there are two things to be aware of:

  • You never know which files are going to be combined before compiling, small changes (such as a space) may cause files to be combined in a different way
  • The files being combined will often mask the fact that some of your code is missing includes.

For example if a source file of yours is using the class MyClass and it’s not being included, that’s an error in your code right? But unity build may be combining that file with another file that does include MyClass.h and your project will build fine, unless some small changes causes files to be combined in a different way and the error surfaces.

That’s the only explanation that I can think of for why a newline is the difference between error and no error for you.

Hi Zhi, thanks for the interest. I will post the error in the next cycle of building process (the project is too global and the error spits are overwhelming!).

Ok equivalent problem. In UTGameState.h the following directives are fine



#pragma once

#include "UTPlayerState.h"
#include "UTCharacter.h"
#include "ChartCreation.h"

#include "UTGameState.generated.h"


but when I include



#include "UTLineUpZone.h"


QT Creator spits the error in UTGameState.cpp



incomplete type 'AUTGameState'  in nested name specifier


in front of all the member functions defined.
I tried all possible permutations in the list but of no avail. Curiously it doesn’t even matter if I comment out the last directive, it is as if it is counting the number of lines.

Finally the code



#pragma once

#include "UTPlayerState.h"
#include "UTCharacter.h"
#include "ChartCreation.h"
#include "UTLineUpZone.h"
#include "UTGameState.generated.h"


without white space in the last pair works.

Few things that may help:

  1. In the .h files you usually don’t need to have other header includes. If you’re using other classes you can use ‘class’ in front to forward declare it.
    Ex:


UPROPERTY() class AUTGameState* GameState;


That will compile fine, and will only need the header when used in the .cpp file, not in the .h file. This may clear up issues with things referencing each other in headers which is what it seems like that ‘nested’ error is.

  1. If you ever try using something like FTableRowBase and notice bogus linkage or ‘not found’ errors, google it and the page will show the following:
  • Which header to include in the .cpp
  • Which module name to add to your publicdependencies in ProjectName.Build.cs
  1. Cached problems! Somethings things just don’t work even though they should. Usually worth doing a full rebuild of the project, or deleting the intermediate folder, regenerating project files (right click the .uproject file) and rebuilding the game.
    This could fix odd compiler/game errors that just don’t make sense (possibly the spacing errors).

But that occurs in the .cpp file.

Maybe I wasn’t clear enough. The error is detected by QT Creator as per the presence/absence of certain white spaces. That is weird and mysterious.

Just a sound reference for the utility of “.generated.h” files the way I wanted What do, GENERATED_BODY() and GENERATED_UCLASS_BODY(), do? - Programming & Scripting - Epic Developer Community Forums