Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

Dynamic cast to certain valid component type with unique_ptr fails

This topic is 866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After inserting the arrivals component to the GameObject which has a unique_ptr to the component in a vector,

while the ComponentManager holding a raw pointer of it.

I can't seem to successfully dynamic cast the Arrivals component after it is inserted into the component container.

When I did insert the Arrivals component into the container, it seemed it couldn't be reverted back into an arrivals component,

which looked very strange to me.

template<typename T>
    T* FindCompoentByType()
    {
        for (component_vector_itr itr = m_components.begin(); itr != m_components.end(); ++itr)
        {                
            if (T* l_type = dynamic_cast<T*>((*itr).get()))
            {
                return l_type;
            }
        }

        return NULL;
    };
Arrivals* l_arrival = new Arrivals(m_name + "Arrivals");
l_arrival->m_simObject = this;
l_arrival->addObserver((Observer&)*this);
this->AddComponent(ComponentManager::getInstance()->AddComponent(l_arrival));
    
CMinMax<float> arrivalTime(10.0f, 20.0f);
this->FindCompoentByType<Arrivals>()->PostRequest(arrivalTime.GetRandomNumber(), arrivalTime.GetRandomNumber());
Edited by lucky6969b

Share this post


Link to post
Share on other sites
Advertisement

I don't think it's related to the code you posted, instead you should check the type of "m_components" and "Arrivals" again. Maybe your Base is not polymorphic? You could fix that by atleast adding a virtual destructor.

Share this post


Link to post
Share on other sites

Hi there.

This is the second post of how this components system is giving you grief. Time to take a step back, Or at least put in a function call when one of the main objects dies that is linked to other components that it removes all links then deletes it self, call it some thing like RemoveAllLinkedComponentsFromMatserObject();. Or this may haunt you for ever.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!