本文主要是介绍UE5/C++ 基于GAS的怪物AI 6.3.2 创建怪物的生成点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.
打开UE引擎,创建TargetPoint C++类,命名为MMOARPGAISpawnPoint
打开MMOARPGAISpawnPoint.h
#pragma once#include "CoreMinimal.h"
#include "Engine/TargetPoint.h"
#include "MMOARPGAISpawnPoint.generated.h"/*** */
UCLASS()
class MMOARPGGAME_API AMMOARPGAISpawnPoint : public ATargetPoint
{GENERATED_BODY()
public:UPROPERTY(EditAnywhere, Category = "AISpawn")int32 CharacterID;//角色IDUPROPERTY(EditAnywhere, Category = "AISpawn")int32 Lv;//等级UPROPERTY(EditAnywhere, Category = "AISpawn")bool bScopeSpawnAI;//是否范围性生成UPROPERTY(EditAnywhere, Category = "AISpawn", meta = (EditCondition = "bScopeSpawnAI"))float SpawnAIRadius;//生成半径UPROPERTY(EditAnywhere, Category = "AISpawn", meta = (EditCondition = "bScopeSpawnAI"))int32 SpawnAINumberInRange;//范围内怪物生成数量
public:AMMOARPGAISpawnPoint();virtual void BeginPlay() override;virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);virtual void Tick(float DeltaTime) override;protected:void SpawnAICharacter(int32 CharacterID,int32 InLV);
};
进行实现
// Fill out your copyright notice in the Description page of Project Settings.#include "MMOARPGAISpawnPoint.h"
#include "../Character/Core/MMOARPGCharacterBase.h"
#include "../MMOARPGGameState.h"AMMOARPGAISpawnPoint::AMMOARPGAISpawnPoint()
{CharacterID = INDEX_NONE;Lv = 1;bScopeSpawnAI = false;SpawnAIRadius = 500.f;SpawnAINumberInRange = 3;
}void AMMOARPGAISpawnPoint::BeginPlay()
{Super::BeginPlay();if (GetWorld() && GetWorld()->IsServer()){//在BeginPlay中生成SpawnAICharacter(CharacterID, Lv);}
}void AMMOARPGAISpawnPoint::EndPlay(const EEndPlayReason::Type EndPlayReason)
{Super::EndPlay(EndPlayReason);
}void AMMOARPGAISpawnPoint::Tick(float DeltaTime)
{Super::Tick(DeltaTime);
}void AMMOARPGAISpawnPoint::SpawnAICharacter(int32 InCharacterID, int32 InLV)
{if (GetWorld()){if (AMMOARPGGameState* InGameState = GetWorld()->GetGameState<AMMOARPGGameState>()){//通过角色ID获取角色属性数据表if (FCharacterAttributeTable* InAttributeTable = InGameState->GetCharacterAttributeTable(InCharacterID)){//通过角色ID获取角色类数据表if (FCharacterStyleTable* InCharacterTable = InGameState->GetCharacterStyleTable(InCharacterID)){TArray<FVector>Locations;if (bScopeSpawnAI)//是否范围生成怪物{for (int32 i=0;i<SpawnAINumberInRange;i++){FVector Origin = GetActorLocation();FVector2D Point = FMath::RandPointInCircle(SpawnAIRadius);//在范围内随机生成一个点,用于怪物随机生成Locations.Add(Origin + FVector(Point.X, Point.Y, 0.f));}}else{Locations.Add(GetActorLocation());}//遍历生成位置for (auto& Location : Locations){if (AMMOARPGCharacterBase* InCharacterBase = GetWorld()->SpawnActor<AMMOARPGCharacterBase>(InCharacterTable->MMOARPGChearacterClass,//要生成的角色类Location,//生成位置FRotator::ZeroRotator))//旋转{}}}}}}
}
2.
打开UE引擎,在文件夹中找到生成点
然后将其拖拽到场景,进行配置
这篇关于UE5/C++ 基于GAS的怪物AI 6.3.2 创建怪物的生成点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!