在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输出相应的文本