Main loop

Each global function described on this page can be defined once in every module. If there are multiple definitions of a function, the call order between them is undefined.

Initialization

The game initialization happens right after the game launch. It consists of the following steps:

  • All global on_initialize() functions are called.

  • A default prefab is instantiated.

    • For every node, Component.on_initialize() is called (like in any prefab instantiation).

      • The calls order is the same as the order of Component.on_update() calls (see below).

    • This step is skipped if no default prefab is provided.

  • All global on_post_initialize() functions are called.

Game loop

The main loop consists of the following calls to user-provided functions:

  • Perform physics simulation in several steps. For each physics step:

    • Call all global on_fixed_update() functions. Note that there’s no Component.on_fixed_update() counterpart.

    • Update all characters

    • Simulate physics world

    • Invoke collision callbacks for collisions that just occurred

  • All global on_resolution_changed() functions if resolution changed.

  • All global on_update() functions.

  • All Component.on_update() functions for every alive and enabled node.

    • Order of nodes: in the order of creation. For example, if node a is created before b, then a will always be called before b, regardless of the scene tree.

    • Order of components: in the order of addition. For example, if component Foo is added to a node before component Bar, then Foo.on_update() will be called before Bar.on_update().

Functions

on_after_reload()

This function is called after the scene is reloaded

Usage example:

[export]
def on_after_reload() {
    print("Scene reloaded")
}
on_before_reload()

This function is called before the scene is reloaded Usually whole global state of game can be automatically hot-reloaded, but sometimes you need to do some manual setups in this function

Usage example:

[export]
def on_before_reload() {
    print("Reloading scene...")
}
on_editor_update(playing: bool)

This function is called every frame in the editor. Can be used to draw debug information in the editor.

Arguments:
  • playing : bool - True if the game is running in the editor, False if it is paused

Usage example:

[export]
def on_editor_update() {
    print("Inside editor")
}
on_exit()

This function is called when the scene is exited. Last chance to save the game state

Usage example:

[export]
def on_exit() {
    print("Exiting game")
}
on_fixed_update()

This function is called before every physics step. Note that this function is not called in equal intervals of real time, but in equal intervals of physics simulation time.

Usage example:

[export]
def on_fixed_update() {
    print("Fixed delta time {get_delta_time()}")
}
on_initialize()

This function is called once when the scene is first loaded

Usage example:

[export]
def on_initialize() {
    print("Hello, world!")
}
on_pause(pause: bool)

This function is called when the scene is paused. Can be used to save the game state

Arguments:
  • pause : bool - True if the scene is paused, False if it is resumed

Usage example:

[export]
def on_pause(pause : bool) {
    if (pause) {
        print("Game paused")
    } else {
        print("Game resumed")
    }
}
on_post_initialize()

This function is called once after the scene is initialized (after the instantiation of the main prefab)

Usage example:

[export]
def on_post_initialize() {
    print("Post initialization")
}
on_post_update()

This function is called every frame, after all on_update() functions and component updates have been processed

Usage example:

[export]
def on_post_update() {
    print("Post update frame time {get_delta_time()}")
}
on_resolution_changed(res: int2)

This function is called when the resolution is changed

Arguments:
  • res : int2 - The new resolution

Usage example:

[export]
def on_resolution_changed(res : int2) {
    print("Resolution changed to {res}")
}
on_update()

This function is called every frame

Usage example:

[export]
def on_update() {
    print("Frame time {get_delta_time()}")
}