Battery Collector #20 – Handling New Play States

I am continuing the Battery Collector tutorial. This is the 20th video. Click here for the first part and here for the previous one.

Handling New Play States

In BatteryCollectorGameMode.h, I am declaring a private function to handle new state:

	/** Hanlde any function calls that rely upon changing the playing state of our game */
	void HandleNewState(EBatteryPlayState NewState);

In BatteryCollectorGameMode.cpp, I am defining the function

void ABatteryCollectorGameMode::HandleNewState(EBatteryPlayState NewState)
{
	switch (NewState)
	{
	// If the game is playing
	case EBatteryPlayState::EPlaying:
	{
		// spawn volumes active
		for (ASpawnVolume* Volume : SpawnVolumeActors)
		{
			Volume->SetSpawningActive(true);
		}
	}
	break;
	// If we've won the game
	case EBatteryPlayState::EWon:
	{
		// spawn volumes inactive
		for (ASpawnVolume* Volume : SpawnVolumeActors)
		{
			Volume->SetSpawningActive(false);
		}
	}
	break;
	// If we've lost the game
	case EBatteryPlayState::EGameOver:
	{
		// spawn volumes inactives
		for (ASpawnVolume* Volume : SpawnVolumeActors)
		{
			Volume->SetSpawningActive(false);
		}
		// block player input
		APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);
		if (PlayerController)
		{
			PlayerController->SetCinematicMode(true, false, false, true, true);
		}
		// ragdoll the character
		ACharacter* MyCharacter = UGameplayStatics::GetPlayerCharacter(this, 0);
		if (MyCharacter)
		{
			MyCharacter->GetMesh()->SetSimulatePhysics(true);
			MyCharacter->GetMovementComponent()->MovementState.bCanJump = false;
		}
	}
	break;
	// Unknown//default state
	default:
	case EBatteryPlayState::EUnknown:
	{
		// do nothing
	}
	break;
	}
}

I am calling that function in SetCurrentState:

void ABatteryCollectorGameMode::SetCurrentState(EBatteryPlayState NewState)
{
	CurrentState = NewState;
	HandleNewState(CurrentState);
}

Back in Unreal Editor, I am setting the Decay rate to 0,1 in the GameMode_BP to quickly test the Game Over state.
I need also to create Physics Assets for the Mannequin by right-clicking on it and select “Create Physics Asset”:

CreatePhysicsAsset

Then I am opening the ThirdPersonCharacter blueprint, I am selecting the Mesh and in the Details Panel, I scroll down to the Collision section, change the Presets to Custom and set the “Collision Enabled” to “Collision Enabled (Query and Physics)“:

Collision

Now, I can see the character dying when Game is Over:

GameOver

Next Step

Next, I will adding finishing touches

About

Gamer

Tagged with:

Leave a Reply