Skip to content

Commit 5bb7b6f

Browse files
committed
Merge branch 'laurenbee-master'
2 parents 7f0255e + dc95eca commit 5bb7b6f

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

book/update-method.markdown

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The player's mighty valkyrie is on a quest to steal glorious jewels from where
1212
they rest on the bones of the long-dead sorcerer-king. She tentatively
1313
approaches the entrance of his magnificent crypt and is attacked by...
1414
*nothing*. No cursed statues shooting lightning at her. No undead warriors
15-
patrolling the entrance. She just walks right in grabs the loot. Game over. You
15+
patrolling the entrance. She just walks right in and grabs the loot. Game over. You
1616
win.
1717

1818
Well, that won't do.
@@ -81,7 +81,7 @@ problem.
8181
</aside>
8282

8383
The pattern we'll use to fix this is so simple you probably have it in mind
84-
already: *Each entity in the game should encapsulate its own behavior.* This
84+
already: *each entity in the game should encapsulate its own behavior.* This
8585
will keep the game loop uncluttered and make it easy to add and remove entities.
8686

8787
To do this, we need an *abstraction layer*, and we create that by defining an
@@ -117,9 +117,9 @@ frame, the game updates every object in the collection.
117117
## When to Use It
118118

119119
If the <a href="game-loop.html" class="pattern">Game Loop</a> pattern is the
120-
best thing since sliced bread, then this pattern is its butter. A wide swath of
120+
best thing since sliced bread, then the Update Method pattern is its butter. A wide swath of
121121
games featuring live entities that the player interacts with use this pattern in
122-
some form or other. If the game has space marines, dragons, martians, ghosts, or
122+
some form or other. If the game has space marines, dragons, Martians, ghosts, or
123123
athletes, there's a good chance it uses this pattern.
124124

125125
However, if the game is more abstract and the moving pieces are less like living
@@ -130,7 +130,7 @@ themselves every frame.
130130

131131
<aside name="pawn">
132132

133-
You may not need to update their *behavior* each frame, but even in a boardgame,
133+
You may not need to update their *behavior* each frame, but even in a board game,
134134
you may still want to update their *animation* every frame. This pattern can
135135
help with that too.
136136

@@ -146,7 +146,7 @@ Update methods work well when:
146146

147147
## Keep in Mind
148148

149-
This pattern is pretty simple so there aren't a lot of hidden surprises in its
149+
This pattern is pretty simple, so there aren't a lot of hidden surprises in its
150150
dark corners. Still, every line of code has its ramifications.
151151

152152
### Splitting code into single frame slices makes it more complex
@@ -156,19 +156,19 @@ complex. Both simply make the skeleton guard walk back and forth, but the second
156156
one does this while yielding control to the game loop each frame.
157157

158158
That change is <span name="coroutine">almost</span> always necessary to handle
159-
user input, rendering and the other stuff that the game loop takes care of, so
160-
the first example wasn't very practical. But it's worth keeping in mind there's
159+
user input, rendering, and the other stuff that the game loop takes care of, so
160+
the first example wasn't very practical. But it's worth keeping in mind that there's
161161
a big up front complexity cost when you julienne your behavioral code like this.
162162

163163
<aside name="coroutine">
164164

165165
I say "almost" here because sometimes you can have your cake and eat it too. You
166166
can have straight-line code that never returns for your object behavior, while
167-
simultaneously having a number of them running concurrently and coordinating
167+
simultaneously having a number of objects running concurrently and coordinating
168168
with the game loop.
169169

170170
What you need is a system that lets you have multiple "threads" of execution
171-
going on at the same time. If the code for an object can just pause and resume
171+
going on at the same time. If the code for an object can pause and resume
172172
in the middle of what it's doing, instead of having to *return* completely, you
173173
can write it in a more imperative form.
174174

@@ -189,7 +189,7 @@ currently executing.
189189

190190
When we changed this to a one-frame-at-a-time form, we had to create a
191191
`patrollingLeft` variable to track that. When we return out of the code, the
192-
execution position is lost so we need to explicitly store enough information to
192+
execution position is lost, so we need to explicitly store enough information to
193193
restore it on the next frame.
194194

195195
The <a href="state.html" class="pattern">State</a> pattern can often help here.
@@ -206,16 +206,16 @@ means the *order* in which the objects are updated is significant.
206206

207207
If A comes before B in the list of objects, then when A updates, it will see B's
208208
previous state. But when B updates, it will <span
209-
name="double-buffer">see</span> A's *new* state, since it's already updated this
210-
frame. Even though from the player's perspective everything is moving at the
209+
name="double-buffer">see</span> A's *new* state, since A has already been updated this
210+
frame. Even though from the player's perspective, everything is moving at the
211211
same time, the core of the game is still turn-based. It's just that a complete
212212
"turn" is only one frame long.
213213

214214
<aside name="double-buffer">
215215

216216
If, for some reason, you decide you *don't* want your game to be sequential like
217217
this, you would need to use something like the <a href="double-buffer.html"
218-
class="pattern">Double Buffer</a> pattern. That makes the order that A and B
218+
class="pattern">Double Buffer</a> pattern. That makes the order in which A and B
219219
update not matter because *both* of them will see the previous frame's state.
220220

221221
</aside>
@@ -226,7 +226,7 @@ game of chess where black and white moved at the same time. They both try to
226226
make a move that places a piece in the same currently empty square. How should
227227
this be resolved?
228228

229-
Updating <span name="sequential">sequentially</span> solves this: each update
229+
Updating <span name="sequential">sequentially</span> solves this -- each update
230230
incrementally changes the world from one valid state to the next with no period
231231
of time where things are ambiguous and need to be reconciled.
232232

@@ -244,12 +244,12 @@ these update methods. That often includes code that adds or removes updatable
244244
objects from the game.
245245

246246
For example, say a skeleton guard drops an item when slain. With a new object,
247-
you can usually just add it to the end of the list without too much trouble.
247+
you can usually add it to the end of the list without too much trouble.
248248
You'll keep iterating over that list and eventually get to the new one at the
249249
end and update it too.
250250

251251
But that does mean that the new object gets a chance to act during the frame
252-
that it was spawned, before the player has had a chance to even see it. If you
252+
that it was spawned, before the player has even had a chance to see it. If you
253253
don't want that to happen, one simple fix is to cache the number of objects in
254254
the list at the beginning of the update loop and only update that many before
255255
stopping:
@@ -290,7 +290,7 @@ removing an object only shifts items that were already updated.
290290

291291
One fix is to just be careful when you remove objects and update any iteration
292292
variables to take the removal into account. Another is to defer removals until
293-
you're done walking the list. Mark the object as "dead" but leave it in place.
293+
you're done walking the list. Mark the object as "dead", but leave it in place.
294294
During updating, make sure to skip any dead objects. Then, when that's <span
295295
name="defer">done</span>, walk the list again to remove the corpses.
296296

@@ -304,17 +304,17 @@ synchronization during updates.
304304

305305
## Sample Code
306306

307-
This pattern is so straightforward the sample code almost belabors the point.
307+
This pattern is so straightforward that the sample code almost belabors the point.
308308
That doesn't mean the pattern isn't *useful*. It's useful in part *because* it's
309309
simple: it's a clean solution to a problem without a lot of ornamentation.
310310

311-
But just to keep things concrete, let's walk through a basic implementation.
312-
We'll start with an entity class that will represent the skeletons and statues:
311+
But to keep things concrete, let's walk through a basic implementation.
312+
We'll start with an `Entity` class that will represent the skeletons and statues:
313313

314314
^code entity-class
315315

316316
I stuck a few things in there, but just the bare minimum we'll need later.
317-
Presumably in real code there'd be lots of other stuff like graphics and
317+
Presumably in real code, there'd be lots of other stuff like graphics and
318318
physics. The important bit for this pattern is that it has an abstract
319319
`update()` method.
320320

@@ -349,7 +349,7 @@ href="game-loop.html" class="pattern">Game Loop</a> pattern.
349349
### Subclassing entities?!
350350

351351
There are some readers whose skin is crawling right now because I'm using
352-
inheritance on the main entity class to define different behaviors. If you don't
352+
inheritance on the main `Entity` class to define different behaviors. If you don't
353353
happen to see the problem, I'll provide some context.
354354

355355
When the game industry emerged from the primordial seas of 6502 assembly code
@@ -366,7 +366,7 @@ the Gang of Four knew this in 1994 when they wrote:
366366
367367
<aside name="subclass">
368368

369-
Just between you and me, I think the pendulum has swung a bit too far *away*
369+
Between you and me, I think the pendulum has swung a bit too far *away*
370370
from subclassing. I generally avoid it, but being dogmatic about *not* using
371371
inheritance is as bad as being dogmatic about using it. You can use it in
372372
moderation without having to be a teetotaler.
@@ -403,21 +403,21 @@ new entity that implements `update()` appropriately:
403403
As you can see, we pretty much just cut that chunk of code from the game loop
404404
earlier in the chapter and pasted it into `Skeleton`&#8217;s `update()` method.
405405
The one minor difference is that `patrollingLeft_` has been made into a field
406-
instead of a local variable. That way its value sticks around between calls to
406+
instead of a local variable. That way, its value sticks around between calls to
407407
`update()`.
408408

409409
Let's do this again with the statue:
410410

411411
^code statue
412412

413-
Again, most of the change is just moving code from the game loop into the class
413+
Again, most of the change is moving code from the game loop into the class
414414
and renaming some stuff. In this case, though, we've actually made the codebase
415415
simpler. In the original nasty imperative code, there were separate local
416416
variables for each statue's frame counter and rate of fire.
417417

418418
Now that those have been moved into the `Statue` class itself, you can create as
419419
many as you want and each instance will have its own little timer. That's really
420-
the motivation behind this pattern: it's now much easier to add new entities to
420+
the motivation behind this pattern -- it's now much easier to add new entities to
421421
the game world because each one brings along everything it needs to take care of
422422
itself.
423423

@@ -462,12 +462,12 @@ so:
462462

463463
Now, the distance the skeleton moves increases as the elapsed time grows. You
464464
can also see the additional complexity of dealing with a variable time step. The
465-
skeleton may overshoot the bounds of its patrol with a large time slice and we
466-
have to carefully handle that.
465+
skeleton may overshoot the bounds of its patrol with a large time slice, and we
466+
have to handle that carefully.
467467

468468
## Design Decisions
469469

470-
With a simple pattern like this, there isn't too much variation, but there's
470+
With a simple pattern like this, there isn't too much variation, but there are
471471
still a couple of knobs you can turn.
472472

473473
### What class does the update method live on?
@@ -485,14 +485,14 @@ The most obvious and most important decision you'll make is what class to put
485485
Having to subclass `Entity` every time you want a new behavior is brittle
486486
and painful when you have a large number of different kinds. You'll
487487
eventually find yourself wanting to reuse pieces of code in a way that
488-
doesn't gracefully map to a single inheritance hierarchy and then you're
488+
doesn't gracefully map to a single inheritance hierarchy, and then you're
489489
stuck.
490490

491491
* **The component class:**
492492

493493
If you're already using the <a href="component.html"
494494
class="pattern">Component</a> pattern, this is a no-brainer. It lets each
495-
component update itself independently. In the same way that the update
495+
component update itself independently. In the same way that the Update Method
496496
pattern in general lets you decouple game entities from each other in the
497497
game world, this lets you decouple *parts of a single entity* from each
498498
other. Rendering, physics, and AI can all take care of themselves.
@@ -508,8 +508,8 @@ The most obvious and most important decision you'll make is what class to put
508508

509509
If you're using one of those patterns, it's natural to put `update()` on
510510
that delegated class. In that case, you may still have the `update()` method
511-
on the main class, but it will be non-virtual and just forward to the
512-
delegate object. Something like:
511+
on the main class, but it will be non-virtual and will simply forward to the
512+
delegated object. Something like:
513513

514514
^code forward
515515

@@ -519,7 +519,7 @@ The most obvious and most important decision you'll make is what class to put
519519

520520
### How are dormant objects handled?
521521

522-
You often have a number of objects in the world that for whatever reason
522+
You often have a number of objects in the world that, for whatever reason,
523523
temporarily don't need to be updated. They could be disabled, or off-screen, or
524524
not unlocked yet. If a large number of objects are in this state, it can be a
525525
waste of CPU cycles to walk over them each frame only to do nothing.
@@ -570,9 +570,9 @@ collection that avoids them during your core game loop.
570570

571571
## See Also
572572

573-
* This pattern is part of a trinity with <a href="game-loop.html"
573+
* This pattern, along with <a href="game-loop.html"
574574
class="pattern">Game Loop</a> and <a href="component.html"
575-
class="pattern">Component</a> that often form the nucleus of a game engine.
575+
class="pattern">Component</a>, is part of a triinity that often forms the nucleus of a game engine.
576576

577577
* When you start caring about the cache performance of updating a bunch of
578578
entities or components in a loop each frame, the <a

0 commit comments

Comments
 (0)