跳到主内容
Documentation

2.动态创建actor.md

2025/12/16预计阅读 5 分钟

动态创建actor

步骤1: 加载class,不是object

引用后名加_C


static ConstructorHelpers::FClassFinder<AEnemyPathActorBase> PathActor(TEXT("/Script/Engine.Blueprint'/Game/BP/BP_Path_Test_Actor.BP_Path_Test_Actor_C'"));

步骤2: 获取默认class


if (PathActor.Succeeded())
	{
		const TSubclassOf<AEnemyPathActorBase> Clazz = PathActor.Class;
		PathComponent = Clazz.GetDefaultObject(); //创建默认对象
		if (PathComponent)
		{
			MyPrintfDebug("生成路径对象成功...");
			UE_LOG(LogTemp,Log,TEXT("路径:%s"),*UMyCoreTool::GetFVectorFormatString(PathComponent->GetActorLocation()));
		}
	}

在世界中创建actor

不要在构造函数里面生成….因为world还没有被创建

FClassFinder 不能再构造函数外使用



void AMyAIControllerTest::CreateComponent()
{
	static ConstructorHelpers::FClassFinder<AEnemyPathActorBase> PathActor(TEXT("/Script/Engine.Blueprint'/Game/BP/BP_Path_Test_Actor.BP_Path_Test_Actor_C'"));
	if (PathActor.Succeeded())
	{
		const TSubclassOf<AEnemyPathActorBase> Clazz = PathActor.Class;
		PathComponent = GetWorld()->SpawnActor<AEnemyPathActorBase>(Clazz);
		if (PathComponent)
		{
			PathComponent->SetActorLocation(FVector(2400.f, 960.f, 0));
			MyPrintfDebug("生成路径对象成功...");
			UE_LOG(LogTemp,Log,TEXT("actor路径:%s"),*UMyCoreTool::GetFVectorFormatString(PathComponent->GetActorLocation()));
		}
	}
}

示例

使用c++创建蓝图AEnemyPathActorBase


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

#pragma once

#include "CoreMinimal.h"
#include "Components/Path/EnemyPathComponentBase.h"
#include "Runtime/AIModule/Classes/AIController.h"
#include "MyEnemyAIControllerBase.generated.h"


/**
 * 怪物的AI控制器,不能直接使用
 */
UCLASS(ABstract)
class DEMO2_API AMyEnemyAIControllerBase : public AAIController
{
	GENERATED_BODY()

public:
	AMyEnemyAIControllerBase();

	/// 控制胖时调用
	virtual void OnPossess(APawn* InPawn) override;


	///设置样条线
	UPROPERTY(Blueprintable, EditAnywhere, BlueprintReadWrite,
		Category="DD Component",meta=(AllowPrivateAccess=true))
	AEnemyPathActorBase* PathComponent;


	UPROPERTY(DisplayName="当前移动的样条线下标")
	int CurrentSplinePointIndex;
	
protected:
	virtual void BeginPlay() override;

	UFUNCTION(Blueprintable, BlueprintCallable, DisplayName="开始移动", meta=(ShortTooltip="让怪物开始根据样条线移动"))
	void StartMovement();

	UFUNCTION(DisplayName="移动到下一个点")
	void MoveToNextSplinePoint();
	
	/// 移动成功后
	virtual void OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result) override;

public:
	virtual void Tick(float DeltaTime) override;

	UFUNCTION()
	int GetCurrentSplinePointIndex() const;
	
	UFUNCTION()
	int GetSplinePointCount() const ;

};



AMyAIControllerTest::AMyAIControllerTest()
{
	PrimaryActorTick.bCanEverTick = true;
	static ConstructorHelpers::FClassFinder<AEnemyPathActorBase> PathActor(TEXT("/Script/Engine.Blueprint'/Game/BP/BP_Path_Test_Actor.BP_Path_Test_Actor_C'"));
	if (PathActor.Succeeded())
	{
		const TSubclassOf<AEnemyPathActorBase> Clazz = PathActor.Class;
		PathComponent = Clazz.GetDefaultObject();
		if (PathComponent)
		{
			MyPrintfDebug("生成路径对象成功...");
			UE_LOG(LogTemp,Log,TEXT("路径:%s"),*UMyCoreTool::GetFVectorFormatString(PathComponent->GetActorLocation()));
		}
	}
}
返回顶部