Home Game Development Unreal Engine 5 Game Development with C++ Scripting

Unreal Engine 5 Game Development with C++ Scripting

By ZHENYU GEORGE LI
ai-assist-svg-icon Book + AI Assistant
eBook + AI Assistant $39.99 $27.98
Print $49.99
Subscription $15.99 $10 p/m for three months
ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription.
ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription. $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime! ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription.
What do you get with a Packt Subscription?
Gain access to our AI Assistant (beta) for an exclusive selection of 500 books, available during your subscription period. Enjoy a personalized, interactive, and narrative experience to engage with the book content on a deeper level.
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
Gain access to our AI Assistant (beta) for an exclusive selection of 500 books, available during your subscription period. Enjoy a personalized, interactive, and narrative experience to engage with the book content on a deeper level.
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Along with your eBook purchase, enjoy AI Assistant (beta) access in our online reader for a personalized, interactive reading experience.
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription. ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription. BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime! ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription.
eBook + AI Assistant $39.99 $27.98
Print $49.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
Gain access to our AI Assistant (beta) for an exclusive selection of 500 books, available during your subscription period. Enjoy a personalized, interactive, and narrative experience to engage with the book content on a deeper level.
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
Gain access to our AI Assistant (beta) for an exclusive selection of 500 books, available during your subscription period. Enjoy a personalized, interactive, and narrative experience to engage with the book content on a deeper level.
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Along with your eBook purchase, enjoy AI Assistant (beta) access in our online reader for a personalized, interactive reading experience.
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Chapter 1: Creating Your First Unreal C++ Game
About this book
Unreal Engine is one of the most popular and accessible game engines in the industry, creating multiple job opportunities. Owing to C++ scripting's high performance, advanced algorithms, and engineering maintenance, it has become the industry standard for developing commercial games. However, C++ scripting can be overwhelming for anyone without a programming background. Unreal Engine 5 Game Development with C++ Scripting will help you master C++ and get a head start on your game development journey. You’ll start by creating an Unreal Engine C++ project from the shooter template and then move on to building the C++ project and the C++ code inside the Visual Studio editor. You’ll be introduced to the fundamental C++ syntax and essential object-oriented programming concepts. For a holistic understanding of game development, you’ll also uncover various aspects of the game, including character creation, player input and character control, gameplay, collision detection, UI, networking, and packaging a completed multiplayer game. By the end of this book, you’ll be well-equipped to create professional, high-quality games using Unreal Engine 5 with C++, and will have built a solid foundation for more advanced C++ programming and game development technologies.
Publication date:
August 2023
Publisher
Packt
Pages
384
ISBN
9781804613931

 

Creating Your First Unreal C++ Game

Unreal Engine (UE) is one of the most popular 3D computer graphics game engines developed by Epic Games, providing a comprehensive set of tools and functionalities to develop high-quality, immersive 3D simulations. The engine offers its intuitive visual scripting system, Blueprint, and a robust C++ programming framework for developers of all skill levels. This book provides a concise introduction to C++ programming and demonstrates how to write C++ scripts in UE for game development.

In this chapter, you will learn the essential skill of creating an Unreal C++ project from scratch or converting an existing Unreal Blueprint project into an Unreal C++ project, which serves as a fundamental skill to advance in game development. By mastering this process, you will gain the necessary foundation to take your game development abilities to the next level.

This chapter will cover the following topics:

  • Understanding C++ scripting in Unreal
  • Creating your C++ shooter project from a template
  • Converting an existing Blueprint project to a C++ project
 

Technical requirements

As a reader of this book, you will be expected to have common computer operational skills. You should also have basic knowledge of and experience with the UE5 editor, as well as some Blueprint scripting skills.

To follow this chapter, you should have installed Epic Games Hub and the 5.03 or later version of the engine editor on your computer. If you haven’t done so, please go to the official Epic website (https://www.unrealengine.com/en-US) to register an account and download the Epic Games Launcher.

The minimum required development environment is as follows:

  • Operating system: Windows 10
  • Processor: Intel 7th generation or equivalent
  • Memory: 16 GB of RAM
  • GPU: GTX 1080 (or AMD equivalent)
  • DirectX: Version 12
  • Storage: 25 GB of available space
  • Additional notes: 8 GB of VRAM recommended

The official system requirements can be found here: https://docs.unrealengine.com/5.0/en-US/hardware-and-software-specifications-for-unreal-engine/. To save game editing time in the UE5 editor, it is recommended to use a computer with an i9 (or an AMD equivalent) CPU, 64 GB of RAM, and a GeForce RTX 3060 video card.

 

Understanding C++ scripting in Unreal

Before getting started, we need to answer some questions that people usually ask about C++ scripting. This will help to clarify the pros and cons of using C++, the reasons to use C++, and the difference between UE C++ scripting and C++ programming.

What is the difference between C++ and Blueprint?

Both C++ and Blueprint are scripting languages that can accomplish the same tasks, but one might be better suited than the other under certain circumstances. The main difference between them is that C++ is a programming language that allows you to write general-purpose, text-based code, whereas Blueprint is a visual scripting system for UE.

For UE projects, game studios usually use both C++ and Blueprint to develop commercial-level games. C++ is usually used for advanced techniques, complex algorithms, and big-scale logic code. If you can script with C++, you will have more chances to work on a professional team.

One of the most important advantages of using C++ is performance. C++ allows you to write low-level operational code. It also provides control over the core system that is not accessible to Blueprint. In addition, the final C++ code will eventually be optimized and compiled to be machine-friendly binary native code. On the other hand, Blueprint scripts are interpreted and executed by a middle layer, which means more execution time.

C++ code and files can be well-organized based on an entire project’s mechanics. It is easy to globally search, locate, and access code blocks to edit, maintain, and troubleshoot. In the meantime, it is also easier to read and understand a big chunk of code that implements complex algorithms and logic. Blueprint, on the other hand, is a context-sensitive scripting environment. Blueprint graphs are relatively independent. When a graph needs to solve complex logic, the nodes and the connection lines create messy spaghetti that can hardly be understood and maintained.

C++ also has some shortcomings. One example is that it may cause critical errors that may crash an entire system. That is usually caused by the developer’s mistakes. Since Blueprint is a protected layer, it is safer, and hence, the chances of the system crashing are fewer.

In conclusion, the choice between C++ and Blueprint should be made based on specific development requirements and conditions, considering the pros and cons of each approach.

When do you use C++?

Both C++ and Blueprint can handle game development processes without a problem. There is no exact rule that regulates when to use C++ or Blueprint. It mainly depends on your experience and the actual needs of different games. You make your own decision based on how much you know about the two scripting systems.

Before you start working on something, you can ask yourself this question: “Where does it make sense to use C++, and where does it make sense to use Blueprints?” We recommend basing your answer on the following aspects and trade-offs:

  • Performance
  • Logic and algorithm complexity
  • Accessibility to a system’s core functions
  • The developer’s experience

If you want higher performance and deal with advanced game logic and system processes, and you are capable of coding and solving complex problems, you should go for C++.

What is the difference between C++ programming and C++ scripting?

You may be confused about the difference between C++ programming and C++ scripting. We want to clarify the meanings of these two terms.

C++ programming means using the C++ programming language to write code for any purpose; it doesn’t have to be just for UE projects. C++ scripting, in this book, is a specific dialect of the C++ programming language supported by the UE. It takes advantage of the power of C++ syntax and also works with UE’s Application Programming Interfaces (APIs), which allow developers to create and extend the engine’s functionalities for their games and the development environment’s context, such as objects, graphics, audio, and network communication.

Now that we have a basic overview of C++ and have learned why and when to use C++ for Unreal game developments, let’s dive deeper into C++ scripting by creating a sample project.

 

Creating your C++ Shooter project from a template

Now, it’s the time to get your hands dirty working on a UE5 C++ project yourself. We will go through the steps to create a new C++ project from scratch based on the First Person template.

The First Person template is one of the default game templates that come with UE. When you want to create a new project, you can pick this template from the Unreal Project Browser window. Our new MyShooter game will derive all the features from the template game, and we don’t have to do any additional work.

To get started with C++ scripting, we first need to install an IDE. In this book, we will use MS Visual Studio 2022 as an example.

Installing Visual Studio 2022

Visual Studio (VS) is an Integrated Development Environment (IDE) from Microsoft. It is a tool used to create, edit, debug, and compile code. In order to do C++ scripting, you need to go to the official website at https://visualstudio.microsoft.com/vs/ and download the Community 2022 version installation package (see Figure 1.1).

Figure 1.1 – Downloading VS 2022

Figure 1.1 – Downloading VS 2022

Note

To install VS, a Microsoft account is typically required. If you don’t have a Microsoft account, you can register using the following page: https://account.microsoft.com/account/.

Next, launch VisualStudioSetup.exe inside the folder where you downloaded the VS installer (the \Downloads folder, for example).

Enable the two Game development with C++ and Desktop development with C++ checkboxes – these two options tell the installer to install the C++ compiler and the professional game development support for UE (see Figure 1.2).

Figure 1.2 – Picking workloads for the VS installation

Figure 1.2 – Picking workloads for the VS installation

Also, keep an eye on the following options on the Installation details panel that belongs to the Desktop development with C++ group, and make sure the following are checked:

  • C++ profiling tools
  • C++ AddressSanitizer
  • Windows 10 SDK
  • IntelliCode
  • IDE support for Unreal Engine

Then, click the Install button to install the workloads and reboot the system, and then you will see a prompt from the dialog popup (see Figure 1.3):

Figure 1.3 – The VS Done installing dialog box

Figure 1.3 – The VS Done installing dialog box

The next thing we need to do is to confirm that we have installed the engine source code together with the UE5 editor. The reason why we need this is that when we generate a new project, the engine source code can be integrated into the new project; under certain circumstances, we may need to modify or customize the engine for the game’s specific needs.

Ensuring your UE has the source code installed

Before launching the UE5 editor, we first need to check whether Engine Source is installed for the editor. By doing this check, we make sure that the UE5 source code is integrated with the C++ projects we are going to create.

The three steps to check or install the engine source code are as follows:

  1. Click the downward arrow button and choose Options from the drop-down menu.
  2. Make sure that the Engine Source option is checked.
  3. Press the Apply button:
Figure 1.4 – The UE5 Options menu

Figure 1.4 – The UE5 Options menu

UE is an ongoing development product, with bugs and defects that may need to be fixed by its users. Also, professional developers sometimes modify the engine source code to adapt to their specific needs. An example of this is when we face an issue with geometry instancing (or instanced rendering) working only in the game’s development build but not in the release build, which is subsequently resolved by our engineer modifying the engine’s source code.

Note

Geometry instancing is a rendering technique that renders multiple instances of a visual object in a single draw call and provides each instance with some unique attributes: https://en.wikipedia.org/wiki/Geometry_instancing.

We are now ready to start the UE editor through the Epic Games Launcher.

Launching the UE5 editor through the Epic Games Launcher

Launching the UE5 editor is pretty straightforward. You simply click the Launch button on the 5.03 engine card to start the editor (see Figure 1.5).

Figure 1.5 – Launching the UE5 editor from the Epic Games Launcher

Figure 1.5 – Launching the UE5 editor from the Epic Games Launcher

The next thing we want to do is to create a new game project. Let’s name the new project MyShooter.

Creating the MyShooter C++ project

To create the project, follow these steps (and see Figure 1.6 for reference):

  1. In the Unreal Project Browser window, choose the GAMES tab on the left side.
  2. Select the First Person template.
  3. Select the C++ button.
  4. Choose the project location (for example, C:\UEProjects) and type MyShooter in the Project Name field.
  5. Click the Create button.
Figure 1.6 – Creating the MyShooter project

Figure 1.6 – Creating the MyShooter project

The created game project also includes the starter content, which is packaged with assets and resources that can be used to prototype the game.

The engine will do some initialization work and then open the editor when things are ready. If you look at the project tree panel’s MyShooter tab in the bottom-left corner of the editor window, you should see the C++ Classes node on the same layer as the Content node (see Figure 1.7).

Figure 1.7 – The MyShooter C++ project opened in the UE5 editor

Figure 1.7 – The MyShooter C++ project opened in the UE5 editor

Associating VS with UE5 as the default source code editor

Since we created the C++, project, all the C++ source code for the game was already generated. To open the source files directly in the UE5 editor, we want to associate VS as the engine editor’s default IDE.

On the UE5 Editor’s main menu, select Edit | Editor Preferences to open the preference window, then find the General | Source Code item on the left panel, and finally, pick Visual Studio 2022 from the Source Code Editor dropdown (see Figure 1.8).

Figure 1.8 – Making VS the default source code editor

Figure 1.8 – Making VS the default source code editor

You can now use VS to open the source code files.

Opening the C++ source code in VS (optional)

If you want to open and view the C++ source code in VS, you can find the source code file (for example, C++/MyShooter/MyShooterCharacter.cpp) in the project and simply double-click on it (see Figure 1.9).

Figure 1.9 – Opening MyShooterCharacter.cpp source code in VS

Figure 1.9 – Opening MyShooterCharacter.cpp source code in VS

The system will automatically launch VS, and the VS editor will open the MyShooterCharacter.cpp file (see Figure 1.10).

Figure 1.10 – Viewing the MyShooterCharacter.cpp source code in VS

Figure 1.10 – Viewing the MyShooterCharacter.cpp source code in VS

Back in the Unreal editor, click the Play (A picture containing text, electronics, clock radio

Description automatically generated) button to start the game. While playing the game on the battlefield, you can control your character, move them around, and pick up the gun in front of them (see Figure 1.11).

Figure 1.11 – Playing the MyShooter game

Figure 1.11 – Playing the MyShooter game

We have learned how to create a UE C++ project from scratch. However, what if we already have a Blueprint project and want to convert it to a C++ project? UE allows developers to do it by adding a new C++ class to the project. Let’s practice converting a MyBPShooter Blueprint project.

 

Converting an existing Blueprint project to a C++ project

UE provides a very straightforward way to convert an existing Blueprint project to a C++ project. All you need to do is add a C++ class to your project and then let UE take care of the conversion and add the needed project files:

  1. First of all, you have to create a Blueprint project, MyBPShoopter, under C:\UEProjects (you can choose a different path to create the new project). Use the same steps introduced in the Creating the MyShooter C++ project section, but choose BLUEPRINT instead of C++ for the creation of the MyBPShooter project.
Figure 1.12 – Creating MyBPShooter in UE5

Figure 1.12 – Creating MyBPShooter in UE5

  1. Secondly, open the new project in UE5. Pay attention to the project tree; it doesn’t have the C++ Classes node at this stage.
Figure 1.13 – Open MyBPShooter in UE5

Figure 1.13 – Open MyBPShooter in UE5

  1. Select Tools | New C++ Class from the editor’s main menu, and then, in the Add C++ Class window (see Figure 1.14), choose Character as the base class (a class that contains common attributes and methods that are shared by its derived classes) to create the MyShooterCharacter class.
Figure 1.14 – Adding a new C++ class from the Character class

Figure 1.14 – Adding a new C++ class from the Character class

Once you click the Next> button, it will navigate to the NAME YOUR NEW CHARACTER screen.

  1. On the NAME YOUR NEW CHARACTER screen, type MyBPShooterCharacter into the Name field.
Figure 1.15 – Adding the MyBPShooterCharacter C++ class

Figure 1.15 – Adding the MyBPShooterCharacter C++ class

Please pay attention to the path where the header and the source files will be placed. They look different from the MyShooter project because the C++ node hasn’t been created yet. Don’t worry about it at the moment. Once the conversion job is done, the system will automatically move the files to the right place.

  1. After clicking the Create Class button, you will see a progress bar.
Figure 1.16 – The MyBPShooterCharacter C++ class Adding code to project… progress bar

Figure 1.16 – The MyBPShooterCharacter C++ class Adding code to project… progress bar

Wait for the pop-up message, which indicates that the C++ class job has been added.

Figure 1.17 – A message saying that the MyBPShooterCharacter C++ class is now added

Figure 1.17 – A message saying that the MyBPShooterCharacter C++ class is now added

  1. Click the OK button. Now, you will see the message dialog, which asks you whether you want to edit the code (see Figure 1.18). Choose No here.
Figure 1.18 – Dialog for editing the MyBPShooterCharacter source code

Figure 1.18 – Dialog for editing the MyBPShooterCharacter source code

  1. Shut down your UE editor and reopen MyBPShooter. When you see a dialog that asks whether you want to rebuild the project, answer Yes here.
Figure 1.19 – The rebuilding MyBPShooter dialog

Figure 1.19 – The rebuilding MyBPShooter dialog

When it is done, you will find the new C++ Classes node on the project tree, and the MyShooterCharacter class is already placed in the MyBPShooter folder:

Figure 1.20 – The converted MyBPShooter C++ project

Figure 1.20 – The converted MyBPShooter C++ project

You may have noticed that some other files, such as MyBPShooterGameMode are missing, in comparison with the MyShooter project. That is because the Blueprint versions already exist, so the corresponding C++ versions are not automatically generated. You can choose to manually convert those blueprints to C++ classes only when necessary; otherwise, you just keep the blueprints.

 

Summary

In this chapter, we introduced C++ and the advantages of using it for professional game development. Then, you practiced creating the new MyShooter C++ project and converting the MyBPShooter Blueprint project to a C++ project. Plus, you also set up the development environment with VS and the C++ solution files.

In the next chapter, we will first walk through each part of the IDE’s user interface. Then, you will create a C++ project and practice writing some simple C++ code. Some code editing tricks will be introduced while editing your code.

About the Author
  • 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.

    Browse publications by this author
Unreal Engine 5 Game Development with C++ Scripting
Unlock this book and the full library FREE for 7 days
Start now