Component order of destruction


#1

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?


#2

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?


#3

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];

			componentInstance->onDestroy();
			mInstancesByID.erase(componentInstance->mID);

			std::string entityInstanceID = componentInstance->getEntityInstance()->mID;
			EntityByIDMap::iterator entity_instance = mEntityInstancesByID.find(entityInstanceID);
			if (entity_instance != mEntityInstancesByID.end())
			{
				entity_instances_to_delete.emplace_back(std::move(entity_instance->second));
				mEntityInstancesByID.erase(entityInstanceID);
				mInstancesByID.erase(entityInstanceID);
			}
		}

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.


#4

Regarding component processing order: ComponentInstance processing order.