六一的部落格


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




AGameModeBase

关卡类


AGameModeBase::HUDClass

设置关卡使用HUD类



AGameModeBase::StartPlay

先于AGameModeBase::BeginPlay和所有AActor::BeginPlay之前调用

1/** Transitions to calls BeginPlay on actors. */
2UFUNCTION(BlueprintCallable, Category=Game)
3virtual void StartPlay();

AGameModeBase::DefaultPawnClass

在世界设置中或AGameModeBase构造函数中设置的默认Pawn类型

1/** The default pawn class used by players. */
2UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Classes)
3TSubclassOf<APawn> DefaultPawnClass;

动态创建游戏角色


便签

-
BlueprintNativeEvent C++实现在同名函数加上_Implementation后缀中

逻辑梳理

入口 调用函数1 调用函数2 调用函数3 调用函数4
第一层 AGameModeBase::RestartPlayer
第二层 AGameModeBase::RestartPlayer AGameModeBase::FindPlayerStart AGameModeBase::RestartPlayerAtPlayerStart
第三层 AGameModeBase::RestartPlayerAtPlayerStart AGameModeBase::GetDefaultPawnClassForController AGameModeBase::SpawnDefaultPawnFor AGameModeBase::InitStartSpot AGameModeBase::FinishRestartPlayer
第四层 AGameModeBase::SpawnDefaultPawnFor AGameModeBase::SpawnDefaultPawnAtTransform
第五层 AGameModeBase::SpawnDefaultPawnAtTransform AGameModeBase::GetDefaultPawnClassForController UWorld::SpawnActor
-
AGameModeBase::FindPlayerStart 寻找场景中的PlayerStart: 场景中必须存在PlayerStart
AGameModeBase::RestartPlayerAtPlayerStart 控制器没有Pawn则为其创建Pawn; 设置PlayerStart被占用
AGameModeBase::GetDefaultPawnClassForController 获取控制器默认Pawn类型
AGameModeBase::SpawnDefaultPawnFor 在PlayerStart指示地点创建Pawn
AGameModeBase::InitStartSpot 设置PlayerStart被占用
AGameModeBase::FinishRestartPlayer 更新控制器的旋转和Pawn
AGameModeBase::SpawnDefaultPawnAtTransform 提供变换信息, 使用控制器默认Pawn类型创建Pawn

AGameModeBase::RestartPlayer

1/** Returns true if it's valid to call RestartPlayer. By default will call Player->CanRestartPlayer */
2UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Game)
3bool PlayerCanRestart(APlayerController* Player);

通过控制器创建游戏角色

  1. 寻找场景中的PlayerStart
  2. 若Controller拥有Pawn, 无特别处理; 否则, 在PlayerStart指定位置创建Pawn

    Pawn类型由DefaultPawnClass决定
-
AGameModeBase::FindPlayerStart 寻找场景中的PlayerStart, 决定游戏角色变换信息
AGameModeBase::RestartPlayerAtPlayerStart 若Controller拥有Pawn, 无特别处理; 否则, 在PlayerStart指示位置创建Pawn
 1void AGameModeBase::RestartPlayer(AController* NewPlayer)
 2{
 3    if (NewPlayer == nullptr || NewPlayer->IsPendingKillPending())
 4    {
 5        return;
 6    }
 7
 8    AActor* StartSpot = FindPlayerStart(NewPlayer);
 9
10    // If a start spot wasn't found,
11    if (StartSpot == nullptr)
12    {
13        // Check for a previously assigned spot
14        if (NewPlayer->StartSpot != nullptr)
15        {
16            StartSpot = NewPlayer->StartSpot.Get();
17            UE_LOG(LogGameMode, Warning, TEXT("RestartPlayer: Player start not found, using last start spot"));
18        }	
19    }
20
21    RestartPlayerAtPlayerStart(NewPlayer, StartSpot);
22}

AGameModeBase::FindPlayerStart

BlueprintNativeEvent

寻找场景中的PlayerStart

 1/**
 2 * Return the specific player start actor that should be used for the next spawn
 3 * This will either use a previously saved startactor, or calls ChoosePlayerStart
 4 * 
 5 * @param Player The AController for whom we are choosing a Player Start
 6 * @param IncomingName Specifies the tag of a Player Start to use
 7 * @returns Actor chosen as player start (usually a PlayerStart)
 8 */
 9UFUNCTION(BlueprintNativeEvent, Category=Game)
10AActor* FindPlayerStart(AController* Player, const FString& IncomingName = TEXT(""));

AGameModeBase::RestartPlayerAtPlayerStart

1/** Tries to spawn the player's pawn at the specified actor's location */
2UFUNCTION(BlueprintCallable, Category=Game)
3virtual void RestartPlayerAtPlayerStart(AController* NewPlayer, AActor* StartSpot);
  1. 获取PlayerStart的旋转信息

  2. 检查Controller是否拥有Pawn, 有则使用现有Pawn, 无则尝试创建Pawn

    有Pawn, 控制器旋转信息来自当前Pawn; 否则来自PlayerStart

    -
    AGameModeBase::GetDefaultPawnClassForController 获取控制器默认的Pawn类型
    AGameModeBase::SpawnDefaultPawnFor 创建Pawn
     1if (NewPlayer->GetPawn() != nullptr)
     2{
     3    // If we have an existing pawn, just use it's rotation
     4    SpawnRotation = NewPlayer->GetPawn()->GetActorRotation();
     5}
     6else if (GetDefaultPawnClassForController(NewPlayer) != nullptr)
     7{
     8    // Try to create a pawn to use of the default class for this player
     9    APawn* NewPawn = SpawnDefaultPawnFor(NewPlayer, StartSpot);
    10    if (IsValid(NewPawn))
    11    {
    12        NewPlayer->SetPawn(NewPawn);
    13    }
    14}
  3. 设置PlayerStart被占用, 更新控制器的旋转和Pawn

    -
    AGameModeBase::InitStartSpot
    AGameModeBase::FinishRestartPlayer
     1if (!IsValid(NewPlayer->GetPawn()))
     2{
     3    FailedToRestartPlayer(NewPlayer);
     4}
     5else
     6{
     7    // Tell the start spot it was used
     8    InitStartSpot(StartSpot, NewPlayer);
     9
    10    FinishRestartPlayer(NewPlayer, SpawnRotation);
    11}

AGameModeBase::GetDefaultPawnClassForController

BlueprintNativeEvent

获取控制器默认的Pawn类型

1// Returns default pawn class for given controller
2UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Classes)
3UClass* GetDefaultPawnClassForController(AController* InController);
-
AGameModeBase::DefaultPawnClass 在世界设置中或AGameModeBase构造函数中设置的默认Pawn类型
 1UClass* AGameModeBase::GetDefaultPawnClassForController_Implementation(AController* InController)
 2{
 3#if WITH_EDITOR && DO_CHECK
 4    UClass* DefaultClass = DefaultPawnClass.DebugAccessRawClassPtr();
 5    if (DefaultClass)
 6    {
 7        if (FBlueprintSupport::IsClassPlaceholder(DefaultClass))
 8        {
 9            ensureMsgf(false, TEXT("Trying to spawn class that is, directly or indirectly, a placeholder"));
10            return ADefaultPawn::StaticClass();
11        }
12    }
13#endif
14    return DefaultPawnClass;
15}

AGameModeBase::SpawnDefaultPawnFor

BlueprintNativeEvent

在PlayerStart指示地点创建Pawn

1/**
2 * Called during RestartPlayer to actually spawn the player's pawn, when using a start spot
3 * @param	NewPlayer - Controller for whom this pawn is spawned
4 * @param	StartSpot - Actor at which to spawn pawn
5 * @return	a pawn of the default pawn class
6 */
7UFUNCTION(BlueprintNativeEvent, Category=Game)
8APawn* SpawnDefaultPawnFor(AController* NewPlayer, AActor* StartSpot);
-
AGameModeBase::SpawnDefaultPawnAtTransform 使用指定变换信息创建Pawn
 1APawn* AGameModeBase::SpawnDefaultPawnFor_Implementation(AController* NewPlayer, AActor* StartSpot)
 2{
 3    // Don't allow pawn to be spawned with any pitch or roll
 4    FRotator StartRotation(ForceInit);
 5    StartRotation.Yaw = StartSpot->GetActorRotation().Yaw;
 6    FVector StartLocation = StartSpot->GetActorLocation();
 7
 8    FTransform Transform = FTransform(StartRotation, StartLocation);
 9    return SpawnDefaultPawnAtTransform(NewPlayer, Transform);
10}

AGameModeBase::InitStartSpot

BlueprintNativeEvent

设置PlayerStart被占用

1/** Called from RestartPlayerAtPlayerStart, can be used to initialize the start spawn actor */
2UFUNCTION(BlueprintNativeEvent, Category=Game)
3void InitStartSpot(AActor* StartSpot, AController* NewPlayer);

AGameModeBase::FinishRestartPlayer

1/** Handles second half of RestartPlayer */
2virtual void FinishRestartPlayer(AController* NewPlayer, const FRotator& StartRotation);

对控制器调用Possess, 涉及OnNewPawn委托通知客户端

 1void AGameModeBase::FinishRestartPlayer(AController* NewPlayer, const FRotator& StartRotation)
 2{
 3    NewPlayer->Possess(NewPlayer->GetPawn());
 4
 5    // If the Pawn is destroyed as part of possession we have to abort
 6    if (!IsValid(NewPlayer->GetPawn()))
 7    {
 8        FailedToRestartPlayer(NewPlayer);
 9    }
10    else
11    {
12        // Set initial control rotation to starting rotation rotation
13        NewPlayer->ClientSetRotation(NewPlayer->GetPawn()->GetActorRotation(), true);
14
15        FRotator NewControllerRot = StartRotation;
16        NewControllerRot.Roll = 0.f;
17        NewPlayer->SetControlRotation(NewControllerRot);
18
19        SetPlayerDefaults(NewPlayer->GetPawn());
20
21        K2_OnRestartPlayer(NewPlayer);
22    }
23}

AGameModeBase::SpawnDefaultPawnAtTransform

BlueprintNativeEvent

提供变换信息, 使用控制器默认Pawn类型创建Pawn

1/**
2 * Called during RestartPlayer to actually spawn the player's pawn, when using a transform
3 * @param	NewPlayer - Controller for whom this pawn is spawned
4 * @param	SpawnTransform - Transform at which to spawn pawn
5 * @return	a pawn of the default pawn class
6 */
7UFUNCTION(BlueprintNativeEvent, Category=Game)
8APawn* SpawnDefaultPawnAtTransform(AController* NewPlayer, const FTransform& SpawnTransform);
-
AGameModeBase::GetDefaultPawnClassForController 获取控制器默认的Pawn类型
UWorld::SpawnActor 创建Pawn
FActorSpawnParameters 配置参数
 1APawn* AGameModeBase::SpawnDefaultPawnAtTransform_Implementation(AController* NewPlayer, const FTransform& SpawnTransform)
 2{
 3    FActorSpawnParameters SpawnInfo;
 4    SpawnInfo.Instigator = GetInstigator();
 5    SpawnInfo.ObjectFlags |= RF_Transient;	// We never want to save default player pawns into a map
 6    UClass* PawnClass = GetDefaultPawnClassForController(NewPlayer);
 7    APawn* ResultPawn = GetWorld()->SpawnActor<APawn>(PawnClass, SpawnTransform, SpawnInfo);
 8    if (!ResultPawn)
 9    {
10        UE_LOG(LogGameMode, Warning, TEXT("SpawnDefaultPawnAtTransform: Couldn't spawn Pawn of type %s at %s"), *GetNameSafe(PawnClass), *SpawnTransform.ToHumanReadableString());
11    }
12    return ResultPawn;
13}

GameModeBase类



AGameModeBase

关卡类


AGameModeBase::HUDClass

设置关卡使用HUD类



AGameModeBase::StartPlay

先于AGameModeBase::BeginPlay和所有AActor::BeginPlay之前调用

1/** Transitions to calls BeginPlay on actors. */
2UFUNCTION(BlueprintCallable, Category=Game)
3virtual void StartPlay();

AGameModeBase::DefaultPawnClass

在世界设置中或AGameModeBase构造函数中设置的默认Pawn类型

1/** The default pawn class used by players. */
2UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Classes)
3TSubclassOf<APawn> DefaultPawnClass;

动态创建游戏角色


便签

-
BlueprintNativeEvent C++实现在同名函数加上_Implementation后缀中

逻辑梳理

入口 调用函数1 调用函数2 调用函数3 调用函数4
第一层 AGameModeBase::RestartPlayer
第二层 AGameModeBase::RestartPlayer AGameModeBase::FindPlayerStart AGameModeBase::RestartPlayerAtPlayerStart
第三层 AGameModeBase::RestartPlayerAtPlayerStart AGameModeBase::GetDefaultPawnClassForController AGameModeBase::SpawnDefaultPawnFor AGameModeBase::InitStartSpot AGameModeBase::FinishRestartPlayer
第四层 AGameModeBase::SpawnDefaultPawnFor AGameModeBase::SpawnDefaultPawnAtTransform
第五层 AGameModeBase::SpawnDefaultPawnAtTransform AGameModeBase::GetDefaultPawnClassForController UWorld::SpawnActor
-
AGameModeBase::FindPlayerStart 寻找场景中的PlayerStart: 场景中必须存在PlayerStart
AGameModeBase::RestartPlayerAtPlayerStart 控制器没有Pawn则为其创建Pawn; 设置PlayerStart被占用
AGameModeBase::GetDefaultPawnClassForController 获取控制器默认Pawn类型
AGameModeBase::SpawnDefaultPawnFor 在PlayerStart指示地点创建Pawn
AGameModeBase::InitStartSpot 设置PlayerStart被占用
AGameModeBase::FinishRestartPlayer 更新控制器的旋转和Pawn
AGameModeBase::SpawnDefaultPawnAtTransform 提供变换信息, 使用控制器默认Pawn类型创建Pawn

AGameModeBase::RestartPlayer

1/** Returns true if it's valid to call RestartPlayer. By default will call Player->CanRestartPlayer */
2UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Game)
3bool PlayerCanRestart(APlayerController* Player);

通过控制器创建游戏角色

  1. 寻找场景中的PlayerStart
  2. 若Controller拥有Pawn, 无特别处理; 否则, 在PlayerStart指定位置创建Pawn

    Pawn类型由DefaultPawnClass决定
-
AGameModeBase::FindPlayerStart 寻找场景中的PlayerStart, 决定游戏角色变换信息
AGameModeBase::RestartPlayerAtPlayerStart 若Controller拥有Pawn, 无特别处理; 否则, 在PlayerStart指示位置创建Pawn
 1void AGameModeBase::RestartPlayer(AController* NewPlayer)
 2{
 3    if (NewPlayer == nullptr || NewPlayer->IsPendingKillPending())
 4    {
 5        return;
 6    }
 7
 8    AActor* StartSpot = FindPlayerStart(NewPlayer);
 9
10    // If a start spot wasn't found,
11    if (StartSpot == nullptr)
12    {
13        // Check for a previously assigned spot
14        if (NewPlayer->StartSpot != nullptr)
15        {
16            StartSpot = NewPlayer->StartSpot.Get();
17            UE_LOG(LogGameMode, Warning, TEXT("RestartPlayer: Player start not found, using last start spot"));
18        }	
19    }
20
21    RestartPlayerAtPlayerStart(NewPlayer, StartSpot);
22}

AGameModeBase::FindPlayerStart

BlueprintNativeEvent

寻找场景中的PlayerStart

 1/**
 2 * Return the specific player start actor that should be used for the next spawn
 3 * This will either use a previously saved startactor, or calls ChoosePlayerStart
 4 * 
 5 * @param Player The AController for whom we are choosing a Player Start
 6 * @param IncomingName Specifies the tag of a Player Start to use
 7 * @returns Actor chosen as player start (usually a PlayerStart)
 8 */
 9UFUNCTION(BlueprintNativeEvent, Category=Game)
10AActor* FindPlayerStart(AController* Player, const FString& IncomingName = TEXT(""));

AGameModeBase::RestartPlayerAtPlayerStart

1/** Tries to spawn the player's pawn at the specified actor's location */
2UFUNCTION(BlueprintCallable, Category=Game)
3virtual void RestartPlayerAtPlayerStart(AController* NewPlayer, AActor* StartSpot);
  1. 获取PlayerStart的旋转信息

  2. 检查Controller是否拥有Pawn, 有则使用现有Pawn, 无则尝试创建Pawn

    有Pawn, 控制器旋转信息来自当前Pawn; 否则来自PlayerStart

    -
    AGameModeBase::GetDefaultPawnClassForController 获取控制器默认的Pawn类型
    AGameModeBase::SpawnDefaultPawnFor 创建Pawn
     1if (NewPlayer->GetPawn() != nullptr)
     2{
     3    // If we have an existing pawn, just use it's rotation
     4    SpawnRotation = NewPlayer->GetPawn()->GetActorRotation();
     5}
     6else if (GetDefaultPawnClassForController(NewPlayer) != nullptr)
     7{
     8    // Try to create a pawn to use of the default class for this player
     9    APawn* NewPawn = SpawnDefaultPawnFor(NewPlayer, StartSpot);
    10    if (IsValid(NewPawn))
    11    {
    12        NewPlayer->SetPawn(NewPawn);
    13    }
    14}
  3. 设置PlayerStart被占用, 更新控制器的旋转和Pawn

    -
    AGameModeBase::InitStartSpot
    AGameModeBase::FinishRestartPlayer
     1if (!IsValid(NewPlayer->GetPawn()))
     2{
     3    FailedToRestartPlayer(NewPlayer);
     4}
     5else
     6{
     7    // Tell the start spot it was used
     8    InitStartSpot(StartSpot, NewPlayer);
     9
    10    FinishRestartPlayer(NewPlayer, SpawnRotation);
    11}

AGameModeBase::GetDefaultPawnClassForController

BlueprintNativeEvent

获取控制器默认的Pawn类型

1// Returns default pawn class for given controller
2UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Classes)
3UClass* GetDefaultPawnClassForController(AController* InController);
-
AGameModeBase::DefaultPawnClass 在世界设置中或AGameModeBase构造函数中设置的默认Pawn类型
 1UClass* AGameModeBase::GetDefaultPawnClassForController_Implementation(AController* InController)
 2{
 3#if WITH_EDITOR && DO_CHECK
 4    UClass* DefaultClass = DefaultPawnClass.DebugAccessRawClassPtr();
 5    if (DefaultClass)
 6    {
 7        if (FBlueprintSupport::IsClassPlaceholder(DefaultClass))
 8        {
 9            ensureMsgf(false, TEXT("Trying to spawn class that is, directly or indirectly, a placeholder"));
10            return ADefaultPawn::StaticClass();
11        }
12    }
13#endif
14    return DefaultPawnClass;
15}

AGameModeBase::SpawnDefaultPawnFor

BlueprintNativeEvent

在PlayerStart指示地点创建Pawn

1/**
2 * Called during RestartPlayer to actually spawn the player's pawn, when using a start spot
3 * @param	NewPlayer - Controller for whom this pawn is spawned
4 * @param	StartSpot - Actor at which to spawn pawn
5 * @return	a pawn of the default pawn class
6 */
7UFUNCTION(BlueprintNativeEvent, Category=Game)
8APawn* SpawnDefaultPawnFor(AController* NewPlayer, AActor* StartSpot);
-
AGameModeBase::SpawnDefaultPawnAtTransform 使用指定变换信息创建Pawn
 1APawn* AGameModeBase::SpawnDefaultPawnFor_Implementation(AController* NewPlayer, AActor* StartSpot)
 2{
 3    // Don't allow pawn to be spawned with any pitch or roll
 4    FRotator StartRotation(ForceInit);
 5    StartRotation.Yaw = StartSpot->GetActorRotation().Yaw;
 6    FVector StartLocation = StartSpot->GetActorLocation();
 7
 8    FTransform Transform = FTransform(StartRotation, StartLocation);
 9    return SpawnDefaultPawnAtTransform(NewPlayer, Transform);
10}

AGameModeBase::InitStartSpot

BlueprintNativeEvent

设置PlayerStart被占用

1/** Called from RestartPlayerAtPlayerStart, can be used to initialize the start spawn actor */
2UFUNCTION(BlueprintNativeEvent, Category=Game)
3void InitStartSpot(AActor* StartSpot, AController* NewPlayer);

AGameModeBase::FinishRestartPlayer

1/** Handles second half of RestartPlayer */
2virtual void FinishRestartPlayer(AController* NewPlayer, const FRotator& StartRotation);

对控制器调用Possess, 涉及OnNewPawn委托通知客户端

 1void AGameModeBase::FinishRestartPlayer(AController* NewPlayer, const FRotator& StartRotation)
 2{
 3    NewPlayer->Possess(NewPlayer->GetPawn());
 4
 5    // If the Pawn is destroyed as part of possession we have to abort
 6    if (!IsValid(NewPlayer->GetPawn()))
 7    {
 8        FailedToRestartPlayer(NewPlayer);
 9    }
10    else
11    {
12        // Set initial control rotation to starting rotation rotation
13        NewPlayer->ClientSetRotation(NewPlayer->GetPawn()->GetActorRotation(), true);
14
15        FRotator NewControllerRot = StartRotation;
16        NewControllerRot.Roll = 0.f;
17        NewPlayer->SetControlRotation(NewControllerRot);
18
19        SetPlayerDefaults(NewPlayer->GetPawn());
20
21        K2_OnRestartPlayer(NewPlayer);
22    }
23}

AGameModeBase::SpawnDefaultPawnAtTransform

BlueprintNativeEvent

提供变换信息, 使用控制器默认Pawn类型创建Pawn

1/**
2 * Called during RestartPlayer to actually spawn the player's pawn, when using a transform
3 * @param	NewPlayer - Controller for whom this pawn is spawned
4 * @param	SpawnTransform - Transform at which to spawn pawn
5 * @return	a pawn of the default pawn class
6 */
7UFUNCTION(BlueprintNativeEvent, Category=Game)
8APawn* SpawnDefaultPawnAtTransform(AController* NewPlayer, const FTransform& SpawnTransform);
-
AGameModeBase::GetDefaultPawnClassForController 获取控制器默认的Pawn类型
UWorld::SpawnActor 创建Pawn
FActorSpawnParameters 配置参数
 1APawn* AGameModeBase::SpawnDefaultPawnAtTransform_Implementation(AController* NewPlayer, const FTransform& SpawnTransform)
 2{
 3    FActorSpawnParameters SpawnInfo;
 4    SpawnInfo.Instigator = GetInstigator();
 5    SpawnInfo.ObjectFlags |= RF_Transient;	// We never want to save default player pawns into a map
 6    UClass* PawnClass = GetDefaultPawnClassForController(NewPlayer);
 7    APawn* ResultPawn = GetWorld()->SpawnActor<APawn>(PawnClass, SpawnTransform, SpawnInfo);
 8    if (!ResultPawn)
 9    {
10        UE_LOG(LogGameMode, Warning, TEXT("SpawnDefaultPawnAtTransform: Couldn't spawn Pawn of type %s at %s"), *GetNameSafe(PawnClass), *SpawnTransform.ToHumanReadableString());
11    }
12    return ResultPawn;
13}