   C++ and Game Object Component Architecture
  Greetings,I have been implementing a game object component based system for my game in C++.  I have based the design of the system on a lecture given by Marcin Chady of Radical Entertainment at GDC Canada in 2009.  The paper can be found here.On slide 21, the author gives an example of the implementation of HealthBehaviour.  Here is what it looks like:void HealthBehaviour::OnMessage(Message* m){    switch (m.type)    {   case APPLY_DAMAGE:  Attribute<float>* healthAttr = GetAttribute(HEALTH_KEY);  healthAttr->value -= m.damage;  if (healthAttr->value < 0.f) mGameObject->SetLogicState(DEAD);  break;   case ATTR_UPDATED:  if (m.key == HEALTH_KEY)  { Attribute<float>* healthAttr = GetAttribute(HEALTH_KEY); if (healthAttr->value < 0.f)mGameObject->SetLogicState(DEAD);  }  break;    }}This is the line that is confusing me:Attribute<float>* healthAttr = GetAttribute(HEALTH_KEY);I am not sure after reading the paper how they are storing the attributes.  The way I implemented it is that each 'game object' is really just an unsigned int that is used by the game object manager.  For each game object, the game object manager holds a list of components and a map of pointers to registered attributes.  Since attributes can be different types (int, float, bool, etc.) I had to make the map return a void pointer and then cast to the appropriate type.My question is how would I implement that like they did in the paper without using a cast?   
  It might not be real code that's in use somewhere, but rather just there to demonstrate the use of attributes.  Like you said, you'd either need an explicit cast outside the function, or you'd need to templatize the GetAttribute function and call it like GetAttribute<float>(HEALTH_KEY) so that the cast can be performed inside the function.  Then again, if this is real code there could be anything going on -- maybe GetAttribute is really a macro, who knows.   ;Quote:Original post by Zipsteror you'd need to templatize the GetAttribute function and call it like GetAttribute<float>(HEALTH_KEY) so that the cast can be performed inside the function.Yeah that's what I do. I store 32bit "type-hashes" to verify the cast within the GetAttribute function. Something along the lines of:template<class T>Attribute<T>* GetAttribute(int id){  void* unsafePtr = GetAttributeUnsafe(id);  uint32 actualTypeHash = GetAttributeType(id);  uint32 requestedType  = GetTypeHash<T>();  if( actualTypeHash == requestedType )    return reinterpret_cast<Attribute<T>*>( unsafePtr );  else    return 0;};template < class T > struct Key {  ...};const Key<float> HEALTH_KEY("health"); // or some other way that makes key uniquetemplate < class T >Attribute<T> * GetAttribute(const Key<T> &);Memory must still be allocated for those above, but strongly typed allocators can be used, something like:template < class T >BlockAllocator<T> & getInstance(const Key<T> &) {  static BlockAllocator<T> alloc;  return alloc;}Replace with non-singleton for non-trivial implementations. This way, each type has its own strongly typed storage, so even if void * is used at some point, type check can be delegated to BlockAllocator, who knows whether it owns the address or not, which can come handy for debugging.Alternatively, a common non-templated version of Attribute can be used to hold type information.   