Reader small image

You're reading from  Unreal Engine 5 Game Development with C++ Scripting

Product typeBook
Published inAug 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781804613931
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
ZHENYU GEORGE LI
ZHENYU GEORGE LI
author image
ZHENYU GEORGE LI

Zhenyu George Li is a passionate video game developer with 20+ years of experience. He has contributed significantly to many games and serves as a senior development consultant at Unity. His early immersion in technologies like C/C++, DirectX, OpenGL, and Windows GUI laid the foundation for his success. Notable titles in his portfolio include Magic Arena, Halo Infinity, Stela, Dead Rising 2, and The Bigs 2. He has gained extensive knowledge in programming, graphics, animation, gameplay, AI, multiplayer, and UI using Unreal and Unity engines. Additionally, he has taught UE at Vancouver Film School and has college teaching experience at College of Interactive Arts and Hefei Union University.
Read more about ZHENYU GEORGE LI

Right arrow

Investigating the Shooter Game’s Generated Project and C++ Code

Game developers choose to use game engines because they provide toolsets that help accelerate developer workflows. In Unreal Engine, what C++ scripting does is to program based on Unreal Engine’s predefined classes and APIs. Therefore, the best way to start learning about and understanding Unreal Engine C++ scripting is to review an engine-generated project.

By investigating the project structure and the source code, you will obtain not only an overall view of the C++ project’s structure but also the most used engine programming APIs. Based on the knowledge you just gained and the MyShooter C++ project created in Chapter 1, we will go through the following topics in this chapter:

  • Understanding the MyShooter C++ project structure
  • Understanding the game program structure
  • Getting familiar with the source code
  • Launching Unreal Editor and opening the game project in Visual Studio
  • ...

Technical requirements

In this chapter, while examining the code, we will just be previewing the concepts and the use of APIs, elements, and functions in the project. The technical details will be introduced in the subsequent chapter.

Understanding the MyShooter C++ project structure

You already learned about the regular C++ project structure in Chapter 3; now, let’s take a close look at an Unreal game’s C++ project structure.

Open MyShooter in Unreal Editor and select the C++ Classes/MyShooter folder in the Content Drawer window. There, you can find five C++ class files:

Figure 4.1 – MyShooter C++ project source files

Figure 4.1 – MyShooter C++ project source files

Double-click on any C++ class file to open the C++ project in VS.

Now, let’s look at Solution Explorer:

Figure 4.2 – MyShooter C++ project Solution Explorer

Figure 4.2 – MyShooter C++ project Solution Explorer

The first layer of the tree has three folder nodes – Engine, Games, and Visualizers:

  • The Engine folder contains the Unreal Engine project and the source code. In some cases, you may want to modify and customize the engine code.
  • The Games folder contains game projects. Here, it only has one project – MyShooter.
  • The...

Understanding the game program structure

A typical and simple game program usually has three phases – game initialization, game loop, and game end – and Unreal Engine handles these internally.

Unreal Engine offers a range of programming interfaces, including base classes, APIs, and systems, empowering developers to create highly immersive and interactive games. Leveraging these interfaces, developers write C++ code that integrates their custom functionality into the game.

The following flowchart illustrates the fundamental game flow and should give you an idea about writing game code for functional modules:

Figure 4.4 – Game program structure

Figure 4.4 – Game program structure

Let’s take a look at some of these ideas:

  • Game initialization happens when a game program starts. All the initialization tasks, such as setting the display mode, loading required contents, spawning game objects, and so on, are done during this phase.
  • Game loop is the main...

Getting familiar with the source code

In this section, we will go through the generated .cpp and .h source code and explain the code lines or line blocks so that you can better understand how code is organized and written to implement the expected functionalities.

First of all, let’s take a look at the MyShooterCharacter.h header file for the definition of a typical Unreal class.

MyShooterCharacter.h

MyShooterCharacter.h is the header file that defines the game’s character class. The MyShooterCharacter class is going to be used to create player characters.

The code of this header file can be divided into four parts:

  • The definition of the AMyShooterCharacter class
  • The definition of the class variables
  • The declaration of the class member functions
  • The declaration of the functions for setting up inputs and the getter functions

The header file’s first part mainly defines the MyShooterCharacter class, which inherits from Unreal...

Launching Unreal Editor and opening the game project in Visual Studio

You already know how to open an existing Unreal C++ project in Unreal Editor through the Epic Games Launcher – this is the standard, easy method in most situations – but another way to launch Unreal Editor and open your game project is by directly running the program in Visual Studio.

The main benefits of starting Unreal Editor and opening game projects in Visual Studio are as follows:

  • Debugging the source code and troubleshooting bugs
  • Having accessibility to the engine source
  • Customizing the engine for special needs
  • Fixing engine bugs

Follow these steps to open the game project in VS:

  1. Launch Visual Studio.
  2. Open the MyShooter.sln C++ solution.
  3. Choose the right build configuration from Solution Configurations:
Figure 4.24 – Visual Studio Solution Configurations

Figure 4.24 – Visual Studio Solution Configurations

The build configuration options are as follows:

    ...

Summary

We just investigated the C++ MyShooter project by looking at the basic project structure and reviewing the source code. You should now have an overall idea about what the C++ scripts do and how they collaborate with the engine and the Blueprints.

From the sample game code, you have also learned some useful Unreal C++ scripting skills.

First, we reviewed the code for creating Unreal recognizable C++ classes by marking them with the UCLASS macro and the specifiers. You now understand that the Unreal Engine Reflection System will use the information associated with the macros and the specifiers to spawn objects and components. You then got to know some Unreal base classes such as ACharacter, AActor, USphereComponent, UActorComponent, and AGameModeBase that can be inherited to create new game classes.

Second, we looked at the code that defines class variables and declares class functions with the UPROPERTY and UFUNCTION macros. Like the UCLASS macro you learned about before...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unreal Engine 5 Game Development with C++ Scripting
Published in: Aug 2023Publisher: PacktISBN-13: 9781804613931
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.
undefined
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 AU $19.99/month. Cancel anytime

Author (1)

author image
ZHENYU GEORGE LI

Zhenyu George Li is a passionate video game developer with 20+ years of experience. He has contributed significantly to many games and serves as a senior development consultant at Unity. His early immersion in technologies like C/C++, DirectX, OpenGL, and Windows GUI laid the foundation for his success. Notable titles in his portfolio include Magic Arena, Halo Infinity, Stela, Dead Rising 2, and The Bigs 2. He has gained extensive knowledge in programming, graphics, animation, gameplay, AI, multiplayer, and UI using Unreal and Unity engines. Additionally, he has taught UE at Vancouver Film School and has college teaching experience at College of Interactive Arts and Hefei Union University.
Read more about ZHENYU GEORGE LI