Bootstrappers

Description

Bootstrappers are responsible for the initial application startup and the sequential initialization of systems.

Main responsibilities:

  • starting the application when a scene is loaded

  • creating and configuring root Zenject contexts

  • initializing global services

  • transitioning the game to its initial state

Bootstrappers define the loading order but do not contain business logic.

circle-info

When extending the project, it is recommended to add new bootstrap stages without modifying existing ones.


Built-in Bootstrapper Implementations

The project includes two built-in bootstrappers:

1. Bootstrapper

Bootstrapper is a startup MonoBehaviour responsible for application initialization.

Using Zenject, it receives a reference to the GameStateMachine and, when the scene starts, transitions the game into the initial BootstrapState, thereby initiating the overall application lifecycle.

2. GameplayBootstrapper

GameplayBootstrapper is a startup MonoBehaviour used in gameplay scenes.

It receives the GameplayStateMachine via Zenject and, on scene start, transitions it into the initial GameplayBootstrapState. In the Update method, it calls UpdateState() every frame, ensuring continuous updates of the active gameplay state and proper real-time gameplay logic execution.

How to Add a Bootstrapper

A Bootstrapper is a standard MonoBehaviour that defines which initial state should be entered when a scene is loaded.

To create a new bootstrapper:

  1. Create a new class

  2. Inject the required state machine (more detailed)

  3. Enter the desired initial state in the Start method

circle-info

It is recommended to use the Start() method instead of Awake(), because Awake() is called before Zenject finishes its initialization: at this point, SceneContext is not yet fully registered in the SceneContextRegistry, and any logic that interacts with DI or SceneContext may cause the warning “Failed to remove SceneContext from SceneContextRegistry” when the scene is unloaded; Start() is called after Zenject has fully initialized, when all dependencies are injected and the container state is consistent.

Example

  1. Add a new GameObject to the scene and attach the YourBootstrapper script to it.

Last updated