Systems#

Systems implement game logic/behavior.

During runtime, systems receive a list of compatible entities, and they can read component data of these entities and also modify component data to essentially update the entity.

Let's say, you were creating a player controller system. Your system would essentially perform the following:

  • Receive the list of matching entities In this case, the list would have only one player entity
  • Check for if the corresponding keyboard keys have been pressed
  • Read the position object of the player entity $transform component
  • Update the position object of the player entity $transform component

System Lifecycle#

System follow a fixed lifecycle, managed by the scene containing it:

start → update → react → render → flush → dispose
          ↑                         │
          └—————————————————————————┘

Function Description Required
start Called once to perform initializion No
update Called every frame to update entities No
react Called every frame to handle events of interest generated in the update phase No
render Called every frame to render relevant entities No
flush Called at the end of every frame to perform cleanup before the next frame No
dispose Called once to perform disposal before it is destroyed No

Creating a System#

Create a system using the createSystem function.

import { createSystem } from "gamekit-engine/core/system";

export const playerMovement = createSystem({
  name: "player-movement",
  deps: [$transform, $playerMarker],
  updateIntervalMs: 1000,
  createState() {
    return { foo: "bar" };
  },
  start({ state, entities }) { /* Initialization logic */ },
  update({ state, entities }) { /* Update logic */ },
  react: {
    "event1": ({ data, state, entities }) => { /* React to event 1 */ },
    "event2": ({ data, state, entities }) => { /* React to event 2 */ }
  },
  render({ state, entities }) { /* Render logic */ },
  flush({ state, entities }) { /* End of frame cleanup logic */ },
  dispose({ state, entities }) { /* Cleanup logic before system destruction */ },
});

Let's go through the above step-by-step:

  • We've created a system called "player-movement"
  • The system processes entities with $transform and $playerMarker components
  • The system updates every 1000ms
  • The system reacts to two events: event1 and event2 which can be generated by itself or other systems

Below is a table of all createSystem options:

Argument Desription Required
name Name of the system Yes
deps Component factories which the system depends on No
updateIntervalMs Time (in milliseconds) between each update call. Omit to execute every frame No
createState Function to create the initial state of the system No
start Defines the system's initialization logic No
update Defines the system's update logic No
react Map of event names to handler functions No
render Defines the system's rendering logic No
flush Defines the system's end-of-frame logic No
dispose Defines the system's dispose logic No

System State#

Sometimes, it becomes necessary for the system to store some data, not related to any entity. This systems can also have a storage object called a state.

If needed, the createState can be provided to create the system's initial state object.

This object is made available to all lifecycle functions as state, to read or modify.

Entity-specific data should remain in its components, not in the system state.

Update Interval#

By default, the update function of systems is called every frame.

If you need your system's update to execute in regular intervals, provide the interval as the updateIntervalMs. For a 10s interval between update calls, updateIntervalMs = 10000

System Events#

Systems are isolated from one another and do not communicate through direct references. Instead, they use a pub/sub system via events to send and receive data between themselves.

Systems simply emit events into the scene without caring about the receiver.

A system can react to an event it emitted itself.

A system event in GameKit has the structure:

type Event = { type: string, data: any }

Use the emitEvent function to emit an event object into the scene.

import { emitEvent } from "gamekit-engine/core/scene";

// Emit an event with no data
emitEvent("event-name-0");

// Emit an event with any data
emitEvent("event-name-1", "hello");
emitEvent("event-name-2", 42);
emitEvent("event-name-3", [1, 2, "three"]);
emitEvent("event-name-4", { foo: "bar" });

emitEvent can be used from any part of your code, not just from system functions

A system interested in an event, should declare it in its react object along with a handler function. The event data is passed to the handler as data.

import { createSystem } from "gamekit-engine/core/system";

export const playerMovement = createSystem({
  name: "player-movement",
  react: {
    "event-name-0": ({ data, state, entities }) => { /* data is undefined       */ },
    "event-name-1": ({ data, state, entities }) => { /* data is "hello"         */ },
    "event-name-2": ({ data, state, entities }) => { /* data is 42              */ },
    "event-name-3": ({ data, state, entities }) => { /* data is [1, 2, "three"] */ },
    "event-name-4": ({ data, state, entities }) => { /* data is { foo: "bar" }  */ }
  },
});

Builtin Systems#

GameKit ships with a number of built-in systems, which are included in your scene automatically.

System Description
$spriteRenderer Processes $sprite components in entities and renders images
$animatedSpriteRenderer Processes $animatedSprite components in entities and renders spritesheet animations
$audioPlayer Processes $audioSource components in entities and plays audio

Next Steps#

In the next guide, we'll explore scenes in more detail.