NetStack

Quickstart

This walks through creating a project, writing a Function, and calling it from a client.

1. Create a project

Sign in to the dashboard and create a project. Choose a name and a subdomain. The subdomain is the address your players connect to, for example space-raiders.netstack.network.

2. Write a Function

Open the Functions tab and choose New from template > Use an item. You will get something like this:

export default async function (ctx, args) {
  const inventory = ctx.player.get("inventory") ?? [];

  if (!inventory.includes(args.itemId)) {
    throw new NetStackError("ITEM_NOT_OWNED", "You do not own that item.");
  }

  ctx.player.set(
    "inventory",
    inventory.filter((id) => id !== args.itemId)
  );
  ctx.room.broadcast("itemUsed", { playerId: ctx.player.id, itemId: args.itemId });

  return { remaining: inventory.length - 1 };
}

Use the Test run panel under the editor to try it against sample state, then press Save function.

3. Connect from your game

Download the client into your project (see Client SDK), then connect:

import { NetStackClient } from "./netstack-client.js";

const client = new NetStackClient({
  url: "wss://space-raiders.netstack.network",
  roomId: "lobby-1",
  playerId: "alice",
});

await client.connect();

client.on("event", ({ name, data }) => console.log(name, data));

const result = await client.call("useItem", { itemId: "potion" });
console.log(result.remaining);

Native engines can connect over UDP instead. See Connecting your game.

Next steps