29,99 €
Have you ever wondered how to create engaging gameplay experiences that involve formidable AI opponents, capable of challenging and pushing players to their limits? If the answer is yes, then get ready to enter the realm of AI creation with Unreal Engine 5.
Within the pages of this book, written by a brilliant author and game development expert, you’ll find the secrets of Unreal Engine's cutting-edge AI framework. With this newfound knowledge, you’ll be able to create immersive and dynamic gaming experiences. This step-by-step guide will teach you the art of crafting intelligent and responsive virtual opponents that challenge and engage players on a whole new level. As you follow along with practical examples, the book will guide you through the creation of fully functional AI systems. You’ll be able to harness the power of behavior trees, NavMesh systems, and sensory perception models, breathing life into your virtual characters.
By the end of this book, you’ll be equipped with the knowledge you need to unleash the full potential of AI in Unreal Engine. Get ready to revolutionize your gaming creations and captivate players with AI-driven wonders that push the boundaries of what's possible!
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 379
Veröffentlichungsjahr: 2024
Artificial Intelligence in Unreal Engine 5
Unleash the power of AI for next-gen game development with UE5 by using Blueprints and C++
Marco Secchi
Copyright © 2024 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
The author acknowledges the use of cutting-edge AI, in this case you.com and Aria, Opera Browser’s AI, with the sole aim of enhancing the language and clarity within the book, thereby ensuring a smooth reading experience for readers. It’s important to note that the content itself has been crafted by the author and edited by a professional publishing team.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Rohit Rajkumar
Publishing Product Manager: Bhavya Rao
Book Project Manager: Arul Viveaun S
Senior Editor: Mark D’Souza
Technical Editor: K Bimala Singha
Copy Editor: Safis Editing
Proofreader: Mark D’Souza
Indexer: Tejal Soni
Production Designer: Ponraj Dhandapani
DevRel Marketing Coordinator: Nivedita Pandey
First published: October 2024
Production reference: 1151024
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK.
ISBN 978-1-83620-585-2
www.packtpub.com
To my nephews, Camila Mae Lynn Marcellina and Livio Siu Long, for, well... just being here.
To my students; you are the reason I keep on teaching.
And finally, to my adorable wife, Ambra – or should I say, “Professor Viktoria” – for helping me out with all the crazy ideas I come up with every day.
– Marco Secchi
Marco Secchi is a freelance game developer who graduated in computer engineering at the Polytechnic University of Milan. He is a lecturer and lead game advisor at Nuova Accademia di Belle Arti (NABA) where he also mentors BA students in their final thesis projects. In his spare time, he reads a lot, plays video games (less than he would like), and tries to practice CrossFit.
Syed Zaib Farooq is an accomplished entrepreneur and game developer with extensive experience specializing in gaming, VR, and immersive simulations. His expertise spans a range of industries, including real estate visualization, multiplayer gaming, metaverses, and EdTech simulations. Renowned for his proficiency in Unreal Engine, Zaib Farooq excels in developing custom plugins and utilizing both C++ and Blueprints. Driven by a passion for innovation, he consistently strives to create scalable gaming and simulation experiences that redefine immersion.
Nicholaus Price is a freelance game developer who has programmed in C++ since 2015 and in the Unreal Engine since UE3. His favorite genres to program are action, RPGs, platformers, and third-person shooters. Nick is currently an AI programmer for Project Sentinel. Artificial Intelligence in Unreal Engine 5 is the first book that Nick has reviewed.
In the first part of this book, you will receive a beginner-friendly introduction to the realm of artificial intelligence (AI) development in games. Once you have a solid understanding of its key concepts, you will be ready to start implementing a project leveraging these topics.
This part includes the following chapters:
Chapter 1, Getting Started with AI Game DevelopmentChapter 2, Introducing the Unreal Engine AI SystemWelcome to the fascinating world of artificial intelligence (AI) development in Unreal Engine! I am thrilled that you have chosen me and my book as your guide on this sometimes intimidating journey into the realm of AI programming. Rest assured that I am committed to making this experience as easy and enjoyable as possible.
Throughout this book, you will acquire the skills required to create Unreal Engine games that involve the use of AI techniques and learn how to handle them at runtime. We will start from the basics, such as moving agents within a game level, and gradually progress to more advanced topics such as creating complex behaviors and managing multiple AI entities (even dozens or hundreds). By the end of this journey, you will be proficient in crafting formidable opponents that will challenge your players; what’s more, you will possess a deep understanding of the potential pitfalls in AI development and how to avoid them.
In this chapter, I will introduce you to some basic keywords about AI development; these concepts will serve as a gentle introduction to the whole book, providing you with a foundation to delve deeper into the fascinating world of AI programming in Unreal Engine.
In this chapter, we will be covering the following topics:
Introducing AIUnderstanding AI in game developmentExplaining AI techniques in video gamesI guess you are already aware that the Unreal Engine Editor can be quite demanding in terms of hardware prerequisites. However, there is no need to be intimidated as this book primarily focuses on game programming rather than real-time visual effects.
In this section, we will explore the hardware and software requirements necessary to follow along with this book. Additionally, we will discuss some prerequisite knowledge that will be beneficial for your journey.
Before we dive into the exciting world of AI in game development, I want to kindly remind you that this book is designed for individuals who already possess some knowledge about working with Unreal Engine. Therefore, you must be already familiar with the following topics:
Unreal Engine: It’s essential to have a basic understanding of this software interface, tools, and workflow.Game development basics: Having a good grasp of general game development principles and terminology will greatly aid your understanding of the concepts discussed in this book.Programming knowledge: As this book focuses on game development, it is assumed that you have some programming experience. Ideally, you should be familiar at least with the Unreal Engine visual scripting system (Blueprints) and, to some extent, with C++.Note
If you are new to Unreal Engine, I highly recommend exploring some introductory books or resources to familiarize yourself with its fundamentals. One amazing starting point is Blueprints Visual Scripting for Unreal Engine 5 by Marcos Romero, Packt Publishing, which will guide you through the main features of programming in Unreal Engine with Blueprints.
In this book, and whenever possible, I will be showing you techniques by using both Blueprints and C++. If you need a gentle introduction to C++, at the end of this book, you will find a valuable appendix that delves into the intricacies of C++ programming in the context of Unreal Engine. This quick guide is also designed to provide you with some understanding of how C++ works within the Unreal Engine framework.
At the time of writing this book, Epic Games is officially recommending the following basic requirements. If your hardware meets at least these specifications, you can expect to have a pleasant experience while reading through the chapters:
Windows OS:Operating system: Windows 10 or 11 64-bit versionProcessor: Quad-core Intel or AMD, 2.5 GHz or fasterMemory: 8 GB RAMGraphics card: DirectX 11- or 12-compatible graphics cardLinux:Operating system: Ubuntu 22.04Processor: Quad-core Intel or AMD, 2.5 GHz or fasterMemory: 32 GB RAMVideo card: NVIDIA GeForce 960 GTX or higher with the latest NVIDIA binary driversVideo RAM: 8 GB or moremacOS:Operating system: Latest macOS VenturaProcessor: Quad-core Intel, 2.5 GHzMemory: 8 GB RAMVideo card: Metal 1.2-compatible graphics cardI've written this book using the following hardware:
Desktop:Operating system: Windows 10 64-bit versionProcessor: Intel Core i9 9900KMemory: 64 GB RAMGraphics card: NVIDIA GeForce RTX 3090tiLaptop:Operating system: Windows 11 64-bit versionProcessor: Intel Core i7 13650HXMemory: 8 GB RAMGraphics card: NVIDIA GeForce RTX 4060This book assumes you have the Epic Games Launcher and Unreal Engine 5 installed and fully working on your computer.
Note
At the time of writing this book, the latest version of Unreal Engine is 5.4 but you will be able to follow along with any version more recent than 5.4.
Additionally, as we will also be working with C++, you’ll need an IDE supporting this language and Unreal Engine. If you already have some experience, chances are you have already installed Visual Studio 2019/2022 or JetBrains Rider; if you don’t, you will need to install one of them to follow along with the C++ coding parts.
Once you have Visual Studio installed, you’ll need the following extra components to make it work properly with Unreal Engine:
C++ profiling toolsC++ AddressSanitizerWindows 10 SDKUnreal Engine installerTo include these tools, follow these steps:
Open Visual Studio Installer.Select Modify from your Visual Studio installation, selecting the version you will be using:Figure 1.1 – Selecting the Visual Studio Installer version
Once the Modifying modal window opens, in the top bar, make sure you are in the Workloads section.Then, activate the Game development with C++ option by clicking the checkmark.Next, if it is closed, open Installation details | Game development with C++ | Optional from the right sidebar.Select the following fields, as shown in Figure 1.2:C++ profiling toolsThe latest Windows 11 SDKversion availableC++ AddressSanitizerIDE support for Unreal Engine (optional)Unreal Engine installerFigure 1.2 – The Workload section
Click the Install while downloading button (or the Download all, then install one) to start the installation process.Note
The IDE support for Unreal Engine integration is an extension introduced in Visual Studio 2022 and adds some nifty features such as Blueprint references, Blueprint assets, and CodeLens hints on top of the Unreal Engine classes, functions, and properties. Although not mandatory, I highly recommend using it as it will make your life as a developer much easier!
After completing the download and installation process, you will be fully prepared to embark on the development of your own C++ games using Unreal Engine.
Now that you have successfully set up your system, it is time to get acquainted with some of the key terminologies in the AI environment. This will provide you with a solid foundation to understand and navigate the world of AI more effectively.
AI has emerged as a transformative force in various industries; in its broadest sense, AI involves the simulation of human intelligence in machines that are programmed to think and (sometimes) learn like humans.
As such, developing AI means studying methods and software that enable machines to perceive their environment, learn from data, reason, and make decisions to achieve defined goals.
AI encompasses various subfields and applications, including the following:
Robotics: The development of intelligent machines that can interact with the physical worldNatural language processing: The ability of computers to understand, interpret, and generate human languageMachine learning: The use of algorithms and statistical models to enable computers to learn from data and make predictions or decisions without being explicitly programmedDeep learning: A branch of machine learning that leverages neural networks to emulate decision-making abilities observed in the human brainComputer vision: The ability of computers to understand and interpret visual information from images or videosWhat’s more, AI has been making significant strides in the entertainment industry, transforming the way content is created, consumed, and personalized. Here are some key points about AI’s impact on the entertainment industry:
Generative AI: This kind of technology can create outputs such as stories, scripts, and images and has the potential to revolutionize content creation in the entertainment industry.Personalized recommendations: AI-enabled tools are being used to help users discover content tailored to their preferences by providing personalized suggestions. These recommendations are based on user behavior, viewing history, and other data, enhancing the user experience.Data-driven insights: By analyzing user behavior, AI allows the entertainment industry to gain valuable insights from data such as preferences and trends and can help companies understand their audiences better and make data-driven decisions regarding content production, distribution, and marketing.However, as the age-old adage suggests, “With great power comes great responsibility.” The adoption of AI brings forth many challenges and considerations. Discussions surrounding intellectual property and copyright matters pertaining to AI-generated content have taken place and still do. Furthermore, there is growing concern about the impact of AI on employment within various industries, as certain roles may face disruption or transformation due to the advancements in AI technologies.
Ethics in utilizing AI is an essential subject that delves into the moral considerations and implications linked to the utilization of these systems. As AI technology continues to progress at a faster and faster pace, it evokes profound ethical concerns regarding its effects on society, individuals, and the environment.
While it cannot be denied that AI has the potential to enhance efficiency and productivity, it may also result in job losses within specific sectors; as such, some people consider it mandatory to implement measures that alleviate adverse effects on workers and society.
You have just been introduced to some of the most common terminology about AI in general. Now, let’s shift our focus to understanding how AI works specifically in the realm of game development. In this next section, we will delve into its fundamentals, providing you with a basic understanding of its principles and workings.
When applied to game development, AI is employed to create intelligent systems capable of performing tasks without explicit programming. These systems adapt and improve their performance based on experience, enhancing the overall gaming experience. For instance, game characters have been imbued with AI for many years, enabling them to exhibit seemingly intelligent behavior. Even the four iconic Pac-Man ghosts have been programmed with unique and distinct behaviors!
AI in game development extends far beyond the control of non-playable characters (NPCs) or enemies. It encompasses a diverse range of applications that revolutionize game design, development, and player experience. By leveraging AI, game developers can introduce innovative and immersive gameplay elements that will captivate players during gameplay.
If you have picked up this book, chances are you are eager to grasp the fundamental principles of AI programming in games and apply this knowledge to create your next big hit. As an AI programmer, you will have the power to craft stunning opponents, create NPCs that will help players achieve their goals, or simply invent new and engaging behaviors that will make your games enjoyable to the next level; this is going to be an immensely rewarding endeavor!
However, it is important to note that AI video game programming can present significant challenges, demanding long hours and potentially inducing stress. Being aware of these potential pitfalls is crucial before embarking on this career path. To avoid such setbacks, it is essential to develop a solid understanding of how AI functions, enabling players to have a seamless and enjoyable gaming experience. What’s more, comprehending this subject also entails troubleshooting computer issues that may arise and effectively resolving them. Rest assured, these issues will inevitably surface sooner or later!
In the following section, you will receive a gentle introduction to the major AI techniques used in games, along with the distinctive features that set them apart.
AI plays a pivotal role in enhancing gaming experiences, making them more immersive and exciting. Therefore, it is crucial to have a comprehensive understanding of the underlying principles behind AI development and how they function. This knowledge will empower game developers to effectively harness AI’s potential, creating rich and captivating gameplay that keeps players engaged and enthralled.
Just think about the Assassin’s Creed series, which is known for its open-world gameplay, where complex AI behaviors are used to control NPCs. On a more advanced level, games such as Counter-Strike introduced AI-controlled player characters – called bots – that can be created and managed to stand in place of real players.
Finally, the future of AI in game development holds exciting possibilities and innovations as AI is being used to create dynamic and adaptive narratives in games. By observing player behavior and preferences, AI algorithms can construct narrative branches, challenges, and rewards tailored uniquely to each player.
In this section, I will give a brief and non-exhaustive overview of AI techniques that are commonly used in games. In this book, you’ll get the chance to explore some of these techniques and see how they are used in Unreal Engine. For those techniques that won’t be covered in this book, there will be plenty of opportunities for you to explore and delve into them on your own. The world of AI in gaming is vast and ever-evolving, offering endless possibilities for experimentation and innovation. So, don’t be discouraged if a particular technique is not covered here – the journey of discovery continues, and there are countless resources available to help you unlock new horizons in AI game development.
Pathfinding is essential for efficient navigation in game environments and refers to the process of determining the optimal path while simulating the movement from one point to another. It can be used by autonomous agents, such as NPCs or opponents, but it is also useful in point-and-click games, where your character needs to reach a specific location. Pathfinding involves finding the optimal path from one location to another while avoiding obstacles; algorithms such as A* are commonly used in these situations. NPCs can use this technique to plan their movement, whether to avoid enemy units, find shortcuts, or follow waypoints.
One of the most common pathfinding techniques in game development is achieved by using a navigation mesh – or nav mesh, which is a data structure that represents the walkable surfaces of a level. Figure 1.3 shows an example of AI movement through a nav mesh:
Figure 1.3 – AI movement through a navigation mesh
Rule-based systems refer to a type of AI that operates based on a set of predefined rules. These rules are coded by humans and dictate the behavior and decision-making of the system itself; this means following the rules to produce predetermined outcomes based on some kind of input. Put simply, these rules are commonly referred to as if statements because they typically adhere to the structure of if something is true, then do something else. Although limited, these systems are relatively easy to implement and manage because the knowledge encoded in the rules is modular, and the rules can be coded in any order. This provides much flexibility in both coding and modifying the system.
Finite state machines (FSMs) are a common technique used in AI development and they involve breaking down an opponent’s or NPC’s behavior into different states, where each state represents a specific behavior or action. Transitions between states are triggered when certain conditions or events are satisfied. For instance, a sentinel character may have states such as patrol, alert, or chase, with transitions occurring when the character has made some noise or has been spotted because they are in the line of sight. FSMs provide a clear and organized way to control NPC behavior, especially in games with predefined sequences of actions.
Figure 1.4 shows an example of a simple FSM with states and conditions:
Figure 1.4 – A finite state machine
Behavior trees are hierarchical structures used to control AI behavior. They consist of nodes representing specific actions or conditions. The tree structure allows for the sequencing of actions and decision-making based on the conditions themselves. The system will traverse the tree from the root to the leaf nodes, executing actions or evaluating conditions along the way. Behavior trees provide a flexible and modular approach to NPC behavior, allowing for complex and dynamic decision-making. A behavior tree can include nodes such as selectors, sequences, conditions, or action nodes. Figure 1.5 shows a behavior tree where a selector decides which part of the tree will execute, and sequence nodes will perform a list of tasks in a predefined order.
Figure 1.5 – A behavior tree
If you are unfamiliar with these terms, have no fear! I’ll be explaining them in Chapter 7, Introducing Behavior Trees.
Machine learning involves training AI models using data and algorithms to enable NPCs to learn and improve their behavior over time. This technique allows NPCs to adapt, make decisions, and respond to unpredictable situations based on patterns and experiences from previous gameplay. Machine learning can provide more dynamic, realistic, and engaging interactions with NPCs, as their behavior evolves through iterations and learning from player actions.
One example of a game that uses machine learning is AlphaGo (https://deepmind.google/technologies/alphago/), developed by DeepMind. AlphaGo is an AI system that mastered the ancient Chinese game of Go by using machine learning techniques to calculate probabilities and make strategic decisions in the game.
Reinforcement learning is a type of machine learning system where NPCs learn through trial and error, receiving feedback or rewards based on their actions. NPCs explore the game environment, take action, and learn from the consequences. Reinforcement learning enables NPCs to optimize their behavior by maximizing rewards and minimizing penalties. This technique can result in NPCs that exhibit adaptive and strategic decision-making, enhancing the challenge and immersion of the game. Reinforcement learning is typically employed during the development process to create a functional system by the time a game is released. Due to the nature of reinforcement learning, sometimes results may not be as expected, and NPCs may exhibit weird or erratic behavior.
The aforementioned generative AI is being increasingly used in video game development, offering new possibilities and transforming various aspects of game development. Some of these aspects involve creating more realistic NPCs whose behaviors go beyond fixed patterns and making decision-making systems more adaptative and engaging for players.
Although generative AI is still at its initial stages in game development and its full potential is still to be explored, it is already demonstrating promising capabilities to transform various aspects of the game industry.
In this chapter, we explored the fundamental principles of AI development and saw how it is applied in the game industry. In the upcoming chapter, I’ll introduce you to the incredible potential that Unreal Engine offers and how its framework can be leveraged to create intelligent and immersive AI in games. Brace yourself for a thrilling exploration of its possibilities and let’s dive into the fascinating realm of AI game programming with your favorite game engine!
The examples in this chapter were created with the help of Basic Miscellany Lineal icons from Flaticon (https://www.flaticon.com/).
Welcome to the exciting world of AI programming with Unreal Engine! In this chapter, I’ll be introducing you to Unreal Engine’s powerful tools that will bring life and intelligence to your virtual worlds. By exploring various aspects of the Unreal Engine AI system, such as moving agents using the Navigation System, implementing semi-intelligent behaviors through behavior trees and Blackboards, and incorporating features such as smart objects and mass entities, you will gain a comprehensive understanding of the remarkable capabilities offered by this robust framework.
Mastering these skills will elevate you to the ranks of elite game programmers – and who wouldn’t want to be one of those?
By the end of this chapter, you will have a sharp vision of what can be accomplished using the Unreal Engine AI system, empowering you to create advanced AI pawns in your projects.
In this chapter, we will be covering the following topics:
Getting to know the Unreal Engine Gameplay FrameworkPresenting the Unreal Engine AI systemUnderstanding advanced AI featuresThere are no technical requirements to follow for this chapter.
As you may already know, Unreal Engine provides an out-of-the-box system called Gameplay Framework (GF) that includes many features necessary for developing a game; this spans from having an advanced input system to common entry points that will allow you to easily access data or game state.
Here are some key points explaining why the GF is so important:
Structure and organization: The GF provides a structured and organized approach to developing games. It offers a collection of systems, classes, and interfaces that work together to create the core structure of a game.Game logic and progression: This framework includes predefined concepts that help define the logic, progression, and organization of a game.Player and AI control: The GF includes systems for handling player input and decision-making for characters within the game world. This encompasses player and AI control, which are essential for creating interactive and immersive gaming experiences.Utility functions: The framework provides a library of utility functions that assist with common gameplay operations and interactions. These functions can streamline gameplay logic and enhance efficiency in implementing various functionalities.Flexibility and integration: The GF is highly flexible and integrates deeply with the Unreal Engine. It uses common game programming patterns and performs heavy lifting, allowing developers to focus on building their games rather than creating their own game framework.As a personal reflection, I have found that using and comprehending the GF over the years has significantly enhanced my overall understanding of game programming best practices.
Quite obviously, managing an AI system is also part of the GF job, so, in the next subsections, I will provide you with a concise introduction to the key AI features available in the GF, enabling you to be prepared for their use.
I’m pretty sure you’re already familiar with actors and components in Unreal Engine, but just in case, let’s do a quick refresher on both of them.
In Unreal Engine, an Actor class refers to any entity that can be placed within a level, whether it’s a camera, a static mesh, or the player’s character. An actor can undergo transformations such as translation, rotation, and scaling.
Actors serve as containers for specialized classes known as components that play various roles in controlling a movement, rendering, and more. There are three types of components that serve different purposes within an actor:
Actor components: These primarily contain code logic for an actor. They handle various functionalities and interactions without any visual representation.Scene components: These are used to position and orient other components within the actor. They serve as reference points for transformations such as translation, rotation, and scaling but do not have any visible presence and are mainly used for organizational purposes.Primitive components: These are responsible for the visual representation of an actor within a level. They can be rendered and interacted with by players or other objects.By combining these components, a game developer can create complex and interactive actors with both functional and visual aspects.
The Unreal Engine GF is a comprehensive collection of classes that serves as a modular foundation for constructing gameplay experiences. Within this framework, game developers have the freedom to handpick specific elements that best suit the game, while being assured that these classes are intricately designed to seamlessly work together and enhance one another.
In the upcoming subsections, we will present the main elements involved to have a clear view of how things work.
The GameInstance class serves as a manager that operates behind the scenes (i.e., it is not an Unreal Engine actor); a single instance is created when the engine launches and the instance remains active until the engine shuts down. Its primary purpose is to track data and execute code as needed.
A game instance provides a handy central hub for managing persistent data, such as save game systems, and acts as a manager for other subsystems, offering convenient control over the flow of your game.
Different from the GameInstance class, the GameModeBase or its direct descendant, GameMode, instance only exists in a single level and is created right after the level itself has been loaded and the world has been constructed. This class serves as a manager to handle a gameplay session, and each level can have its own different game mode logic. Its main role is to create the remaining framework actors.
GameState and PlayerState are specialized actors that play a key role in tracking the state of the game and the players involved. The game state is responsible for storing and handling data pertinent to all players in a game, while the player state focuses on a specific player. Given their inherent characteristics, these classes find their primary application in multiplayer games, regardless of whether they are played online or locally.
A pawn refers to the base class of all actors that can be controlled by players or AI entities within the game world. It serves as the physical representation of an entity, handling its involvement within the game world, including collisions and other physical interactions. It is also usually used to determine the visual appearance of an entity.
The Pawn class gains additional functionality through the more advanced Character class. The character class is specifically designed to represent players in a vertically oriented manner, enabling them to perform a wide range of actions such as walking, running, jumping, and swimming within a level. As a side note, the character class incorporates essential features for multiplayer handling.
The Controller class is responsible for governing the logic that determines a player’s actions within the game world. Two widely used types of controller classes are PlayerController and AIController; the second option is something we eagerly anticipate, for