I am continuing the Battery Collector tutorial. This is the 18th video. Click here for the first part and here for the previous one.
Setting Up the Play States
In BatteryCollectorGameMode.h, I am defining an enum to specify the different gameplay states:
// enum to store the current state of gameplay UENUM(BlueprintType) enum class EBatteryPlayState { EPlaying, EGameOver, EWon, EUnknown };
I am creating a private variable to keep track of the current playing state:
private: /** Keeps track of the current playing state */ EBatteryPlayState CurrentState;
In the public section, I am declaring getter and setter functions:
/** Returns the current playing state */ UFUNCTION(BlueprintPure, Category = "Power") EBatteryPlayState GetCurrentState() const; /** Returns the current playing state */ void SetCurrentState(EBatteryPlayState NewState);
I am defining these functions in BatteryCollectorGameMode.cpp:
EBatteryPlayState ABatteryCollectorGameMode::GetCurrentState() const { return CurrentState; } void ABatteryCollectorGameMode::SetCurrentState(EBatteryPlayState NewState) { CurrentState = NewState; }
In the same file, I am setting the current play state in the BeginPlay function:
void ABatteryCollectorGameMode::BeginPlay() { Super::BeginPlay(); SetCurrentState(EBatteryPlayState::EPlaying);
I am also modifying the Tick() function to set the play state when the power get greater than PowerTowin or negative:
void ABatteryCollectorGameMode::Tick(float DeltaTime) { Super::Tick(DeltaTime); // Check that we are using the battery collector character ABatteryCollectorCharacter* MyCharacter = Cast(UGameplayStatics::GetPlayerPawn(this, 0)); if (MyCharacter) { // If our power is greater than needed to win, set the game's state to Won if (MyCharacter->GetCurrentPower() > PowerToWin) { SetCurrentState(EBatteryPlayState::EWon); } // if the character's power is positive else if (MyCharacter->GetCurrentPower() > 0) { // decrease the chracter's power using the decay rate MyCharacter->UpdatePower(-DeltaTime*DecayRate*(MyCharacter->GetInitialPower())); } else { SetCurrentState(EBatteryPlayState::EGameOver); } } }
BatteryHUD
Back to Unreal Editor, I am compiling the project.
I am opening the BatteryHUD and I am adding a Text at the top of the Canvas Panel.
The value for PositionX is 0, PositionY is 50 and AlignmentX is 0,5. I am also selecting “Size to Content“.
Then I am creating a binding for the Text in the Content section of the Details Panel.
In the new blueprint, I am returning the text corresponding to the current state.
When I am playing, I can see the text:
Next Step
Next, I will toggle the Spawn Volumes
Leave a Reply