Ahri and Bear Game Mode: Game Overs - Trevor802/AhriAndBear GitHub Wiki
Game Over states are managed primarily by the AAhriAndBearGameModeBase
class.
EGameOverReason
Enum
The The EGameOverReason
enum defines the following values:
C++ | Blueprints | Purpose |
---|---|---|
GO_AnimalCaught |
Animal Was Caught | Used when an animal is caught. |
GO_AnimalsDead |
Animals Died | When both animals' hunger and thirst stats have been zeroed out. |
GO_Victory |
Can be used for win conditions. | Not currently used. |
If you want to add a game over reason, you can add it to this enum. Please update this table afterwards, if possible.
Defining a Game Over Condition
You can define your own game over state two ways. Both methods require calling the public EndGame
method, which takes a EGameOverReason reason
as its argument. This reason
argument is propagated to the FGameOver
delegate down the line.
EndGame
from the Game Mode
Method 1: Invoking This is currently how game overs (losses) are invoked. To do this, the game mode finds the actor that can create the game over state and adds a listener to a delegate that is fired when the game over state occurs.
For example, when an animal is caught, it's game over. To have the game manager listen for this condition, the following code is used:
void AAhriAndBearGameModeBase::BeginPlay()
{
...
AActor* dogActor = UGameplayStatics::GetActorOfClass(GetWorld(), AABDogCharacter::StaticClass());
dog = Cast<AABDogCharacter>(dogActor);
if (dog != nullptr) {
...
dog->OnAnimalCaught.AddDynamic(this, &AAhriAndBearGameModeBase::OnAnimalCaught);
}
...
}
First, we find Bear and make sure that the reference is valid. Once done, we hook into Bear's OnAnimalCaught
dynamic delegate and tell it to invoke the Game Mode's OnAnimalCaught
method when the animal is caught. The AAhriAndBearGameModeBase::OnAnimalCaught
's definition is
void AAhriAndBearGameModeBase::OnAnimalCaught(AActor* captor) {
EndGame(EGameOverReason::GO_AnimalCaught);
}
which will put the Game Mode into a game over state.
EndGame
from Another Class
Method 2: Invoking This method currently isn't in use. However, it still exists. To do so, have the code find the current game mode and have it call the EndGame
method, like in the OnAnimalCaught
example. Personally, I (Joseph) prefer Method 1, since that keeps EndGame
calls within the Game Mode itself.
Listening for a Game Over
The Game Mode provides a delegate that allows other objects to listen for when a game over condition is met. This is done in the GameOverScreen
widget in blueprints. To react to a game over state, process to follow is:
- Find the
AhriAndBearGameModeBase
actor. - Get the
OnGameOver
property. - Assign the
OnGameOver
delegate to use the method you want.
The method you assign to OnGameOver
needs to receive a single parameter - const FGameOver&
. This struct currently only contains one property, which is EGameOverReason Reason
. Since we may want to provide more information down the line without breaking a lot of code, this argument is a struct. The struct is Blueprintable, so you can listen to this delegate as you like.
Here's an example from the game over screen blueprint: