📅  最后修改于: 2023-12-03 15:35:26.986000             🧑  作者: Mango
当我们创建多个相似的Actor时,我们希望能够复制一个Actor以便快速创建其他相似的Actor。在UE4中,我们可以使用C++代码来实现这一目标。
以下是实现复制Actor变量的步骤:
// MyBaseActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyBaseActor.generated.h"
UCLASS()
class MYPROJECT_API AMyBaseActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyBaseActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// MyBaseActor.cpp
#include "MyBaseActor.h"
// Sets default values
AMyBaseActor::AMyBaseActor()
{
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMyBaseActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyBaseActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// MyBaseActor.h
USTRUCT(BlueprintType)
struct FMyActorData
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, Category = "MyActorData")
FString Name;
UPROPERTY(BlueprintReadWrite, Category = "MyActorData")
float Health;
UPROPERTY(BlueprintReadWrite, Category = "MyActorData")
float MaxHealth;
};
// MyBaseActor.h
UCLASS()
class MYPROJECT_API AMyBaseActor : public AActor
{
GENERATED_BODY()
public:
// ...
UFUNCTION(BlueprintCallable, Category = "MyActor")
FMyActorData GetActorData() const;
// ...
};
// MyBaseActor.cpp
FMyActorData AMyBaseActor::GetActorData() const
{
FMyActorData ActorData;
ActorData.Name = GetName();
ActorData.Health = 100.f; // 修改以适合你的Actor
ActorData.MaxHealth = 100.f; // 修改以适合你的Actor
return ActorData;
}
// MyNewActor.h
#pragma once
#include "CoreMinimal.h"
#include "MyBaseActor.h"
#include "MyNewActor.generated.h"
UCLASS()
class MYPROJECT_API AMyNewActor : public AMyBaseActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyNewActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// ...
UFUNCTION(BlueprintCallable, Category = "MyActor")
void CopyActorData(const AMyBaseActor* Source);
// ...
protected:
UPROPERTY(BlueprintReadWrite, Category = "MyActorData")
FMyActorData ActorData;
};
// MyNewActor.cpp
#include "MyNewActor.h"
// Sets default values
AMyNewActor::AMyNewActor()
{
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMyNewActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyNewActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMyNewActor::CopyActorData(const AMyBaseActor* Source)
{
const FMyActorData& SourceData = Source->GetActorData();
ActorData.Name = SourceData.Name;
ActorData.Health = SourceData.Health;
ActorData.MaxHealth = SourceData.MaxHealth;
}
AMyBaseActor* BaseActor = GetWorld()->SpawnActor<AMyBaseActor>();
AMyNewActor* NewActor = GetWorld()->SpawnActor<AMyNewActor>();
NewActor->CopyActorData(BaseActor);
借助C++,我们可以快速地复制Actor的变量。这可以减少一些重复的设置工作,使我们的开发更加高效。