UE5Learn Gameplay (2)  Toon Tanks

UE5Learn Gameplay (2) Toon Tanks
Unreal Engine

A UE5 Third Person Tank Game

Overview Video

What I Learnt?

AGameModeBase and UWorld class

In my current thinking, they are both unique and similar to a singleton instance.

AGameModeBase

The AGameModeBase is a class employed to define game rules and settings. It typically handles the following aspects:

  1. Game Rules: By inheriting AGameModeBase, I can create customized game modes to delineate game rules and logic, such as determining the player’s life value and winning conditions.

  2. Player Spawn Points: It also enables the definition of player spawn points in the game world.

  3. Game State: It keeps track of the game state, detailing if the game has commenced or concluded.

  4. Player Controller: The class allows for the specification of a player controller class tasked with handling player inputs.

In this project, I configured a game mode using C++ that will be utilized throughout the game. Within this framework, I outlined the game’s delay onset time and instances of Pawns, including controls for both the player and enemies.

// Control the end contidition of the game
void AToonTankGameMode::ActorDied(AActor* DeadActor)
{
	if (DeadActor == TankRef)
	{
		TankRef->HandleDestruction();
		if (TankPlayerControllerRef)
		{
			TankPlayerControllerRef->SetPlayerEnabledState(false);
		}
		GameOver(false);
	}
	else if (ATower* DestroyedTower = Cast<ATower>(DeadActor))
	{
		DestroyedTower->HandleDestruction();
		TargetTowers--;
		if (TargetTowers == 0)
		{
			GameOver(true);
		}
	}
}

Subsequently, by registering and executing events adorned with the UFUNCTION(BlueprintImplementableEvent) modifier in Blueprint, I achieved control over the UI Widget.

bp-gamemode

Consequently, it abides by the principles of the singleton pattern, existing uniquely within a specific game instance.

UWorld

The UWorld object acts as a container for all game objects such as characters, artifacts, etc. In UE5, one can directly obtain the world object through:

    GetWorld()

It primarily encompasses the following elements and functionalities:

  1. Object Container: It harbors and manages all game entities including characters, NPCs (Non-Player Characters), and static and dynamic environment objects.
  2. Environment Settings: It contains settings and attributes such as lighting and weather conditions.
  3. Physical Simulation: It governs physical simulations managing all physical interactions and collision detections.
  4. Level Management: The UWorld supervises game levels, enabling the loading, unloading, and transition between various game levels. In this project, I registered the OnDamaged callback in the HPComponent through:
	GetOwner()->OnTakeAnyDamage.AddDynamic(this, &UHPComponent::OnDamageTaken);

During the instance collision of the Project, a signal is “emitted”:

auto DamageTypeClass = UDamageType::StaticClass();

if (OtherActor != nullptr && OtherActor != this && OtherActor != MyOwner)
{
    UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstigator, this, DamageTypeClass);
//...
}
//...

Here, the Damage seemingly embodies an implementation of the observer and mediator patterns, with UWorld serving a mediator role encapsulating entities in a container.

© 2023 🐸 Fanxiang Zhou 🐸