Game Development Reference
In-Depth Information
void Update(int deltaMs);
// accessors
ActorId GetId(void) const { return m_id; }
// template function for retrieving components
template <class ComponentType>
weak_ptr<ComponentType> GetComponent(ComponentId id)
{
ActorComponents::iterator findIt = m_components.find(id);
if (findIt != m_components.end())
{
StrongActorComponentPtr pBase(findIt->second);
// cast to subclass version of the pointer
shared_ptr<ComponentType> pSub(
std::tr1::static_pointer_cast<ComponentType>(pBase));
weak_ptr<ComponentType> pWeakSub(pSub); // convert strong pointer
// to weak pointer
return pWeakSub; // return the weak pointer
}
else
{
return weak_ptr<ComponentType>();
}
}
private:
// This is called by the ActorFactory; no one else should be
// adding components.
void AddComponent(StrongActorComponentPtr pComponent);
};
The m_components member is the map of all components that this actor has.
Notice that they
'
re keyed off the component ID. This ID is unique for each compo-
nent interface.
The Init() and PostInit() functions are called by the factory as the actor is
being created and were covered in the CreateActor() function previously.
The Destroy() function is called when you want to destroy the actor. The actor
holds onto strong references to each of its components, but the components also
need to hold onto strong references to the actor. If you recall from my peanut butter
and jelly example in Chapter 3, having a circular reference can potentially cause
memory leaks. It ' s not easily avoided since some components may still need to access
the actor during destruction time. If weak pointers were used instead, it would cause
Search WWH ::




Custom Search