How to add a Component to Character in C++

We are currently developing a turn-based game, which until now has often been implemented in BP, but we are trying to implement it in C++.
I’m converting BP to C++ and implementing it on a trial basis with reference to the following video, but I am unable to add a component to a class that inherits ACharacter.

Specifically, I have created an AUnitBase that inherits from ACharacter and a CombatComponent that inherits from UActorComponent.
The code for each is shown below.

UnitBase.h

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

#pragma once

#include "CoreMinimal.h"
#include "CombatComponent.h"
#include "GameFramework/Character.h"
#include "UnitBase.generated.h"

UCLASS()
class SOULS_API AUnitBase : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AUnitBase();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

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

	UPROPERTY(VisibleAnywhere)
	UCombatComponent* CombatComponent;
};

UnitBase.cpp

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


#include "UnitBase.h"

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

}

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

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

}

// Called to bind functionality to input
void AUnitBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

CombatComponent.h

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

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "CombatComponent.generated.h"

class AUnitBase;

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class SOULS_API UCombatComponent : public UActorComponent
{
	GENERATED_BODY()

public:
	// Sets default values for this component's properties
	UCombatComponent();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

	UPROPERTY(EditAnywhere)
	AUnitBase* UnitCharacter;

	UPROPERTY(EditAnywhere)
	FTransform BattlePosition;

public:
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

	UFUNCTION(BlueprintCallable)
	void StartUnitTurn();
};

CombatComponent.cpp

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


#include "CombatComponent.h"

// Sets default values for this component's properties
UCombatComponent::UCombatComponent()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void UCombatComponent::BeginPlay()
{
	Super::BeginPlay();
	AActor* Owner = GetOwner();
	UnitCharacter = Cast<AUnitBase>(Owner);
	BattlePosition = UnitCharacter->GetActorTransform();
}


// Called every frame
void UCombatComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

void UCombatComponent::StartUnitTurn()
{

}

I had assumed that this implementation would result in the following BP (I think it is actually a C++ class, so it is only imaginary)
image

When I compile it, I get the following error.

Running C:\UE_5.0\Engine\Binaries\DotNET\UnrealBuildTool\UnrealBuildTool.exe -Target=“SoulsEditor Win64 Development -Project=”“C:/Users//Documents/Unreal Projects/Souls/Souls.uproject”“” -LiveCoding -LiveCodingModules=“C:/UE_5.0/Engine/Intermediate/LiveCodingModules.txt” -LiveCodingManifest=“C:/UE_5.0/Engine/Intermediate/LiveCoding.json” -WaitMutex -LiveCodingLimit=100
Log file: C:\Users\AppData\Local\UnrealBuildTool\Log.txt
Building SoulsEditor…
Using Visual Studio 2019 14.29.30146 toolchain (C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133) and Windows 10.0.18362.0 SDK (C:\Program Files (x86)\Windows Kits\10).
[Adaptive Build] Excluded from Souls unity file: CombatComponent.cpp, Souls.cpp, SoulsGameModeBase.cpp, UnitBase.cpp
Determining max actions to execute in parallel (8 physical cores, 16 logical cores)
Executing up to 8 processes, one per physical core
Building 9 actions with 8 processes…
[1/9] Compile SharedPCH.Engine.ShadowErrors.cpp
[2/9] Compile CombatComponent.cpp
C:\Users\Documents\Unreal Projects\Souls\Source\Souls\CombatComponent.cpp(23): error C2027: use of undefined type ‘AUnitBase’
C:\Users\Documents\Unreal Projects\Souls\Source\Souls\CombatComponent.h(9): note: see declaration of ‘AUnitBase’
[3/9] Compile Souls.cpp
[4/9] Compile SoulsGameModeBase.cpp
C:\Users\Documents\Unreal Projects\Souls\Source\Souls\SoulsGameModeBase.cpp(13): error C2065: ‘CombatComponent’: undeclared identifier
[5/9] Compile Souls.init.gen.cpp
[6/9] Compile CombatComponent.gen.cpp
[7/9] Compile UnitBase.cpp
[8/9] Compile UnitBase.gen.cpp
[9/9] Compile SoulsGameModeBase.gen.cpp
Build failed.

I usually use PHP in my work and I am learning C++ as I go along, but I think I have made a strange implementation because I don’t know how to use it at all, so I would appreciate it if you could help me.

theComponentClass*  myNewComponent = CreateDefaultSubObject<theComponentClass>(TEXT("name to be shown");

Wow, Thank you.

1 Like