Guides
Scripting Lifecycle

Understanding Scripting Lifecycle Methods

Main Scripts in Game Development

In Oncyber Studio, the main script serves as the central control hub for your game. It's where the core game loop and major game logic reside. This script orchestrates how different components and scripts interact, ensuring smooth gameplay.

A main script should have a default export of a class with some specially named methods.

Below is an example of main script:

import { World, Componens, Player } from "@oo/scripting"
 
 
export default class GameManager {
 
    elapsedTime = 0;
 
    onPreload() { ... }
 
    onReady() { ... }
 
    onStart() {
 
        this.elapsedTime = 0;
    }
 
 
    onUpdate() {
 
        this.elapsedTime += dt;
 
        if(this.elapsedTime >= config.maxGameTime) {
 
            World.stop()
        }
    }
 
    onPause() { ... }
 
    onResume() { ... }
 
    onDispose() { ... }
}

Understanding Lifecycle Methods:

In the previous example, the exported class defined some specially named methods. Those are lifecycle methods.

Lifecycle methods are a set of predefined functions in the main script that are automatically called by the game engine at specific points during the game's execution. These methods allow you to hook into the game's lifecycle and define how your game reacts to events like starting, pausing, or ending.

1. Initialization: onPreload()

This method is invoked before the game assets start loading, it's a perfect place for pre-loading operations, like setting up asset paths or initializing settings.

You can await for other resources or any other async calls in this method. The engine will wait for this method to resolve before triggering the onReady callback.

2. Game Ready: onReady()

This method is called once all assets are loaded and the game is ready to start. Use this method for one time initialization code for the whole game. This could include things like creating a set of game entities that will presist accross game restarts.

3. Game Start: onStart()

Triggered when the game is started by the user. The state affected by this method should include anything that must be reset after a game restarts. This could include things such as the initial position of the player, the placement of the enemies ...etc

4. Game Update Loop: onUpdate(dt)

This is the core of the game loop, called repeatedly, typically many times per second. It's where you put code that changes over time, like player movement, collision detection, or score updates. The parameter dt represents the time elapsed since the last frame, which helps in creating time-independent movements.

5. Pausing and Resuming: onPause() and onResume()

  • onPause(): Invoked when the game is paused, for instance, when a player opens a menu. Use this to halt animations or timers, pause the audio ...

  • onResume(): Called when the game resumes from a paused state. Used typically for eactivating paused elements.

6. Game End: onEnd()

This method is called when the game ends. In the previous example we saw an example of how to end the game after a fixed number of seconds by calling World.stop().

You can use this method for example to update player score, stop the playing audios in the game ...

7. Cleanup: onDispose()

Executed when the game is about to be destroyed, it's used for final cleanup, like releasing resources or unsubscribing from events.

Understanding and effectively using these lifecycle methods is crucial in controlling the flow of your game and ensuring a smooth and responsive gaming experience. In the following sections, we will delve into more specific features and capabilities of the Oncyber Studio scripting environment.