I have just finished reading the excellent book “Game Programming Patterns”, which you can read for free at Table of Contents · Game Programming Patterns
The book gives several Unity examples. As a learning exercise I thought I would apply the content of the book to Unreal engine.
Let me start with a very brief summary of the patterns covered by the book:
1.Design patterns revisited
Command Express actions for actors as classes.
Flyweight Separate intrinsic (context-free) and extrinsic (unique to this instance) data to optimize memory.
Observer An object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes.
Prototype Ability to clone objects.
Singleton Singletons harm the code because they are global, making harder to follow the code, encouraging coupling, no concurrent-friendly implementations.
State Finite State Machines
2. Sequencing patterns
Double buffer Cause a series of sequential operations to appear instantaneous or simultaneous.
Game loop Decouple progression of game from user input and processor speed.
Update Method Simulate a collection of independent objects by telling each other to process one frame of behavior at a time.
3. Behavioural patterns
Bytecode Give behavior the flexibility of data by encoding it as instructions for a virtual machine.
Subclass Sandbox Define behavior in a subclass using a set of operations provided by its base class.
Type Object Allow the flexible creation of new “classes” by creating a single class, each instance of which represents a different type of object.
4. Decoupling Patterns
Component Allow a single entity to span multiple domains without coupling the domains to each other.
Event queue Decouple when a message or event is sent from when it is processed.
Service Locator Provide a global point of access to a service without coupling users to the concrete class that implements it.
5. Optimization Patterns
Data Locality Accelerate memory access by arranging data to take advantage of CPU caching.
Dirty flag Avoid unnecessary work by deferring it until the result is needed.
Object Pool Improve performance and memory use by reusing objects from a fixed pool instead of allocating and freeing them individually.
Spatial partitions Efficiently locate objects by storing them in a data structure organized by their positions.
I have identified the following Unreal tools to implement design patterns: TFunctions, delegates, TSubclassOf, Actor and ActorComponents, Blueprints.
What have I missed?
- TFunctions can store lambda functions to implement the Command pattern.
- Udata assets help sharing data by several instances (as the Flyweight pattern indicates)
- Unreal delegates help organising the code as the Observer pattern suggests. They also decouple code by processing Event queues.
- TSubclassOf is brilliant. They change programming style to data oriented, as in the Prototype, Type Object Patterns
- The engine has a game loop as described in https://docs.unrealengine.com/en-US/Gameplay/Framework/GameFlow
- Every Actor and ActorComponent have a Tick function, following the Update Method pattern.
- Blueprints are a very fast way to store game logic and iterate gameplay mechanics, as the Bytecode pattern suggests. Also, blueprints can derive from a C++ base class and, making use of UPROPERTIES and UFUNCTIONS, as the Subclass Sandbox pattern.
- Unreal engine architecture provide Game Instance, Game State or Player State as central places where to add managers (service locators).
- Finite State Machines are supported by AI blackboards.
- Singletons cannot be implemented in Unreal. But we know that’s a good thing and we can add managers to the Game Instance class.
- Optimizing: I want to think Unreal stores contiguous Component arrays (data locality). I’m not sure if there is any support for dirty flag, object pool or spatial partitions?
Thanks for reading, let me know your thoughts!