在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輸出相應的文本