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.
The layout
Section titled “The layout”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 editedThe entry point imports and nothing else, so a feature is added by adding a file:
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.
The manifest
Section titled “The manifest”{ "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 |
Sharing between resources
Section titled “Sharing between resources”Three mechanisms, in order of how tightly they couple things:
// 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:
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.
Cleaning up
Section titled “Cleaning up”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:
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.
The loop
Section titled “The loop”refresh # pick up a resource the server has not seenensure my-gamemode # start it, or reload it in placestop my-gamemode # take it down without touching the othersCompile 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.
Related
Section titled “Related”- Use TypeScript: the two tsconfig files this layout expects.
- Write your first resource: the smaller version of all of this.