The C++ switch statement

The switch statement is used to select and execute a given block of code based on the value of a variable. The variable can be an integer type or an enumeration.

The example below shows an example of a switch command. The Difficulty variable is of type int32 and it can store the values 1, 2, and 3, which represent the difficulty of the game. TotalEnemies is also a variable of type int32. The value of the Difficulty variable will determine the value of the TotalEnemies variable.

switch ( Difficulty )
{
    
  case 1:
	  
       TotalEnemies = 20;
       break;

  case 2:
	  
       TotalEnemies = 30;
       break;

  case 3:
	  
       TotalEnemies = 40;
       break;

  default:
	  
       UE_LOG(LogTemp, Warning, TEXT("Difficulty variable: Invalid value."));
       break;		 
} 

The break statement is used to indicate the end of the block of code for each case. The default statement is optional and is executed when the value is not found.

In the example usage, we will see how to use the switch statement with an enumeration.

Example usage:

We are going to create an enumeration named EDifficulty that will be used to represent the difficulty in the game. Create the Difficulty.h header file and add this code:

#pragma once

#include "CoreMinimal.h"
#include "Difficulty.generated.h"

UENUM( BlueprintType )
enum EDifficulty
{
  VeryEasy,
  Easy,
  Normal,
  Hard,
  VeryHard
}; 

In the Unreal editor, create a C++ class named EnemySpawner using the Actor class as the parent class. This class has a variable of type int32 named TotalEnemies and an enumeration variable of type EDifficulty named GameDifficulty. The TotalEnemies value will be set in the BeginPlay() function using a switch statement with the GameDifficulty enumeration.

The EnemySpawner.h file must have this content:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Difficulty.h"
#include "EnemySpawner.generated.h"

UCLASS()
class TUTOPROJECT_API AEnemySpawner : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AEnemySpawner();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	int32 TotalEnemies;
	
	UPROPERTY(EditAnywhere, Category=EnemySpawner)
	TEnumAsByte< EDifficulty > GameDifficulty;
	
	UPROPERTY(VisibleAnywhere)
	USceneComponent* RootScene;

	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* StaticMesh;

};

This is the contents of the file EnemySpawner.cpp:

#include "EnemySpawner.h"

AEnemySpawner::AEnemySpawner()
{
  // Set this actor to call Tick() every frame.
  PrimaryActorTick.bCanEverTick = true;

  RootScene = CreateDefaultSubobject<USceneComponent>("RootScene");
  RootComponent = RootScene;

  StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("StaticMesh");
  StaticMesh->SetupAttachment(RootScene);
}

// Called when the game starts or when spawned
void AEnemySpawner::BeginPlay()
{
  Super::BeginPlay();

  switch ( GameDifficulty )
  {
    case EDifficulty::VeryEasy:
	  
         TotalEnemies = 10;
         break;

    case EDifficulty::Easy:
	  
         TotalEnemies = 20;
         break;

    case EDifficulty::Normal:
	  
         TotalEnemies = 30;
         break;

    case EDifficulty::Hard:
	  
         TotalEnemies = 40;
         break;

    case EDifficulty::VeryHard:
	  
         TotalEnemies = 50;
         break;		 
  }

  FString Message = FString::Printf(TEXT("TotalEnemies: %d"), TotalEnemies);
  
  if(GEngine)
  {
    GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, Message);
  }
  
}

// Called every frame
void AEnemySpawner::Tick(float DeltaTime)
{
  Super::Tick(DeltaTime);
}

Compile the C++ code and add an instance of EnemySpawner at the level. You can select the GameDifficulty value in the Details tab of the instance.

 

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu