754 14. Runtime Gameplay Foundation Systems
to return null when dereferenced rather than returning a pointer to object B
unexpectedly.
The following code snippet shows how a simple handle class might be
implemented. Notice that we’ve also included the handle index in the Game
Object class itself—this allows us to create new handles to a GameObject
very quickly without having to search the handle table for its address to de-
termine its handle table index.
// Within the GameObject class, we store a unique id,
// and also the object’s handle index, for efficient
// creation of new handles.
classGameObject
{
private:
// ...
GameObjectId m_uniqueId; // object’s unique id
U32 m_handleIndex; // speedier handle
// creation
friend class GameObjectHandle; // access to id and
// index
// ...
public:
GameObject() // constructor
{
// The unique id might come from the world editor,
// or it might be assigned dynamically at runtime.
m_uniqueId = AssignUniqueObjectId();
// The handle index is assigned by finding the
// first free slot in the handle table.
m_handleIndex = FindFreeSlotInHandleTable();
// ...
}
// ...
};
// This constant defines the size of the handle table,
// and hence the maximum number of game objects that can
// exist at any one time.
static const U32 MAX_GAME_OBJECTS = ...;
// This is the global handle table -- a simple array of
// pointers to GameObjects.
static GameObject* g_apGameObject[MAX_GAME_OBJECTS];