Chilli Source follows strict rules for the life cycle events that each module receives. Application, States, systems and Components all receive a series of life cycle events depending on the state of the app. The following describes the lifecycle events that each module can receive.
Application
- CreateSystems: This is the first event received by a Application. This is where all App Systems must be created.
- OnInit: This is called once an app has been fully initialised, in other words all App Systems are created and initialised.
- PushInitialState: This is called after OnInit and is responsible for adding the first state to the state stack.
- OnDestroy: This is called immediately before an app begins destroying and cleaning up all Entities, States and systems.
State
- CreateSystems: This is the first event received by a State. It should create all State Systems.
- OnInit: This is called when the State is first added to the state manager, immediately after all State Systems have been initialised.
- OnResumed: This is called whenever the app is resumed while the State is the top of the state stack, or when the State becomes the top of the state stack.
- OnForegrounded: This is called when the app becomes foregrounded–in other words no other app is displayed over the top–or when the State becomes the top of the state stack while the app is in the foreground.
- OnUpdate: This is called every frame that the State is top of the state stack which the app is active.
- OnFixedUpdate: This is called at a fixed time step–typically at 60fps–while the state is top of the state stack. This is useful for physics calculations.
- OnBackground: This is called when the app leaves the foreground, or when the State is no longer the top of the state stack.
- OnSuspend: This is called when the app is suspended, or when the State is no longer the top of the state stack.
- OnDestroy: This is called when the state is removed from the state stack, immediately before the Components and State Systems are cleaned up. The state can no longer be used after this event.
System
- OnInit: This is called immediately after the owner has created all systems.
- OnResumed: This is called whenever the app is resumed. In the case of a State System this is also called when the owning state becomes the top of the state stack.
- OnForegrounded: This is called when the app becomes foregrounded. In the case of a State System this is also called when the owning state becomes the top of the state stack.
- OnUpdate: This is called every frame that the app is active. In the case of a State System this is only called while the owning state is top of the state stack.
- OnFixedUpdate: This is called at a fixed time step–typically at 60fps–while the owning state is top of the state stack. This is useful for physics calculations.
- OnBackground: This is called when the app leaves the foreground. In the case of a State System this is also called when the owning state relinquishes the top of the state stack.
- OnSuspend: This is called when the app is suspended. In the case of a State System this is also called when the owning state relinquishes the top of the state stack.
- OnDestroy: This is called when the owner is destroyed.
Component
- OnAddedToEntity: This is called when the Component is added to an Entity.
- OnAddedToScene: This is called when the Component is either added to an Entity that is already in the scene, or when the owning entity is added to the scene.
- OnResumed: This is called whenever the app is resumed while the Component is in the Scene, or when the Component is added to the Scene while the app is active.
- OnForegrounded: This is called when the app becomes foregrounded while the Component is in the Scene, or when the Component is added to the scene while the app is foregrounded.
- OnUpdate: This is called every frame that the app is active and the Component is in the Scene.
- OnFixedUpdate: This is called at a fixed time step–typically at 60fps–while the Component is in the scene. This is useful for physics calculations.
- OnBackground: This is called when the app leaves the foreground, or when the component is removed from the Scene while the app is foregrounded.
- OnSuspend: This is called when the app is suspended, or when the component is removed from the Scene while the app is active.
- OnRemovedFromScene: This is called either when the Component is removed from an Entity that is in the Scene, or when the owning Entity is removed from the Scene.
- OnRemovedFromEntity: This is called when the Component is removed from an Entity.
All lifecycle events are only received while a module is active (for example a state will only receive events while at the top of the state stack). Events are propagated in the following order: App Systems, State Systems, Components, States then Application. States and Application receive the events after systems and Components so they can act on the result of the events. In addition, destructive events (i.e. OnDestroy, OnBackgrounded, etc) are processed in reverse order. This ensures that something available on construction will always also be available on destruction.