Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Unreal Engine 5 Game Development with C++ Scripting

You're reading from   Unreal Engine 5 Game Development with C++ Scripting Become a professional game developer and create fully functional, high-quality games

Arrow left icon
Product type Paperback
Published in Aug 2023
Publisher Packt
ISBN-13 9781804613931
Length 384 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
ZHENYU GEORGE LI ZHENYU GEORGE LI
Author Profile Icon ZHENYU GEORGE LI
ZHENYU GEORGE LI
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Part 1 – Getting Started with Unreal C++ Scripting
2. Chapter 1: Creating Your First Unreal C++ Game FREE CHAPTER 3. Chapter 2: Editing C++ Code in Visual Studio 4. Chapter 3: Learning C++ and Object-Oriented Programming 5. Chapter 4: Investigating the Shooter Game’s Generated Project and C++ Code 6. Part 2 – C++ Scripting for Unreal Engine
7. Chapter 5: Learning How to Use UE Gameplay Framework Base Classes 8. Chapter 6: Creating Game Actors 9. Chapter 7: Controlling Characters 10. Chapter 8: Handling Collisions 11. Chapter 9: Improving C++ Code Quality 12. Part 3: Making a Complete Multiplayer Game
13. Chapter 10: Making Pangaea a Network Multiplayer Game 14. Chapter 11: Controlling the Game Flow 15. Chapter 12: Polishing and Packaging the Game 16. Index 17. Other Books You May Enjoy

Packaging the game

During the development process, we extensively played the Pangaea game within Unreal Editor. However, our goal is to make the game accessible to players without requiring them to install Unreal Engine. Luckily, Unreal Engine provides a convenient packaging feature that allows us to create standalone install packages for various platforms. This means we can distribute the Pangaea game as a separate application for Windows, macOS, iOS, Android, and more. To demonstrate this, we will create a Pangaea Windows game installation package.

Prior to the packaging process for the game, certain project settings need to be configured.

Configuring the project settings for packaging

To successfully package a game, as the bare minimum, there are two tasks you need to complete, as follows:

  • The first task involves setting the project defaults, which entails designating the default game mode, player pawn, and so on
  • The second task involves including the necessary game levels within the generated installation package, ensuring that the game levels are present for the gameplay

Let’s start setting the project defaults in the project settings.

Setting the project defaults

To work on the settings, choose Edit | Project Settings… from the editor’s main menu. Then, from the Project Settings window, find and select Maps & Modes under the Project group on the All Settings panel. Now, choose the following settings:

  • Default Game Mode: PangaeaGameMode
  • Default Pawn Class: BP_PlayerAvatar
  • Player Controller Class: BP_TopdownPlayerController
  • Game State Class: PangaeaGameState
  • Game Default Map: LobbyMap
  • Game Instance Class: PangaeaGameInstance

We can see these settings in the following screenshot:

Figure 12.11 – Project – Maps & Modes settings

Figure 12.11 – Project – Maps & Modes settings

Next, we want to add the required game levels to the list of included maps.

Including the game levels in the built package

In the Project Settings window, find and select Packaging under the Project group. Then, click the Add Element button (denoted by the + symbol) to add LobbyMap and TopDownMap to the List of maps to include in a packaged build setting:

Figure 12.12 – Adding game maps that will be included in the package build

Figure 12.12 – Adding game maps that will be included in the package build

The project settings are now prepared for packaging the project, but if we desire the built game to run in the 1,280x720 window mode, this is the opportune moment to utilize the r.setres console command to accomplish it.

Making the build a windowed game

By default, the packaged standalone build of the game is launched in fullscreen mode. However, it is more convenient to test the multiplayer functionality by running two instances of Pangaea in separate windows on your machine.

To facilitate this, you can configure the game to start as a windowed application with a resolution of 1,280x720 and ensure that the mouse cursor is visible. To achieve this, a minor modification can be made to the level blueprint of the LobbyMap level blueprint, as follows:

  1. Execute the console command to start the game in the 1,280x720 window mode.
  2. Check the Show Mouse Cursor checkbox in the SET node:
Figure 12.13 – Modifying the LobbyMap level blueprint

Figure 12.13 – Modifying the LobbyMap level blueprint

We just accomplished the task by utilizing the LobbyMap level blueprint to ensure we start the game with the required resolution and in window mode. One more thing to improve is to remove the hardcoded path for finding the BP_Fireball and BP_Hammer blueprint classes for spawning the actors.

We just completed a task that allowed us to ensure that the game starts with the required resolution and in window mode. One further improvement is to eliminate the hardcoded path used to locate the BP_Fireball and BP_Hammer blueprint classes for spawning actors.

Avoiding the hardcoded path for finding content

Within the game’s C++ source code, we utilized ConstructorHelpers::FObjectFinder to obtain references to the BP_Fireball and BP_Hammer classes for spawning fireball and hammer instances. However, this approach employs hardcoded paths to the assets, which may lead to runtime errors when the target asset is missing or if the specified path cannot be located.

The following code snippets depict the original implementation of the ADefenseTower and AEnemy classes that we aim to replace.

Here is DefenseTower.cpp:

static ConstructorHelpers::FObjectFinder<UBlueprint> blueprint_finder(TEXT("Blueprint'/Game/TopDown/Blueprints/BP_Fireball.BP_Fireball'"));
_FireballClass = (UClass*)blueprint_finder.Object->GeneratedClass;

And here is Enemy.cpp:

static ConstructorHelpers::FObjectFinder<UBlueprint> blueprint_finder(TEXT("Blueprint'/Game/TopDown/Blueprints/BP_Hammer.BP_Hammer'"));
_WeaponClass = (UClass*)blueprint_finder.Object->GeneratedClass;

To improve this code, we want to define _FireballClass and _WeaponClass as UPROPERTY variables so that we can select and specify Blueprint classes in the editor.

Let’s start by modifying the DefenseTower class:

  1. In Enemy.h, change the _FireballClass variable type to TSubClassOf<AProjectile>. Then, move the variable definition code to the public section. To maintain the code standard, we can change the variable name to FireballClass (removing the heading underscore, _):
    public:
      UPROPERTY(EditAnywhere, Category = "Tower Params")
      TSubclassOf<AProjectile> FireballClass;
  2. In Enemy.cpp, remove the following two lines of code:
    static ConstructorHelpers::FObjectFinder<UBlueprint>   blueprint_finder(TEXT("Blueprint'/Game/TopDown/Blueprints/  BP_Fireball.BP_Fireball'"));
    FireballClass = (UClass*)blueprint_finder.Object->GeneratedClass;
  3. Change the parameter name from _FireballClass to FireballClass when calling the SpawnOrGetFireball function:
    void ADefenseTower::Fire()
    {
    auto fireball = _PangaeaGameMode->SpawnOrGetFireball(
                           FireballClass);
      …
    }
  4. Select BP_Fireball for the FireballClass field for BP_DefenseTower in the editor. Then, do the following:
    1. Compile and reopen the project in the Unreal Editor.
    2. Open BP_DefenseTower from the Content | Topdown | Blueprints folder.
    3. Select BP_Fireball for the Fireball Class field:
Figure 12.14 – Selecting BP_Fireball for the FireballClass field on the blueprint editor

Figure 12.14 – Selecting BP_Fireball for the FireballClass field on the blueprint editor

  1. Compile and save BP_DefenseTower.

Similarly, we can apply the aforementioned steps to make modifications to the AEnemy class. In Enemy.h, define the WeaponClass variable as a subclass of AWeapon:

public:
  UPROPERTY(EditAnywhere)
  TSubclassOf<AWeapon> WeaponClass;

Then, comment or remove the two asset finder lines of code from the constructor of AEnemy in Enemy.cpp:

AEnemy::AEnemy()
{
  …
  //static ConstructorHelpers::FObjectFinder<UBlueprint>     blueprint_finder(TEXT("Blueprint'/Game/TopDown/Blueprints/    BP_Hammer.BP_Hammer'"));
//_WeaponClass = (UClass*)blueprint_finder.Object->GeneratedClass;
}
void AEnemy::BeginPlay()
{
  Super::BeginPlay();
_Weapon = Cast<AWeapon>(
         GetWorld()->SpawnActor(WeaponClass));
  …
}

We are now ready to package the game build.

Packaging the project

To package the project, we can complete the following steps:

  1. Click Platforms on the editor’s toolbar. Then, select Windows.
  2. Next, choose one of the packaging configuration types (see Figure 12.15):
    1. DebugGame: When packaging with this configuration, the game engine code is optimized, whereas the game code is debuggable without optimizations
    2. Development: This is the engine’s default compiling configuration, which only optimizes the most time-consuming engine and game code but leaves the other code unoptimized and debuggable
    3. Shipping: When packaging with this configuration, all debug symbols—including logs, status, profiling data, and so on—are stripped off, and the project is fully optimized for the best game performance
  3. Click the Package Project item on the second-layer menu.
Figure 12.15 – Starting to pack the game

Figure 12.15 – Starting to pack the game

  1. Select the target folder for where the packaged build will be saved (see Figure 12.16). Once the Select Folder button is pressed, Unreal will start the packaging process:
Figure 12.16 – Selecting the target folder (for example, C:\Projects\PangaeaBuild) to save the game build

Figure 12.16 – Selecting the target folder (for example, C:\Projects\PangaeaBuild) to save the game build

  1. While packaging, you can view the progress and the logs in the Output Log window located at the bottom of the editor:
Figure 12.17 – Viewing packaging progress logs in the Output Log window

Figure 12.17 – Viewing packaging progress logs in the Output Log window

  1. Once the packaging process is complete, you can locate the packaged files and their corresponding subfolders in the designated target folder (see Figure 12.18):
Figure 12.18 – The generated package build files

Figure 12.18 – The generated package build files

  1. Then, to launch the standalone game, simply double-click on the Pangaea.exe executable.

Congratulations on acquiring the playable multiplayer Pangaea game build, which you can now copy and distribute to share the game with others!

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image