六一的部落格


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



Match State


创建游戏状态

ShootThemUp: STUCoreTypes.h

1UENUM(BlueprintType)
2enum class ESTUMatchState : uint8
3{
4    WaitingToStart = 0,
5    InProgress,
6    Pause,
7    GameOver
8};

在STUGameModeBase加入游戏状态


添加数据成员和设置接口

private

ShootThemUp: STUGameModeBase.h

1ESTUMatchState MatchState = ESTUMatchState::WaitingToStart;
2
3void SetMatchState(ESTUMatchState NewState);

定义委托类型, 添加数据成员, 设置游戏状态时通知客户端

需传入新状态

ShootThemUp: STUCoreTypes.h

1DECLARE_MULTICAST_DELEGATE_OneParam(FOnMatchStateSignature, ESTUMatchState);

public

ShootThemUp: STUGameModeBase.h

1FOnMatchStateSignature OnMatchStateChanged;

ShootThemUp: STUGameModeBase.cpp

1void ASTUGameModeBase::SetMatchState(ESTUMatchState NewState)
2{
3    if (MatchState == NewState) return;
4
5    MatchState = NewState;
6    OnMatchStateChanged.Broadcast(NewState);
7}

设置游戏状态

  1. 开始时, 设置为 InProgress

    ShootThemUp: STUGameModeBase.cpp
    1// StartPlay
    2SetMatchState(ESTUMatchState::InProgress);
  2. 结束时, 设置为 GameOver
    1// GameOver
    2SetMatchState(ESTUMatchState::GameOver);

在STUGameHUD注册服务


实现处理函数, 输出新状态

private

ShootThemUp: UI/STUGameHUD.h

1#include "STUCoreTypes.h"
2
3void OnMatchStateChanged(ESTUMatchState NewState);

ShootThemUp: UI/STUGameHUD.cpp

1DEFINE_LOG_CATEGORY_STATIC(LogSTUGameHUD, All, All);
2
3void ASTUGameHUD::OnMatchStateChanged(ESTUMatchState NewState)
4{
5    UE_LOG(LogSTUGameHUD, Display, TEXT("Match state changed to %s"), *UEnum::GetValueAsString(NewState));
6}

在BeginPlay中注册服务

 1#include "STUGameModeBase.h"
 2
 3// BeginPlay
 4if (GetWorld())
 5{
 6    const auto GameMode = Cast<ASTUGameModeBase>(GetWorld()->GetAuthGameMode());
 7    if (GameMode)
 8    {
 9        GameMode->OnMatchStateChanged.AddUObject(this, &ASTUGameHUD::OnMatchStateChanged);
10    }
11}

查看

设置 BP_STUGameModeBase

-
Rounds Num 2
Round Time 3

游戏状态


Match State


创建游戏状态

ShootThemUp: STUCoreTypes.h

1UENUM(BlueprintType)
2enum class ESTUMatchState : uint8
3{
4    WaitingToStart = 0,
5    InProgress,
6    Pause,
7    GameOver
8};

在STUGameModeBase加入游戏状态


添加数据成员和设置接口

private

ShootThemUp: STUGameModeBase.h

1ESTUMatchState MatchState = ESTUMatchState::WaitingToStart;
2
3void SetMatchState(ESTUMatchState NewState);

定义委托类型, 添加数据成员, 设置游戏状态时通知客户端

需传入新状态

ShootThemUp: STUCoreTypes.h

1DECLARE_MULTICAST_DELEGATE_OneParam(FOnMatchStateSignature, ESTUMatchState);

public

ShootThemUp: STUGameModeBase.h

1FOnMatchStateSignature OnMatchStateChanged;

ShootThemUp: STUGameModeBase.cpp

1void ASTUGameModeBase::SetMatchState(ESTUMatchState NewState)
2{
3    if (MatchState == NewState) return;
4
5    MatchState = NewState;
6    OnMatchStateChanged.Broadcast(NewState);
7}

设置游戏状态

  1. 开始时, 设置为 InProgress

    ShootThemUp: STUGameModeBase.cpp
    1// StartPlay
    2SetMatchState(ESTUMatchState::InProgress);
  2. 结束时, 设置为 GameOver
    1// GameOver
    2SetMatchState(ESTUMatchState::GameOver);

在STUGameHUD注册服务


实现处理函数, 输出新状态

private

ShootThemUp: UI/STUGameHUD.h

1#include "STUCoreTypes.h"
2
3void OnMatchStateChanged(ESTUMatchState NewState);

ShootThemUp: UI/STUGameHUD.cpp

1DEFINE_LOG_CATEGORY_STATIC(LogSTUGameHUD, All, All);
2
3void ASTUGameHUD::OnMatchStateChanged(ESTUMatchState NewState)
4{
5    UE_LOG(LogSTUGameHUD, Display, TEXT("Match state changed to %s"), *UEnum::GetValueAsString(NewState));
6}

在BeginPlay中注册服务

 1#include "STUGameModeBase.h"
 2
 3// BeginPlay
 4if (GetWorld())
 5{
 6    const auto GameMode = Cast<ASTUGameModeBase>(GetWorld()->GetAuthGameMode());
 7    if (GameMode)
 8    {
 9        GameMode->OnMatchStateChanged.AddUObject(this, &ASTUGameHUD::OnMatchStateChanged);
10    }
11}

查看

设置 BP_STUGameModeBase

-
Rounds Num 2
Round Time 3