Context/Virtual Self
Sharing state data between multiple objects, either to implement a finite state machine or to compose multiple objects to perform a particular task.
Sharing state data between multiple objects, either to implement a finite state machine or to compose multiple objects to perform a particular task.
[size="5"]Problem
Sharing data between multiple objects that have only one instance is pretty easy. Global data or singletons are easy solutions. However, if you want to compose discrete objects into a single component, any sort of global access pattern fails to scale to a finer level.
[size="5"]Solution
Create an object Context that will hold any state data and methods relevant to the operation of the component. Add this object as a parameter or member pointer to every class the component will create and use. Have the component create and own the Context object. Using Context as an extra parameter provides more flexibility with respect to usage, as it allows multiple components to rely on a single object for part of their implementation.
[size="5"]Structure
See the State Pattern in Design Patterns for an implementation of the Context pattern.
[size="5"]Consequences
- Easier development of components that are a composition of smaller objects.
- Extra overhead, either as an extra parameter to every method call or an extra member pointer.
- Using the Context object as an extra parameter allows stateless objects (i.e. the Context object contains all relevant state information) to be implemented as Flyweights, obviating the need for multiple instances of lightweight objects.
[size="5"]Related Patterns
State uses a Context to pass state information between various states. Context can be thought of as a Singleton with finer levels of access beyond global.
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Mat Noguchi
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion