While trying to structure and sort my code files and includes, I encountered a problem I don’t know how to fix.
I made an example Project, so the files will be small.
The class MyActor can spawn Objects form type “ObjectA” and ObjectA contains the structure “FObjectAData” to hold some data.
The file “TestProject.h” is unedited.
MyActor.cpp
#include "TestProject.h"
#include "MyActor.h"
#include "ObjectA.h"
AMyActor::AMyActor()
{
PrimaryActorTick.bCanEverTick = true;
}
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
void AMyActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AMyActor::MakeObjectA(FObjectAData data)
{
// code to spawn an Actor from type ObjectA
}
(we can assume that the code for spawning is working)
MyActor.h
#pragma once
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
class AObjectA;
struct FObjectAData;
UCLASS()
class TESTPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
AMyActor();
virtual void BeginPlay() override;
virtual void Tick( float DeltaSeconds ) override;
UFUNCTION(BlueprintCallable, Category = "myActor")
void MakeObjectA(FObjectAData data);
};
ObjectA.cpp
#include "TestProject.h"
#include "ObjectA.h"
#include "MyActor.h"
AObjectA::AObjectA()
{
PrimaryActorTick.bCanEverTick = true;
this->actorRef = nullptr;
}
void AObjectA::BeginPlay()
{
Super::BeginPlay();
}
void AObjectA::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AObjectA::SetActorRef(AMyActor *inActorRef)
{
this->actorRef = inActorRef;
}
ObjectA.h
#pragma once
#include "GameFramework/Actor.h"
#include "ObjectA.generated.h"
class AMyActor;
USTRUCT(BlueprintType)
struct FObjectAData
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, Category = "object A")
float data1;
UPROPERTY(BlueprintReadWrite, Category = "object A")
float data2;
UPROPERTY(BlueprintReadWrite, Category = "object A")
float data3;
};
UCLASS()
class TESTPROJECT_API AObjectA : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AObjectA();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "object A")
AMyActor *actorRef;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "object A")
FObjectAData data;
UFUNCTION(BlueprintCallable, Category = "object A")
void SetActorRef(AMyActor *inActorRef);
};
When I want to compile I get this error:
Basically both classes need to know each other. So I made them visible with forward declerations. I don’t want to include files itself in the Header.