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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容