Reader small image

You're reading from  Game Development Patterns with Unreal Engine 5

Product typeBook
Published inJan 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781803243252
Edition1st Edition
Languages
Right arrow
Authors (2):
Stuart Butler
Stuart Butler
author image
Stuart Butler

Stuart Butler is an Unreal Engine Expert with over 13 years of experience in teaching Games Development in Higher Education. Stuart has published projects in a multitude of disciplines including Technical Design, Art, and Animation. Stuart is the Course Director for Games Technology at Staffordshire University, responsible for the programming team within the UK's largest Games Education Department. Stuart is also an Unreal Authorised Instructor and Educational Content Creator who works with Epic Games on developing learning materials for Unreal Engine 5. Stuart holds a BSc (Hons) in Computer Games Design and a PgC in Higher and Professional Education.
Read more about Stuart Butler

Tom Oliver
Tom Oliver
author image
Tom Oliver

Tom Oliver is a game programmer with over 10 years of experience in working with game engines both commercially and in an educational capacity. He has used Unreal Engine for contract work both in and out of the games industry, creating systems for games to mixed reality training simulations. Tom is the Course Leader for the BSc (Hons) Computer Games Design and Programming program at Staffordshire University, responsible for maintaining the award winning structure and teaching of the course in the UK's largest Games Education Department. Tom holds a BSc (Hons) in Computer Games Design and Programming and a PGc in Higher and Professional Education. Tom specialises in researching gameplay systems driven through mathematical phenomena.
Read more about Tom Oliver

View More author details
Right arrow

UE5 Patterns in Action – Double Buffer, Flyweight, and Spatial Partitioning

Hopefully, you will have realized by now that Unreal Engine 5 is a really big engine. Behind the scenes, it already employs a lot of the patterns that we will cover in later chapters. This chapter will break down the double buffer, flyweight, and spatial partitioning patterns. You don’t need to build these three patterns yourself as Unreal already has good implementations, but knowledge of their existence and how they have been created will help you build on top of them. This chapter will look into how Unreal implements each pattern into a system and what problems they are solving for you in the process. This should give you a roadmap to not only discover more about the engine but also some examples of good practice to reference moving forward.

In this chapter, we’re going to cover the following main topics:

  • Double buffer
  • Flyweight
  • Spatial partitioning

Technical requirements

In this chapter, we are going to delve into three patterns that are important to how any commercial engine performs. There will be some use of Big O notation, which is simply a low-resolution way of measuring the time efficiency of an algorithm. The lower the resulting number when replacing the n with a large number, such as 1,000, the better the time efficiency. For example, an algorithm that compares each element of an array with every other element of the same array could be described as O(n2). This comes from the idea that the algorithm is a couple of nested for loops that run for the length of the input data. Maybe then we improve efficiency, meaning we don’t need to recheck elements as we go through making the seconds for the loop shorter with each iteration. This would result in O(n log2n). Looking at these values, you can tell that for large numbers, O(n2) is far worse, giving an estimated cost of 1,000,000 executions for an array of size 1,000...

Double buffer

For this pattern, we need to imagine a photocopier being operated by an artist. The artist has been commissioned to deliver at least two copies of every picture they draw, so they think smart and use a photocopier to copy their work. To save time moving artwork from the easel to the copier, they simply put their canvas straight onto the scanner. They then paint as fast as they can and hit Copy at the same time. What follows is a race where the artist needs to paint each line fast enough to stay ahead of the scanning head. If they are successful, the artwork and the copy will look the same with no extra time taken. More than likely, the scanning head will get in front of the artist, which will result in a picture up until the point where the artist fell behind and a blank copy after that point. This is known as frame tearing, the issue we are trying to solve. Frame tearing occurs when the frame buffer, our example artist’s canvas, has not been fully updated by the...

Flyweight

The flyweight pattern is focused on reducing memory usage for large collections of objects and reducing the time spent loading them in. For the flyweight pattern, we first need to consider three things:

  • Intrinsic data – The data values that are immutable and considered to always be true on an object
  • Extrinsic data – The data values that are mutable and considered to be changeable per instance
  • Memory costs – All data must be stored somewhere and for loaded objects, that means on our RAM

With that in mind, let’s look at trees in the forest. Taking a simple approach, we could load in and store the model, texture, and transform data once for each tree. This would make our data storage look like Figure 3.4, with each tree connected to its own plot of memory, holding its copy of the data needed for rendering:

Figure 3.4 – Diagram showing the data associated with each tree

Figure 3.4 – Diagram showing the data associated with each tree

If we consider the...

Spatial partitioning

Imagine the Matrix is a real concept, and we are living in a giant physics simulation. Anybody who has dabbled in physics simulations knows that you don’t need many interacting objects to make the simulation chug. The naïve solution is to check every object against every other object leading to an O(n2-n) solution, where you can see in Figure 3.6 that 4 objects have 12 collision checks:

Figure 3.6 – Diagram showing each collision check performed in an inefficient collision detection solution

Figure 3.6 – Diagram showing each collision check performed in an inefficient collision detection solution

Improvements can obviously be made to not repeat calculations that have already been made, bringing us down to O(nLog2(n)) where those same four points use only six collision checks. This is better, but there is only so far we can go by checking every object against every other object. We could just not calculate some of them, but that would be like giving some people in our Matrix simulation wall hacks, allowing them to possibly...

Summary

In this chapter, we have discussed a number of existing patterns that are present in Unreal Engine, focusing on double buffer, flyweight, and spatial partitioning.

We have taken a look at some of these systems, including exploring editor examples of spatial partitioning using the World Partition system, experimenting with different variables to control when objects are loaded and unloaded, and testing an actor with a World Partition streaming source component.

In the next chapter, we are going to explore some pre-made patterns in Unreal. We will be looking at creating components using the update method, and creating a simple AI-controlled Real-Time Strategy (RTS) game unit using Unreal’s AI Behavior Trees.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Game Development Patterns with Unreal Engine 5
Published in: Jan 2024Publisher: PacktISBN-13: 9781803243252
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 $15.99/month. Cancel anytime

Authors (2)

author image
Stuart Butler

Stuart Butler is an Unreal Engine Expert with over 13 years of experience in teaching Games Development in Higher Education. Stuart has published projects in a multitude of disciplines including Technical Design, Art, and Animation. Stuart is the Course Director for Games Technology at Staffordshire University, responsible for the programming team within the UK's largest Games Education Department. Stuart is also an Unreal Authorised Instructor and Educational Content Creator who works with Epic Games on developing learning materials for Unreal Engine 5. Stuart holds a BSc (Hons) in Computer Games Design and a PgC in Higher and Professional Education.
Read more about Stuart Butler

author image
Tom Oliver

Tom Oliver is a game programmer with over 10 years of experience in working with game engines both commercially and in an educational capacity. He has used Unreal Engine for contract work both in and out of the games industry, creating systems for games to mixed reality training simulations. Tom is the Course Leader for the BSc (Hons) Computer Games Design and Programming program at Staffordshire University, responsible for maintaining the award winning structure and teaching of the course in the UK's largest Games Education Department. Tom holds a BSc (Hons) in Computer Games Design and Programming and a PGc in Higher and Professional Education. Tom specialises in researching gameplay systems driven through mathematical phenomena.
Read more about Tom Oliver