Component order of destruction


Hi there, I was wondering if there is any way to ensure the order of destruction of component instances within an entity instance. Is there a difference between using component dependency or a component ptr when it comes to order of initi/destruction? It would be helpful for me if the order of destruction would be the reverse of the order of initialization, but I read in an older thread that the order is undefined?


Now we are talking about this, also about the order of updating the component instances I would like to learn moreā€¦ Is this order defined behavior?


Every component instance has an onDestroy() method, which is called in the inverse order of initialization (scene.cpp: 839):

		// Call onDestroy in reverse initialization order, and remove both ComponentInstance and EntityInstance from the instance maps.
		for (int index = pos->second.size() - 1; index >= 0; --index)
			ComponentInstance* componentInstance = pos->second[index];


			std::string entityInstanceID = componentInstance->getEntityInstance()->mID;
			EntityByIDMap::iterator entity_instance = mEntityInstancesByID.find(entityInstanceID);
			if (entity_instance != mEntityInstancesByID.end())

To ensure order of initialization use a nap::ComponentPtr or Component::getDependentComponents. Actual destruction is based on order of creation, which is something you should not depend on since objects can be created / added out of order by the scene and resource manager.


Regarding component processing order: ComponentInstance processing order.