The Shoulders of Giants: LensManagers

This is part 1 in a hopefully-ongoing series about videogame programming techniques I’ve acquired over the past several years. Broadly, it’s intended to help you solve common problems you encounter in your daily game development life. Yet rather than merely throwing a github or Asset Store link at you, my goal is to present a comprehensive programming style: how to think about the problem, why to think about it this way, and ultimately how to attack similar problems with valour and grace.

I once read a Gamasutra post describing something called ‘The Curtain Problem’. Here’s the basic idea: you must program a big Broadway-style stage curtain for the videogame you’re creating. You’ll use this curtain firstly as a loading screen: closing it whenever it’s time to unload the previous scene, then reopening it once the new scene has loaded. Yet your level designer is musing about reusing this curtain for dramatic effect at various points during the middle of gameplay. (Your antagonist, it turns out, is an underground R&B idol called ‘The Bleaknd’ who changes outfits often; it would be cool if each outfit swap caused the curtain to close and reopen around him.)

Had you brought this problem to an entry-level programming class, you’d inevitably find yourself staring at a projector slide with the following text on it:

The very first thing universities teach us about object-oriented programming is that this variety of public member access is, for some reason, sinful. Yet it’s difficult to explain to a novice programmer precisely why this is the case. In the land of C# and Unity3d, the first thing your professor would do is show you the ole’ getter & setter pattern:

It’s apparent to the student that this code snippet is functionally identical to the previous one, except that it’s many lines longer and, therefore, much harder to write correctly on the back of the handwritten exam Your professor is preparing to give you. They explain that this technique entitles you to a fresh and steamy mound of OOP shit™, which smells strongly of a thing called ‘encapsulation’ (best remember this one for your first job interview). The two of you soon arrive at something like:

This snippet, unlike the previous one, rises above complete pointlessness. It’s sort of neat that you can tell ‘Stage’ to open or close the curtain without needing to know what CurtainController is or what it’s doing. It’s sort of helpful that CurtainController is inaccessible from outside the Stage, and therefore has only one thing feeding it commands. You could probably reuse CurtainController somewhere else—perhaps as part of some other class—though you’ll probably never need to.

Inevitably, the very next problem you’ll encounter shall concern the sharing of control. What if your antagonist is frenemies with a local rap superstar (notorious, it turns out, for ‘runnin through the Styx with his ghosts’)? What if both characters need to open and close the curtain at arbitrary intervals? What if there were four characters, or six, or twenty?

Here we come to realize that the boolean parameter with which we’ve been working probably needs to be more like a retain counter:

Two weeks later, the problem will inevitably become more complicated than we thought. We’ll need the curtain to open and close at different speeds based on a variety of different contextual factors; sometimes we’ll need it to pause partway, framing one important character on the stage; and so on, and so on. A retain counter is no longer enough; we somehow need to wrangle many potential curtain configurations all at once, appending and removing and making sense of them all on the fly. Here’s about as far as we’ll make it before collapsing in despair:

As the arrival of each new feature brings ruin down upon our treasured OOP shit™, we shall be forced into endless refactoring. As professional programmers we are destined to encounter problems of this same essential form over and over again: no less than twice a week, for as many years as we can bear it.

Rudimentary object-oriented design is mostly incapable of addressing problems of this variety. It’s all about hierarchies of control: everything must have a boss, and everything’s boss must have a boss. Objects within objects within objects, pushing instructions down: wrappers upon wrappers upon wrappers, pulling information up. This is a passable approach for business transactions, maybe, but it’s next to useless for creating videogames. In games our requirements remain fluid, and our solutions are always temporary; any class hierarchy we could devise will inevitably crumble before an onslaught of incremental design shifts. And furthermore, the vast majority of our medium’s traditions call for parallelism rather than vertical chains of command: the more everything in the game world is able to affect everything else, the more space we create for doing design work. Software like ours demands a different kind of programming style. Regrettably, it’s a style few institutions ever teach.

In my experience game programming is not so much about encapsulation, data hiding or any of that rudimentary OOP shit™; those things can be nice, but they scarcely scratch the surface of the problem. More than anything, game programming is about distributing control such that the software won’t implode the moment anything unexpected happens. To achieve this we must do away with all the hidden assumptions underlying traditional programming dogma. We typically assume that our objects should have bosses within bosses; that to call a function is to issue marching orders down a chain of command. We typically assume that the objects on and around our Broadway stage (our antagonist, for example, or our scene changing subroutine) possess a reference to the curtain and, therefore, must tell it precisely how to behave. But what if we treated the curtain as a collaborator, rather than a possession? What if we told it what we wanted, rather than issuing commands for it to follow? What if we decoupled the notion of an outcome from the notion of a request?

Suppose any game entity, anywhere in the world, could notify the stage a) when it is interested in the curtain closing itself and b) when it is no longer interested in the curtain at all. We phrase this as any number of entities requesting and subsequently disposing any number of ‘Lenses’ from a centralized ‘LensManager’ located within the stage. ‘Lenses’, for our purposes, are small tokens that each exert a temporary (in that they can be requested, held, then disposed) and anonymous (in that they do not need to know about or reason with one another) influence on our stage’s behaviour:

The only entity that needs to know about every currently-existing Lens is the stage, which remains free to decide for itself how these Lenses should influence its behaviour. The simplest case we can imagine, analogous to our retain counter code, is: ‘If any Lenses currently exist, the curtain’s state is closed’:

But the beauty of LensManagers is that we can easily extend them to represent any arbitrary information the problem might require. I’ve prepared an example in Unity, featuring a big Broadway Stage and several instances of our favourite underground R&B Star. This is what the code looks like:

Here is, in my opinion, the apotheosis of what we began with at the top of this post. It permits any number of game elements to interact with it using a simple, easily-adjustable ruleset that handles all potential collisions between all our game elements. It’s easy to extend or contract, should we require more or less from it. It is as loosely-coupled as I can imagine; we can drop it directly into some other game project and immediately start doing very complicated things without having to change a line of code. It works well across update loops, coroutines and high-efficiency event-driven systems. It works well for creating and destroying game objects, or on the enabling/disabling of individual components. Level designers love having the ability to layer complicated mechanical effects atop one another; gameplay programmers love having tangible references to these complicated effects.

I’ve come to use LensManagers for almost any game feature that requires shared control. Imagine a camera system where any number of game elements may ask to frame something and the camera automatically tweens towards the most important one. Imagine a stats controller that buffs and/or debuffs the player’s attributes any time you enable/disable any number of Unity components anywhere in your scene. Imagine a physics controller that stacks intensifiers and weakeners of the player’s frictional constants as she travels through any number of overlapping trigger areas. I’ve written all these things using lenses. (The physics one took me ten minutes flat.)

I believe this is the very best way to permit volatile state mutations between loosely-coupled software systems (which, in videogames, is one of thte great Holy Grails). LensManagers will save you time! They will preserve the hairs on your scalp! They will improve your game development life! Give them a shot.

You can download my example unity project here.

(This post was informed by the aforementioned ‘Curtain Problem’ piece by Jamie Fristrom, as well as this brilliant blog post by Craig Gidney laying out the fundamental programming technique.)

Leave a Reply