Game Development Reference
In-Depth Information
Note that VGetActor() returns a weak pointer to the actor so that systems can hold
on to this pointer for as long as they want without keeping the actor from being
destroyed. In fact, the only thing that should maintain a strong pointer to the actor
is the m_actors map and the m_pOwner pointer on components owned by the
actor. Having only two strong pointers to the actor ensures that an actor is truly
destroyed when you call its Destroy() method.
Having this direct control over the lifetime of actors (or really any object) is very
important. Actors are used to represent complex objects like characters. A character
has geometry information, textures, shaders, scripts, maybe an inventory that links to
even more actors, and so on. All of these things together amount to a ton of
data, which means a ton of memory. You need to have the ability to destroy these
actor objects at any time to free up memory. If you allowed other systems to
hold onto strong pointers to actors, you ' d have a tough time ensuring that the actor
was destroyed at all. Even worse, since actors are composites of multiple objects,
you could get actors that lie around in completely broken states. Fixing these types
of issues was my fulltime job for about a month toward the end of The Sims
Medieval.
There are many other ways of storing actors. You could put them all in a single STL
vector and have the index be the ID. This could be very wasteful if you
'
re often delet-
ing actors, unless you account for the reuse of actor IDs. The advantage here is in the
ultra-fast lookups, which are O(1) , or constant time. It
s lightning fast because you
can just index into an array. This type of data structure would work well on a game
where your actors tend to stick around, like an adventure game. It wouldn
'
'
t work as
well in an FPS due to the constant deletions.
Another possible solution is to break up your game world into chunks where each
chunk represents some part of the world. If your whole world is a grid of chunks, it
becomes trivial to find out which actors belong to what chunks by taking their posi-
tion and dividing it by the width and height of the grid cells. This kind of spatial
partitioning is crucial in FPS or RTS games. Let
s say I throw a grenade that explodes
in a 15-foot radius. Which actors are affected? With the implementation above, you
'
'
d
have to loop through the entire map to find your actor. If you had a cell-based par-
titioning system, you could figure out which cells were affected by the grenade and
just loop through the actors in those cells.
Looping through the entire map isn
t a big deal when you have a couple dozen
actors. When you have hundreds or even thousands of actors, it becomes way too
costly. Spatial partitioning gives you a way to cut down the number of actors you
have to consider.
'
Search WWH ::




Custom Search