現在ターン制のゲームを開発しており、今まではBPにて実装することが多かったのですが、C++での実装にチャレンジしています。
そこで以下の動画を参考にBPをC++に変換して試験的に実装しているのですが、ACharacterを継承したクラスにComponentを追加しようとしているところで躓いています。
具体的にはACharacterを継承したAUnitBaseを作成し、UActorComponentを継承したCombatComponentを作成しています。
それぞれのコードが以下になります。
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()
{
}
この実装で以下のようなBPになるような想定をしていたのですが(実際にはC++のクラスだと思うのであくまで想像上です)
コンパイルすると以下のようなエラーになってしまいます。
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.
普段は業務でPHPを使用しており、C++を勉強しながら進めているのですが、全く勝手が分からずトンチンカンな実装をしてしまっていると思いますので、教えていただけるとありがたいです。