Life Cycle

A component represents an object or content in the game such as screens, menus, and all other visual or structural elements. Components are custom HTML elements that encapsulate their own HTML, CSS, and JavaScript logic.

Mounting Cycle

The life cycle of a component follows the same mounting cycle as actions. This cycle runs once when the engine initializes.

1. Setup

Set up everything the component needs for working:

static async setup() {
    // Add HTML content to the document
    this.engine.element().find('game-screen').append('<my-component></my-component>');
    
    // Initialize configuration
    this.configuration({
        defaultValue: 'something'
    });
}

2. Bind

Bind event listeners and perform DOM operations:

3. Init

Final initialization after setup and binding:

Instance Lifecycle

Individual component instances also have their own lifecycle methods.

connectedCallback

Called when the component is added to the DOM:

render

Returns the HTML content for the component:

onStateUpdate

Called when component state changes:

onPropsUpdate

Called when component props change:

Flow Control Methods

Components can participate in game flow control, both at the static level (all instances) and instance level.

Static Flow Control

These methods iterate over all instances of the component:

Instance Flow Control

Each component instance can implement these methods:

Event Methods

onStart

Called when a new game starts:

onLoad

Called when a saved game is loaded:

onSave

Called when the game is saved:

onReset

Called when the game ends or before loading a new game:

Complete Life Cycle Diagram

Last updated

Was this helpful?