Sonolus Wiki

02. Lifetime

In this chapter, we will going over how lifetime of entities are managed in watch mode.

Timeline and updateSpawn

Unlike in play mode where time progresses linearly, players can freely time skip forward and backward in watch mode, and thus it requires a different mechanism for managing lifetime of entities.

Every frame the global updateSpawn callback will be called, and the return value will be used as the current "time" of the timeline. Most engines simply return the current time or scaled time.

For our engine, we can return the scaled time:

export const updateSpawn = () => time.scaled
export const updateSpawn = () => time.scaled

spawnTime and despawnTime

For each entity, its spawnTime and despawnTime callbacks will be called, and the return values are in the same timeline as updateSpawn and used as the spawn range of the entity.

Entities will spawn and despawn automatically based on the current time of the timeline. For example, an entity with a spawn range of 100 to 200 will spawn automatically when current time of the timeline advances from 99 to 100, and will despawn automatically when current time of the timeline advances from 199 to 200.