18 - Setting Up the Play States

在BatteryCollectorGameMode.h 中添加enum 表示游戲狀態 和視頻中有一些不同 需要設置類型uint8 否則編譯不過

UENUM(BlueprintType)
enum class EBatteryPlayState:uint8
{
EPlaying,
EGameOver,
EWon,
EUnknown
};

在BatteryCollectorGameMode類中添加變量CurrentState 以及相應的Get Set函數

private:

EBatteryPlayState CurrentState;

public:
UFUNCTION(BlueprintPure, Category = "Power")
EBatteryPlayState GetCurrentState() const;
UFUNCTION(BlueprintCallable, Category = "Power")
void SetCurrentState(EBatteryPlayState state);

編輯Mode類的Tick函數 根據當前的Power 進行邏輯判斷 設置相應的狀態
void ABatteryCollectorGameMode::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
if (MyCharacter)
{
if (MyCharacter->GetCurrentPower() > PowerToWin)
SetCurrentState(EBatteryPlayState::EWon);
else if (MyCharacter->GetCurrentPower() > 0)
{
MyCharacter->UpdatePower(-DeltaSecondsDecayRate(MyCharacter->GetInitialPower()));
}
else
{
SetCurrentState(EBatteryPlayState::EGameOver);
}
}
}

EBatteryPlayState ABatteryCollectorGameMode::GetCurrentState() const
{
return CurrentState;
}

void ABatteryCollectorGameMode::SetCurrentState(EBatteryPlayState state)
{
CurrentState = state;
}

編輯Widget 藍圖 添加文本框 并綁定問題內容 給句GameMode中的State輸出相應的文本

Paste_Image.png
Paste_Image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容