User Interface (UI)#

User Interface in GameKit is achieved using JSX syntax. This is the same syntax a number of popular web frameworks like React and SolidJS use.

GameKit does not use any special UI syntax. If you know basic HTML, CSS and JS, you are good to go.

Creating A User Interface#

UI belong to scenes. Create a user interface with the createUI function and pass it to a scene object as show below:

import { createScene } from "gamekit-engine/core/scene";
import { createUI } from "gamekit-engine/core/ui";

const scene = createScene({
  name: "my-scene",
  /* ... */
  ui: createUI({
    get() {
      return (
        <div class="grid place-items-center">
          <p>Hello World!</p>
        </div>
      );
    }
  })
});

In the example above, we created a UI object with createUI and supplied the get function to return the UI markup. There is nothing special to it. Everything you know about HTML CSS applies here.

GameKit integrates Tailwind 4 out of the box, so you can use all of its class names in your markup.

Use the included src/main.css to apply global styling or create custom classes.

Standalone Components#

JSX allows you can break apart your user interfaces into standalone (reusable) components to avoid clutter.

For example, you can create a custom button component and reuse it in multiple parts of your UI.

import { createScene } from "gamekit-engine/core/scene";
import { createUI } from "gamekit-engine/core/ui";

function Button(props: { onclick: () => void, children: any }) {
  return (
    <button onclick={onclick}>{children}</button>
  );
}

function PauseMenu() {
  const handleResume = () => { /* Resume logic here */ }

  const handleQuit = () => { /* Quit logic here */ }

  return (
    <div class="space-y-4">
      <h1 class="text-center">Paused</h1>

      <Button onclick={handleResume}>Resume</Button>
      <Button onclick={handleQuit}>Quit</Button>
    </div>
  );
}

const scene = createScene({
  name: "my-scene",
  /* ... */
  ui: createUI({
    get() {
      return (
        <div class="grid place-items-center">
          <PauseMenu />
        </div>
      );
    }
  })
});

This guide does not teach JSX. Learn more about it from the React docs.

Local Component State#

If your component needs local data which persists between renders, use the state hook function like so:

import { state } from "gamekit-engine/core/ui";

function MyComponent() {
  const [count, setCount] = state(0);

  return (
    <div class="flex flex-col gap-2 items-center">
      <p>You have clicked the button {count} times</p>

      <button onclick={() => setCount(count + 1)}>Increase</button>
    </div>
  );
}

state takes an optional initial data and returns a tuple containing the state data and a setter function to update the data. Anytime the function is called, the UI is re-rendered.

Shared UI State#

If you need state that is accessible across multiple components, you need a shared state. Shared state exists on the UI object and isn't tied to one component.

Set up a shared state like so:

my-scene.tsx

import { createScene } from "gamekit-engine/core/scene";
import { createUI } from "gamekit-engine/core/ui";

export const ui = createUI({
  createState() {
    return {
      /* Shared state fields here */
      paused: false
    }
  },
  get() {
    return (
      <div class="grid place-items-center">
        <PauseMenu />
      </div>
    );
  }
});

const scene = createScene({
  name: "my-scene",
  /* ... */
  ui: ui
});

In the above example, we've assigned the UI object to a constant named ui. We can import this constant anywhere we need it and access the current state.

import { ui } from "./my-scene";

export function GameUI() {
  return (
    <div class="">
      {ui.state.paused ? <PauseMenu /> : <GameplayUI />}
    </div>
  );
}

Updating Shared UI State#

To update a shared state, simply use the updateUI function from anywhere in your code. Ideally, you want to update the UI anytime you modify a shared state field.

import { updateUI } from "gamekit-engine/core/ui";
import { ui } from "./my-scene";

export function GameUI() {
  const handlePause = () => {
    updateUI(() => {
      ui.state.paused = true;
    });
  };

  return (
    <div class="">
      {ui.state.paused ? <PauseMenu /> : <GameplayUI />}

      <button onclick={handlePause}>Pause</button>
    </div>
  );
}

Remember to call updateUI whenever you modify your shared state to trigger a re-render.

Mount & Unmount Callbacks#

If a component needs to perform an action when it is mounted to or unmounted from the UI, use the onmount and onunmount hook functions respectively as shown below:

import { onmount, onunmount } from "gamekit-engine/core/ui";

export function GameUI() {
  onmount(() => {
    console.log("Component mounted");
  });

  onunmount(() => {
    console.log("Component about to be unmounted");
  });

  return (
    <div class="">
      <p>Hello World</p>
    </div>
  );
}

Callbacks to onmount and onunmount are called exactly once. If the component is mounted again, the callbacks will be called once again.

Side Effects#

If a function component needs to perform a side effect when one or more parameters change, use the effect hook function as shown below:

import { effect, state } from "gamekit-engine/core/ui";

export function GameUI() {
  const [count, setcount] = state(0);

  effect(() => {
    // Executes when the value of count changes
    console.log(`Count it ${count}`);
  }, [count]);

  return (
    <div class="">
      <p>Counter is {count}</p>
      <button onclick={() => setcount(count + 1)}>Add</button>
    </div>
  );
}

Next Steps#

In the next guide, we'll explore GameKit's audio system.