Execution Order and Lifecycle

Overview

This article aims to give an understanding of the order in which entities are updated, and when specific events are fired.

Update and Execution Order

Entities can receive three events tied to the main engine update:

NameDescriptionUse case
ENTITY_EVENT_PREPHYSICSUPDATESent before a new physics step is started (note that the physics step itself will occur on a different thread).Send physics requests to process with the next tick, causing minimal delay.
ENTITY_EVENT_COLLISIONSent when collisions from the previous physics tick are being logged.Handle collision events from Physics, for example to detect if a bullet hit an object.
ENTITY_EVENT_UPDATEOccurs during the main update, where entities should execute main logic such as moving around.The main update for entities, this is where we can interact with other entities or move our own entity instance (assuming it does not have a physical representation).

The order in which these are received is visualized below:

Lifecycle

On the lowest level, an entity is created with IEntitySystem::SpawnEntity, initialized with IEntitySystem::InitEntity (automatically done by SpawnEntity if not otherwise specified) and persists in the game world until it is removed by calling IEntitySystem::RemoveEntity.

However, if an entity is not removed during the time a level is loaded - it will automatically be removed when the entity system unloads (see IEntitySystem::Unload), in the typical scenario whenever the level itself is unloaded.

The whole existence flow of an entity has been visualized below, and is combined with a series of events being fired as the level is loaded and the player is able to play:

Conclusion

This concludes this article on the Execution Order of Entities. You may be interested in: