How do I override a constructor?

I made a human class (Mensch) out of the pawn class, because I need some additional components. Then I made a child class from human, which is the player (Spieler). The player also needs additional components, the camera and the spring arm, because the human is getting a AI child, and there mustn’t be a camera. And now I don’t know, how I should write it, because I haven’t got much experience in Unreal Engine’s C++.

Mensch.h


// R

#pragma once

#include "GameFramework/Pawn.h"
#include "Mensch.generated.h"

UCLASS()
class UEBUNG_API AMensch : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	AMensch();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

	// Components
	UPROPERTY(EditAnywhere)
		USceneComponent* Root;
	UPROPERTY(EditAnywhere)
		UCapsuleComponent* Collision;
	UPROPERTY(EditAnywhere)
		USkeletalMeshComponent* Kopf;
	UPROPERTY(EditAnywhere)
		UStaticMeshComponent* Nase;
	UPROPERTY(EditAnywhere)
		UStaticMeshComponent* Ohren;
	UPROPERTY(EditAnywhere)
		USkeletalMeshComponent* Koerper;

	// Variablen
	UPROPERTY(EditAnywhere)
		float GMin;
	UPROPERTY(EditAnywhere)
		float GMax;
	UPROPERTY(EditAnywhere)
		float AMin;
	UPROPERTY(EditAnywhere)
		float AMax;
	float Gesundheit; 
	float Ausdauer;
	
	// Components platzieren
	FVector Null;
	FVector CollisionLocation; 
	UPROPERTY(EditAnywhere)
		FVector CollisionScale; 
};


Mensch.cpp


// R 

#include "Uebung.h"
#include "Mensch.h"


// Sets default values
AMensch::AMensch()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	FVector Null(0, 0, 0);
	FVector CollisionLocation(0, 0, 75);
	FVector CollisionScale(1, 1, 1.85);
	
	// Components
	Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
	RootComponent = Root;
	Root->SetRelativeLocation(Null);
	Collision = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Collision"));
	Collision->SetupAttachment(Root);
	Collision->SetRelativeLocation(CollisionLocation);
	Collision->SetRelativeScale3D(CollisionScale);
	Koerper = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Körper"));
	Koerper->SetupAttachment(Root);
	Koerper->SetRelativeLocation(Null);
	Kopf = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Kopf"));
	Kopf->SetupAttachment(Koerper);
	Kopf->SetRelativeLocation(Null);
	Nase = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Nase"));
	Nase->SetupAttachment(Kopf);
	Ohren = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Ohren"));
	Ohren->SetupAttachment(Kopf);

	// Gesundheit und Ausdauer festlegen
	float Gesundheit(FMath::FRandRange(GMin, GMax));
	float Ausdauer(FMath::FRandRange(AMin, AMax));

}

// Called when the game starts or when spawned
void AMensch::BeginPlay()
{
	Super::BeginPlay();

}

// Called every frame
void AMensch::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

// Called to bind functionality to input
void AMensch::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

}



Spieler.h


// R 

#pragma once

#include "Mensch.h"
#include "Spieler.generated.h"

/**
 * 
 */
UCLASS()
class UEBUNG_API ASpieler : public AMensch
{
	GENERATED_BODY()
	ASpieler(const FObjectInitializer& ObjectInitializer);
public:
	
	ASpieler();

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

	// Spieler-Komponenten
	UPROPERTY(EditAnywhere)
		USpringArmComponent* SpringArm;
	UPROPERTY(EditAnywhere)
		UCameraComponent* Camera;

};


Spieler.cpp


// R 

#include "Uebung.h"
#include "Spieler.h"

ASpieler::ASpieler(const FObjectInitializer& ObjectInitializer)
{
	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	SpringArm->SetupAttachment(Root);
	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(SpringArm);

}



Your code seems fine, are you running into a problem?

In your ASpieler class constructor, the AMensch constructor will automatically be called first and then your ASpieler constructor. If you want to move some code out of the constructor (for whatever reason), you can also override PostInitProperties or PostLoad and do whatever custom code you need there as well.



ASpieler::ASpieler(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	SpringArm = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("SpringArm"));
	SpringArm->SetupAttachment(Root);
	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(SpringArm);
}


Use that version of the constructor instead. I’ve run into endless issues trying to use parameter-less constructors in custom Actor / UObject derived classes. They just don’t seem to like it.

2 Likes