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
Express actions for actors as classes.
Separate intrinsic (context-free) and extrinsic (unique to this instance) data to optimize memory.
An object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes.
Ability to clone objects.
Singletons harm the code because they are global, making harder to follow the code, encouraging coupling, no concurrent-friendly implementations.
Finite State Machines
2. Sequencing patterns
Cause a series of sequential operations to appear instantaneous or simultaneous.
Decouple progression of game from user input and processor speed.
Simulate a collection of independent objects by telling each other to process one frame of behavior at a time.
3. Behavioural patterns
Give behavior the flexibility of data by encoding it as instructions for a virtual machine.
Define behavior in a subclass using a set of operations provided by its base class.
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
Allow a single entity to span multiple domains without coupling the domains to each other.
Decouple when a message or event is sent from when it is processed.
Provide a global point of access to a service without coupling users to the concrete class that implements it.
5. Optimization Patterns
Accelerate memory access by arranging data to take advantage of CPU caching.
Avoid unnecessary work by deferring it until the result is needed.
Improve performance and memory use by reusing objects from a fixed pool instead of allocating and freeing them individually.
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
- 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
Game Flow Overview | Unreal Engine Documentation
- 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!