六一的部落格


关关难过关关过,前路漫漫亦灿灿。



NPC Spawn


说明

在GameModeBase中动态创建NPC


添加数据结构: 存放游戏规则

ShootThemUp: STUCoreTypes.h

-
玩家个数 PlayersNum 包含玩家操控的游戏角色和NPC
1// game mode
2USTRUCT(BlueprintType)
3struct FGameData
4{
5    GENERATED_USTRUCT_BODY()
6
7    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (ClampMin = "1", ClampMax = "100"))
8    int32 PlayersNum = 2;    
9};

本章会逐步补充游戏规则


API导航

-
AGameModeBase::StartPlay 在这里动态创建NPC
AGameModeBase::RestartPlayer 动态创建游戏角色
AGameModeBase::GetDefaultPawnClassForController RestartPlayer使用该函数返回的Pawn类型
FActorSpawnParameters 创建Actor时指定参数
ESpawnActorCollisionHandlingMethod 创建Actor时发生碰撞的处理方法

通过控制器创建NPC

  1. 游戏角色销毁时, 会调用其成员函数Destroy, 而控制器不会从场景中移除
  2. 控制器会负责游戏角色的再次动态创建

添加属性: 保存NPC类型

protected

ShootThemUp: STUGameModeBase.h

1#include "STUCoreTypes.h"
2
3class AAIController;
4
5UPROPERTY(EditDefaultsOnly)
6TSubclassOf<APawn> AIPawnClass;

覆写GetDefaultPawnClassForController, AIController创建AIPawnClass类型Pawn

public

ShootThemUp: STUGameModeBase.h

1virtual UClass* GetDefaultPawnClassForController_Implementation(AController* InController) override;

可以返回DefaultPawnClass, 或者调用基类函数

ShootThemUp: STUGameModeBase.cpp

1UClass* ASTUGameModeBase::GetDefaultPawnClassForController_Implementation(AController* InController)
2{
3    if (InController && InController->IsA<AAIController>())
4    {
5        return AIPawnClass;
6    }
7    return DefaultPawnClass;
8    // return Super::GetDefaultPawnClassForController_Implementation(InController);
9}

添加属性: 存放游戏规则

protected

ShootThemUp: STUGameModeBase.h

1#include "STUCoreTypes.h"
2
3UPROPERTY(EditDefaultsOnly)
4FGameData GameData;

添加属性: 保存控制器类型

protected

ShootThemUp: STUGameModeBase.h

1class AAIController;
2
3UPROPERTY(EditDefaultsOnly)
4TSubclassOf<AAIController> AIControllerClass;

添加接口: 动态创建控制器和NPC

private

ShootThemUp: STUGameModeBase.h

1void SpawnBots();

ShootThemUp: STUGameModeBase.cpp

 1#include "AIController.h"
 2
 3void ASTUGameModeBase::SpawnBots()
 4{
 5    if (!GetWorld()) return;
 6
 7    FActorSpawnParameters SpawnInfo;
 8    SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
 9    for (int32 i = 0; i < GameData.PlayersNum - 1; ++i)
10    {
11        const auto STUAIController = GetWorld()->SpawnActor<AAIController>(AIControllerClass, SpawnInfo);
12        RestartPlayer(STUAIController);
13    }
14}

调用SpawnBots

public

ShootThemUp: STUGameModeBase.h

1virtual void StartPlay() override;

ShootThemUp: STUGameModeBase.cpp

1void ASTUGameModeBase::StartPlay()
2{
3    Super::StartPlay();
4
5    SpawnBots();
6}

查看

  1. 设置BP_STUGameModeBase

    -
    AIPawn Class BP_STUAICharacter
    AIController Class BP_STUAIController
    Game Data > Players Num 4


  2. 删除场景中的AICharacter

  3. 在场景中添加Player Start, 一共4个, 供生成NPC和玩家控制的游戏角色使用

  4. 运行游戏, 生成3个AICharacter, 6个AIController


动态创建NPC


NPC Spawn


说明

在GameModeBase中动态创建NPC


添加数据结构: 存放游戏规则

ShootThemUp: STUCoreTypes.h

-
玩家个数 PlayersNum 包含玩家操控的游戏角色和NPC
1// game mode
2USTRUCT(BlueprintType)
3struct FGameData
4{
5    GENERATED_USTRUCT_BODY()
6
7    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (ClampMin = "1", ClampMax = "100"))
8    int32 PlayersNum = 2;    
9};

本章会逐步补充游戏规则


API导航

-
AGameModeBase::StartPlay 在这里动态创建NPC
AGameModeBase::RestartPlayer 动态创建游戏角色
AGameModeBase::GetDefaultPawnClassForController RestartPlayer使用该函数返回的Pawn类型
FActorSpawnParameters 创建Actor时指定参数
ESpawnActorCollisionHandlingMethod 创建Actor时发生碰撞的处理方法

通过控制器创建NPC

  1. 游戏角色销毁时, 会调用其成员函数Destroy, 而控制器不会从场景中移除
  2. 控制器会负责游戏角色的再次动态创建

添加属性: 保存NPC类型

protected

ShootThemUp: STUGameModeBase.h

1#include "STUCoreTypes.h"
2
3class AAIController;
4
5UPROPERTY(EditDefaultsOnly)
6TSubclassOf<APawn> AIPawnClass;

覆写GetDefaultPawnClassForController, AIController创建AIPawnClass类型Pawn

public

ShootThemUp: STUGameModeBase.h

1virtual UClass* GetDefaultPawnClassForController_Implementation(AController* InController) override;

可以返回DefaultPawnClass, 或者调用基类函数

ShootThemUp: STUGameModeBase.cpp

1UClass* ASTUGameModeBase::GetDefaultPawnClassForController_Implementation(AController* InController)
2{
3    if (InController && InController->IsA<AAIController>())
4    {
5        return AIPawnClass;
6    }
7    return DefaultPawnClass;
8    // return Super::GetDefaultPawnClassForController_Implementation(InController);
9}

添加属性: 存放游戏规则

protected

ShootThemUp: STUGameModeBase.h

1#include "STUCoreTypes.h"
2
3UPROPERTY(EditDefaultsOnly)
4FGameData GameData;

添加属性: 保存控制器类型

protected

ShootThemUp: STUGameModeBase.h

1class AAIController;
2
3UPROPERTY(EditDefaultsOnly)
4TSubclassOf<AAIController> AIControllerClass;

添加接口: 动态创建控制器和NPC

private

ShootThemUp: STUGameModeBase.h

1void SpawnBots();

ShootThemUp: STUGameModeBase.cpp

 1#include "AIController.h"
 2
 3void ASTUGameModeBase::SpawnBots()
 4{
 5    if (!GetWorld()) return;
 6
 7    FActorSpawnParameters SpawnInfo;
 8    SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
 9    for (int32 i = 0; i < GameData.PlayersNum - 1; ++i)
10    {
11        const auto STUAIController = GetWorld()->SpawnActor<AAIController>(AIControllerClass, SpawnInfo);
12        RestartPlayer(STUAIController);
13    }
14}

调用SpawnBots

public

ShootThemUp: STUGameModeBase.h

1virtual void StartPlay() override;

ShootThemUp: STUGameModeBase.cpp

1void ASTUGameModeBase::StartPlay()
2{
3    Super::StartPlay();
4
5    SpawnBots();
6}

查看

  1. 设置BP_STUGameModeBase

    -
    AIPawn Class BP_STUAICharacter
    AIController Class BP_STUAIController
    Game Data > Players Num 4


  2. 删除场景中的AICharacter

  3. 在场景中添加Player Start, 一共4个, 供生成NPC和玩家控制的游戏角色使用

  4. 运行游戏, 生成3个AICharacter, 6个AIController