Files
bworld/docs/modding/datastorage/index.md
T
2026-09-29 23:29:14 -03:00

1.9 KiB

Saving Data

Server scripts have no file access, so everything a mod keeps goes through the world, and is saved with it. Pick the place by what the data belongs to:

Data belongs to Keep it in Sent to players
one block its tile data, block.data never
one block, small and visible a block state yes
a set of items a container while a screen shows it
one item stack the stack's data with the stack
the whole mod ctx.storage never

Everything here must be plain JSON: objects, arrays, strings, numbers, booleans and null. No classes, Maps or functions.

Mod Storage

ctx.storage is a small key value store for each mod, saved with the world:

interface ModStorage {
	get<T>(key: string): T | undefined;
	set(key: string, value: unknown): void; // JSON values only
	delete(key: string): void;
}
const count = ctx.storage.get<number>("smelted") ?? 0;
ctx.storage.set("smelted", count + 1);

Each mod has its own, so keys don't clash between mods. It's for things like settings and totals. Data about one block belongs in that block's tile data, which goes away with the block.

What Isn't Saved

  • Variables in a script. Everything in memory is gone when the server restarts, so rebuild it in setup from what was saved.
  • Timers.
  • Scripted entities, like the fishing hook.
  • get_lore text, which is worked out each time a stack is sent.