Change variable's class for child

Hey all!
I’m making a custom character class, as well as a custom character movement component class.
How do I replace the variable “CharacterMovement” (currently a UCharacterMovementComponent class) to my new custom char movement comp class?
Basically I wanna change the charmovementcomp class of my custom character to something else.
Thanks a lot!

In your custom character class constructor:


AYourCharacter::AYourCharacter(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer.SetDefaultSubobjectClass<YourCustomCharMovementCompTemplateClass>(ACharacter::CharacterMovementComponentName))
{
}

I believe you’ll also have to use GENERATED_UCLASS_BODY() macro in your class’ header file instead of GENERATED_BODY(), otherwise you won’t be able to use FObjectInitializer in your constructor to set a different class for subobjects.

Aight, so it ain’t workin!

HCharacter.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

include “GameFramework/Character.h”
include “HCharacter.generated.h”

UCLASS()

class CLEANHERO_API AHCharacter : public ACharacter
{
GENERATED_UCLASS_BODY()

public:
// Sets default values for this character’s properties
AHCharacter(const class FObjectInitializer& ObjectInitializer);

// 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;

};

HCharacter.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

include “CleanHero.h”
include “HCharacter.h”

AHCharacter::AHCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer.SetDefaultSubobjectClass<CustomMovementComponent>(ACharacter::CharacterMovementComponentName))
{
PrimaryActorTick.bCanEverTick = true;
}

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

}

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

}

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

}

Error:

Error C2065 ‘CustomMovementComponent’: undeclared identifier CleanHero C:\Users\Documents\Unreal Projects\Hero\Source\CleanHero\HCharacter.cpp 8

EDIT: I also added include “CustomMovementComponent.h” and added the dependency to the build file and that just created even more crazy errors :frowning:

It should be



ObjectInitializer.SetDefaultSubobjectClass<UCustomMovementComponent>