3.2 KiB
Part 8 (C++): Create Game Mode
← Previous: Part 7 (C++) - Create UI | Back to Index | Next: Part 9 (C++) - Polish & Debug Features →
Overview
Create a custom Game Mode to set the default pawn class and manage game rules.
Time: ~5 minutes
Step 8.1: Create STGGameMode C++ Class
- Tools → New C++ Class
- Choose "Game Mode Base" as parent
- Name:
STGGameMode - Create Class
Step 8.2: Define Game Mode
STGGameMode.h:
⚠️ IMPORTANT: Replace
YOURPROJECTNAME_APIwith your actual project's API macro (e.g.,BULLETHELLCPP_API).
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "STGGameMode.generated.h"
UCLASS()
class YOURPROJECTNAME_API ASTGGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
ASTGGameMode();
};
STGGameMode.cpp:
#include "STGGameMode.h"
#include "STGPawn.h"
ASTGGameMode::ASTGGameMode()
{
// Default to C++ pawn class
// We'll override this in a Blueprint child to use BP_Player
DefaultPawnClass = ASTGPawn::StaticClass();
bStartPlayersAsSpectators = false;
}
Note: We use the C++ STGPawn as default. In the next step, we'll create a Blueprint child of this GameMode to set BP_Player as the default pawn. This approach is more flexible than hardcoding Blueprint paths, which can break if files are moved.
Step 8.3: Create BP_STGGameMode Blueprint
We create a Blueprint child of STGGameMode to configure BP_Player as the default pawn:
- Content Browser → Right-click in empty space
- Select Blueprint Class
- In the popup, expand All Classes and search for
STGGameMode - Select STGGameMode as parent → Click Select
- Name:
BP_STGGameMode - Double-click to open the Blueprint
- In Class Defaults panel (right side):
- Find Default Pawn Class
- Click dropdown → Select
BP_Player
- Compile (top toolbar) → Save
Why use a Blueprint GameMode?
- Avoids hardcoded paths that break when files move
- Allows designers to change settings without recompiling
- Standard Unreal Engine practice for configuration
Step 8.4: Set Game Mode in Project Settings
- Edit → Project Settings
- Project → Maps & Modes
- Under "Default Modes":
- Default GameMode → Select
BP_STGGameMode(the Blueprint, not the C++ class)
- Default GameMode → Select
- Close Project Settings
⚠️ Important: Make sure to select
BP_STGGameMode, notSTGGameMode. The Blueprint version has BP_Player configured as the default pawn.
Step 8.4: Test
Create a new level or use existing one:
-
File → New Level → Empty Level
-
Save as
BulletHellLevel -
Add:
- Player Start (from Place Actors panel)
- STGEnemySpawner
- STGGameDirector
- STGHUDManager
- Directional Light (for visibility)
-
Press Play
Expected Result:
- ✅ Player automatically spawns at Player Start location
- ✅ No need to manually drag BP_Player into level
- ✅ All game systems work together
← Previous: Part 7 (C++) - Create UI | Back to Index | Next: Part 9 (C++) - Polish & Debug Features →