how does GENERATED_BODY() work?
what does it even expand to?
2024-04-09 try looking at what your IDE’s autocomplete suggests - from a cursory glance at the symbols available in a
UCLASS()-declared class, you will notice there are a few that are non-standard ones, such asStaticClass()orSuper. this is what theGENERATED_BODY()macro ends up expanding to after all the preprocessing is done.2024-04-09
the thing is - it doesn’t.
GENERATED_BODY()by itself is incredibly stupid:#define BODY_MACRO_COMBINE_INNER(A,B,C,D) A##B##C##D #define BODY_MACRO_COMBINE(A,B,C,D) BODY_MACRO_COMBINE_INNER(A,B,C,D) #define GENERATED_BODY(...) BODY_MACRO_COMBINE(CURRENT_FILE_ID,_,__LINE__,_GENERATED_BODY);let’s disassemble it piece by piece.
2024-04-09 so
GENERATED_BODYcombines the identifiersCURRENT_FILE_ID,_,__LINE__, and_GENERATED_BODY.2024-04-09 CURRENT_FILE_IDis a preprocessor macro defined by the UnrealBuildTool for each file. for simplicity’s sake, let’s assume it’s the filename with dots replaced by underscores. for instance,GameplayAbility_h.2024-04-09 the actual form it seems to take is
FID_{Path}with{Path}being the file path relative to the project root directory, with slashes and dots replaced with underscores. for:Engine/Source/Runtime/Engine/Classes/Engine/Blueprint.hthe file ID is:
FID_Engine_Source_Runtime_Engine_Classes_Engine_Blueprint_hI haven’t inspected the UnrealBuildTool/UnrealHeaderTool sources though, so there may be more to it.
2024-04-09
therefore for a simple file, let’s call it
MyClass.h:#pragma once #include "UObject/Object.h" #include "MyClass.generated.h" UCLASS() class UMyClass : public UObject { GENERATED_BODY() };after expanding the
GENERATED_BODY(), we’ll get this:// -- snip -- UCLASS() class UMyClass : public UObject { MyClass_h_10_GENERATED_BODY };2024-04-09