GENERATED_BODY vs GENERATED_UCLASS_BODY

I am learning C++ version of the FPS in UE following this: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

When I created a new class all the time there is GENERATED_BODY and the tutorial say I have to override the constructor and use AFPSGameMode(const FObjectInitializer& ObjectInitializer) but the compiler give me an error because I can’t redeclare the constructor.

The get around of this is use GENERATED_UCLASS_BODY and then my constructor have the correct parameters but I dont know if I am doing it right.

I am new in UE so I really want to learn what I should use.

4 Likes

Hello, ubi

Please note that with GENERATED_BODY(), a constructor is no longer necessary for each class (however, if you need a constructor, you can declare and define it as usual).
The other difference is that, in comparison to GENERATED_UCLASS_BODY(), GENERATED_BODY() doesn’t have the public specifier in it, so any class members that are declared after it are private by default (you can explicitly declare them as public).
In your situation, please make sure that you have your constructor declared and defined correctly:

//.h file
//...
	GENERATED_BODY()
	
public:
	AFPSGameMode(const class FObjectInitializer& ObjectInitializer);

// .cpp file
AFPSGameMode::AFPSGameMode(const class FObjectInitializer& ObjectInitializer)
{
       // constructor functionality
}

Hope this helped!

Have a great day!

5 Likes

I’m clear on the public scope issue, my question is not about that. Being new to UE4 myself, does this mean that we should never use GENERATED_UCLASS_BODY going forward? I found the “recommendation” that it not be used anymore in the 4.6 release notes, but I guess it’s not officially deprecated?

I’m experiencing the same behavior described in the OP’s question, because if I use GENERATED_BODY like in my code below, in the way you describe, I still get errors with my (truncated) code.

CODE

// .h
 #pragma once
 #include "GameFramework/Character.h"
 #include "UETestCharacter.generated.h"
 
 UCLASS(config=Game)
 class AUETestCharacter : public ACharacter
 {
     GENERATED_BODY()
 
 public:
      AUETestCharacter(const class FObjectInitializer& ObjectInitializer);
 }...
 
 // .cpp   
 #include "UETestCharacter.h"
 #include "UETestCharacter.h"
 #include "BatteryPickup.h"
 
 AUETestCharacter::AUETestCharacter(const class FObjectInitializer &ObjectInitializer)
 {
         // constructor functionality
 }...

ERRORS

Error	1	error code: OtherCompilationError (5)	C:\Users\\Documents\Unreal Projects\UETest\Intermediate\ProjectFiles\Error	UETest
Error	2	error MSB3073: The command ""C:\Program Files\Epic Games\4.8\Engine\Build\BatchFiles\Build.bat" UETestEditor Win64 Development "C:\Users\\Documents\Unreal Projects\UETest\UETest.uproject" -rocket" exited with code -1.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	38	5	UETest
	3	IntelliSense: no instance of constructor "AUETestCharacter::AUETestCharacter" matches the argument list	c:\Users\\Documents\Unreal Projects\UETest\Source\UETest\UETestCharacter.h	9	2	UETest

Many thanks!

Never mind, it works now. I actually had a syntax error.

This very very helped, I couldn’t understand errors that I had, and difference between GENERATED_BODY and GENERATED_UCLASS_BODY regarding constructor “(const class FObjectInitializer& ObjectInitializer)” helped me a lot. It’s my third day learning UE4. Thank you!

2 Likes