daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class GameObject
  2. {
  3. public:
  4.     template <typename T>
  5.     T* getComponent()
  6.     {
  7.  
  8.         auto iter = m_components.find(typeid(T));
  9.  
  10.         if(iter != std::end(m_components))
  11.         {
  12.             // if found dynamic cast the component pointer and return it
  13.             return dynamic_cast<T*>(iter->second);
  14.         }
  15.        
  16.         // return null if we don't have a component of that type
  17.         return nullptr;
  18.        
  19.     }
  20.  
  21.     template <typename T>
  22.     void addComponent(T* comp)
  23.     {
  24.         // add the component to unoreder map with hash of its typeid
  25.         m_components[typeid(T)] = comp;
  26.     }
  27.    
  28.     virtual void OnUpdate(float dt) = 0;
  29.     virtual void OnMessage(const std::string m) = 0;
  30.    
  31. private:
  32.     std::unordered_map<std::type_index, Component*> m_components;
  33. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top