Write your first resource
Build a resource of your own next to the default gamemode: a chat command, a key bind, and one round trip between the server and a player's game.
A resource is a folder with a package.json in it. This page builds one called survival-kit: it
greets players, adds a /kit command, binds a key in each player’s game, and sends a message both
ways. Plain JavaScript, no build step. Use TypeScript adds one afterwards.
Work inside the fmp-resources/ folder next to your server binary, beside fmp-gamemode.
1. The manifest
Section titled “1. The manifest”{ "name": "survival-kit", "version": "0.1.0", "private": true, "description": "A first resource: a command, a key bind and a round trip", "mafiahub": { "serverScripts": ["server/index.js"], "clientScripts": ["client/index.js"] }}name is how you refer to the resource in the console, so keep it the same as the folder. The
mafiahub block is the whole contract with the server:
| Key | What it means |
|---|---|
serverScripts |
Files the server runs, in order |
clientScripts |
Files sent to every player’s game and run there |
files |
Extra files players may download, HTML and images for a browser view |
priority |
Higher starts earlier; the default gamemode uses 10 |
errorBehavior |
stop by default, or restart to let a crashed resource come back up to three times in five minutes |
2. The server half
Section titled “2. The server half”const KIT = [ { form: 0x1f66a, count: 5 }, { form: 0x4835, count: 2 },];
Events.on("playerReady", (player) => { Chat.sendToPlayer(player, `Welcome to the Commonwealth, ${player.nickname}. Type /kit.`, { author: "Kit" });});
Events.on("playerCommand", (player, command) => { if (command !== "kit") return; for (const entry of KIT) player.giveItem(entry.form, entry.count); Chat.sendToPlayer(player, "Supplies issued.", { author: "Kit" });});
Events.on("playerDeath", (player, killer) => { if (killer) Chat.sendToAll(`${killer.nickname} killed ${player.nickname}.`, { author: "Kit" }); setTimeout(() => player.respawn(), 5000);});Three things worth noticing:
playerReady, notplayerConnect. A connecting player’s entity exists before it has a position.playerReadyis the moment they stand in the world, so it is where you greet, dress and equip them.- Nothing respawns a player on its own. Death is a rule your server decides, so
playerDeaththat does nothing leaves the player lying there. player.giveItemtakes a form ID. A number like0x1f66a, or the editor ID as a string.
console.log from a resource is prefixed with the resource’s name in the server log, which is how
you tell whose line it was.
3. The client half
Section titled “3. The client half”Client scripts are sent to every connected player and run inside their own game. They see a
different API: no Player class and no Chat.sendToPlayer, but Key, Hud and the game’s own
menus.
Key.bind("f8", "down", () => { Events.emitServer("kit:asked");});
Events.on("kit:issued", (payload) => { Hud.notify(`Supplies issued: ${payload}`); Hud.showSubtitle("Check your Pip-Boy.", 3);});Key.bind only fires while the game has foreground input and no menu is capturing the keyboard, so
a bind cannot steal a key from the Pip-Boy or from chat.
4. The round trip
Section titled “4. The round trip”The two halves talk over named events. The client sends with Events.emitServer, and the server
listens on a separate namespace, Events.onClient, which native events can never collide with:
const KIT = [ { form: 0x1f66a, count: 5 }, { form: 0x4835, count: 2 },];
const give = (player) => { for (const entry of KIT) player.giveItem(entry.form, entry.count); // Back to that one player's game. The payload is JSON text, so a plain string is stringified first. player.emit("kit:issued", JSON.stringify(`${KIT.length} kinds`));};
Events.on("playerReady", (player) => { Chat.sendToPlayer(player, `Welcome, ${player.nickname}. Press F8 or type /kit.`, { author: "Kit" });});
Events.on("playerCommand", (player, command) => { if (command === "kit") give(player);});
Events.onClient("kit:asked", (player) => { give(player);});
Events.on("playerDeath", (player, killer) => { if (killer) Chat.sendToAll(`${killer.nickname} killed ${player.nickname}.`, { author: "Kit" }); setTimeout(() => player.respawn(), 5000);});| Direction | Server | Client |
|---|---|---|
| Client to server | Events.onClient(name, handler) |
Events.emitServer(name, payload) |
| Server to one player | player.emit(name, payload) |
Events.on(name, handler) |
| Server to everyone | Events.emitAllClients(name, payload) |
Events.on(name, handler) |
// The first argument of an onClient handler is always the player who sent it.Events.onClient("kit:asked", (player, ...rest) => {});Payloads travel as JSON text. Events.emitServer and Events.emitAllClients serialize anything
that is not a string for you; player.emit takes the JSON text itself, so pass it through
JSON.stringify. A string you pass to any of them is sent as is and must already be JSON: the
receiving side parses it before your handler runs and drops an event that does not parse, with only
a line in its log. So the handler always receives the decoded value, never the JSON text:
// Serverplayer.emit("kit:issued", JSON.stringify("2 kinds")); // arrives as the string "2 kinds"player.emit("kit:stock", JSON.stringify({ stimpaks: 5 })); // arrives as an objectplayer.emit("kit:issued", "2 kinds"); // not JSON: dropped on the client5. Load it
Section titled “5. Load it”In the server console:
refreshensure survival-kitrefresh re-scans fmp-resources/ for manifests it has not seen. ensure starts the resource, or
reloads it in place if it is already running, pushing new client files to everyone connected. After
the first ensure, editing a file and running ensure survival-kit again is the whole loop.
[survival-kit] started6. Store something on a player
Section titled “6. Store something on a player”A state bag is a small key-value store attached to an entity, and the scope decides who gets to
see it:
Events.on("playerReady", (player) => { // 'owner': only this player's game receives it. 'broadcast': everyone's does. 'server': nobody's. player.state.set("kitsIssued", 0, { scope: "owner" });});
Events.on("playerCommand", (player, command) => { if (command !== "kit") return; const issued = player.state.get("kitsIssued") ?? 0; player.state.set("kitsIssued", issued + 1, { scope: "owner" });});Add "server/state.js" to serverScripts to run it. A write raises entityStateChange on the
server and, for a scope that reaches them, in the receiving clients too, so UI can follow a value
without polling it.
What to read next
Section titled “What to read next”- Use TypeScript: the same resource, with autocomplete and a compiler that catches a wrong argument before the server does.
- Structure a larger resource: where files go once there is more than one feature.
- Server API and Client API, in the header: everything either half can call.