📜  ue4 c++ 复制actor变量 - C++ (1)

📅  最后修改于: 2023-12-03 15:35:26.986000             🧑  作者: Mango

UE4 C++ 复制Actor变量

当我们创建多个相似的Actor时,我们希望能够复制一个Actor以便快速创建其他相似的Actor。在UE4中,我们可以使用C++代码来实现这一目标。

步骤

以下是实现复制Actor变量的步骤:

  1. 首先,在你的工程中创建一个基类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);

}
  1. 然后,我们创建一个包含所有需要复制的变量的结构体。
// 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;
};
  1. 在基类Actor中添加一个返回MyActorData的函数。
// 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;
}
  1. 接下来,我们创建一个继承基类Actor的新的Actor,称之为MyNewActor。
// 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;
}
  1. 现在,我们可以从基类Actor创建一个Actor实例,然后调用CopyActorData将数据复制到新的Actor中。
AMyBaseActor* BaseActor = GetWorld()->SpawnActor<AMyBaseActor>();
AMyNewActor* NewActor = GetWorld()->SpawnActor<AMyNewActor>();

NewActor->CopyActorData(BaseActor);
结论

借助C++,我们可以快速地复制Actor的变量。这可以减少一些重复的设置工作,使我们的开发更加高效。