praca_magisterska/games/unreal/tutorial/part-8-cpp-create-game-mode.md

2.5 KiB

Part 8 (C++): Create Game Mode

← Previous: Part 7 (C++) - Create UI | Back to Index | Next: Part 9 (C++) - Final Setup →


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

  1. Tools → New C++ Class
  2. Choose "Game Mode Base" as parent
  3. Name: STGGameMode
  4. Create Class

Step 8.2: Define Game Mode

STGGameMode.h:

⚠️ IMPORTANT: Replace YOURPROJECTNAME_API with 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"
#include "UObject/ConstructorHelpers.h"

ASTGGameMode::ASTGGameMode()
{
    // Find and use the BP_Player Blueprint class
    static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/BP_Player"));
    if (PlayerPawnBPClass.Succeeded())
    {
        DefaultPawnClass = PlayerPawnBPClass.Class;
    }
    else
    {
        // Fallback to C++ class if Blueprint not found
        DefaultPawnClass = ASTGPawn::StaticClass();
    }
    
    bStartPlayersAsSpectators = false;
}

Note: The path /Game/BP_Player assumes BP_Player is in your Content root folder. If you placed it elsewhere (e.g., Content/Blueprints/), adjust the path accordingly (e.g., /Game/Blueprints/BP_Player).


Step 8.3: Set Game Mode in Project Settings

  1. Edit → Project Settings
  2. Project → Maps & Modes
  3. Under "Default Modes":
    • Default GameMode → Select STGGameMode
  4. Close Project Settings

Step 8.4: Test

Create a new level or use existing one:

  1. File → New Level → Empty Level

  2. Save as BulletHellLevel

  3. Add:

    • Player Start (from Place Actors panel)
    • STGEnemySpawner
    • STGGameDirector
    • STGHUDManager
    • Directional Light (for visibility)
  4. 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++) - Final Setup →