Skip to content
FMFallout Multiplayer Scripting APIScripting guides and API
GuideMaintainer authored

Structure a larger resource

How a resource is laid out once it has more than one feature: the manifest, server and client halves, exports, dependencies and the reload loop.

One file is fine for a command or two. Past that, the shape below is what the default gamemode uses and what the rest of these guides assume.

fmp-resources/
my-gamemode/
package.json the manifest, and the npm project
tsconfig.json server half
tsconfig.client.json client half
src/
server/
index.ts entry point: imports the features, owns nothing
commands.ts one file per feature
spawns.ts
economy.ts
client/
index.ts
hud.ts
web/ HTML and images served to players, if any
dist/ the emit; never committed, never edited

The entry point imports and nothing else, so a feature is added by adding a file:

src/server/index.ts
import "./commands";
import "./spawns";
import "./economy";

Each feature module registers its own handlers at load time. No feature reaches into another’s state; they talk through events or exports.

package.json
{
"name": "my-gamemode",
"version": "0.1.0",
"private": true,
"mafiahub": {
"serverScripts": ["dist/server/index.js"],
"clientScripts": ["dist/client/index.js"],
"files": ["web/menu.html", "web/logo.png"],
"priority": 10,
"errorBehavior": "stop",
"exports": ["giveKit"],
"resourceDependencies": ["economy"]
}
}
Key What it decides
serverScripts What the server runs, in the order listed
clientScripts What is sent to every player’s game and run there
files What players may download besides scripts: HTML, CSS and images for a browser view
priority Higher starts earlier. Give a library resource a higher priority than the gamemode that reads it
errorBehavior stop by default. restart lets a crashed resource come back, up to three times in five minutes
exports Names this resource publishes for others, declared so a missing one is a clear error
resourceDependencies Resources that must be running first. An undeclared import logs a warning

Three mechanisms, in order of how tightly they couple things:

src/server/economy.ts
// Values and functions other resources may call. Declare them in `exports` too.
Exports.register("balanceOf", (player: Player) => player.state.get("caps") ?? 0);
// A request with a reply, which is the right shape when the answer matters.
Messages.handle("charge", (payload, reply) => {
reply({ ok: true });
});
// Fire and forget, to everyone listening, within this server.
Events.on("economy:paid", (...args: unknown[]) => {
console.log("paid", args.length);
});

Reading from the other side:

src/server/shop.ts
const balanceOf = Exports.get("economy", "balanceOf");
const everything = Imports.get("economy");
async function buy(player: Player, price: number): Promise<void> {
const answer = await Messages.request("economy", "charge", { player: player.id, amount: price });
console.log("charge answered", answer);
}

Use Events.emitLocal and Events.onLocal for a resource’s own internal events: that namespace is private to the resource, so two resources can both have a ready event without colliding.

A resource that is reloaded must leave nothing behind. Timers, event handlers and exports registered by a resource are cleaned up for you when it stops, but anything you placed in the world is not:

src/server/spawns.ts
Events.on("resourceStop", (resourceName: string) => {
if (resourceName !== "my-gamemode") return;
// Remove what this resource spawned; the next start will place it again.
});

resourceStart runs after the entry point and immediately before the resource counts as running, which is the moment to announce yourself to other resources.

Server console
refresh # pick up a resource the server has not seen
ensure my-gamemode # start it, or reload it in place
stop my-gamemode # take it down without touching the others

Compile first, then ensure. The two are separate on purpose: a half-written file never reaches a connected player because a watcher happened to save it.