Settings and per-town data

A mod can offer settings the player changes on the Mods tab. Its script can keep data per town.

Settings #

Declare up to 16 settings in mod.json. The Mods tab shows them on your mod's row under Settings. Your script reads them with settings.get(name).

"settings": [
  { "name": "greet", "type": "bool", "label": "Say hello", "default": true, "scope": "client" },
  { "name": "price", "type": "number", "label": "Fruit price", "description": "Bells per fruit.",
    "default": 600, "min": 100, "max": 5000 },
  { "name": "drink", "type": "choice", "label": "Drink", "default": "tea", "choices": ["tea", "coffee"] },
  { "name": "motto", "type": "text", "label": "Shop motto", "default": "", "max_length": 40 }
]
  • name is 1 to 32 characters from a-z 0-9 _, starting with a letter.
  • label is 1 to 60 characters. description is up to 200.
  • bool has a default of true or false.
  • number has whole numbers for min, max and default.
  • choice has 2 to 16 choices and a default that is one of them.
  • text has a max_length of 1 to 200 and a one-line default.
  • The game reads settings when it starts. A change is used from the next start, like every mod change.
  • A signed-in player's settings follow their account to their other browsers and devices, with the mod. On the other device, each value goes through the same checks as a change in the Mods tab. It is always a value your declaration allows.
  • A setting can say whose it is on a friend visit, with "scope". See the next section.

World settings and client settings #

A setting is "scope": "world" or "scope": "client". A setting with no scope is a world setting.

World settingClient setting
Whose it isThe town's.The player's own.
A friend visitCompares it. Both towns need the same value.Never compares it.
A visitor who borrows the host's modsPlays with the host's value.Keeps their own value. It is the one set on their installed copy of the mod, or the default when they do not have the mod.
Leaves the browserYes, on a visit, to the friend.No. It is never sent on a visit.
What the Mods tab says under it"Part of your town.""Just for you."
  • Use client for a choice that only changes what its own player sees or hears: whether a villager says your line, which wording, a display choice.
  • Use world, or no scope, for anything that decides what the town gets or gives up: what a letter says or carries, what is given or taken, when.
  • On a visit, the scope matters only for a World mod that counts. A visit never looks at a Just you mod.
  • The rule below holds on every mod, a Just you mod too. Leave scope off a setting that decides what is sent, given or taken.
  • A World mod with only client settings matches a friend's copy whatever each of them picked.

What a client setting may decide #

A client setting may not decide what the town gets. The game checks this one event at a time. In an event that read a client setting with settings.get, cmd.send_letter, cmd.give_item, cmd.take_item and cmd.take_bells are refused. The call returns nil, a message and the code command-client-setting. cmd.show_line and cmd.ask still work, because only the player sees them.

-- "gift" is a client setting: this give_item is refused
on("day_started", function(e)
  local id, why, code = cmd.give_item(settings.get("gift") == "pear" and 0x2802 or 0x2800)
  -- id is nil, code is "command-client-setting"
end)

Per-town data #

  • store is kept per town. Each mod has 8 KB per town.
  • The store travels with the town in its cloud save and backups, and in Export with mod data under the save button's More options. It is never written into the save file itself.
  • store.player is one per mod, per town, per player. A town has up to 4 residents, and they share store. Keep in store.player what one resident earns alone. It lives inside the same 8 KB.
  • game.player().id is the number that tells residents apart.
  • When a resident is deleted, the game deletes what your mod saved for them in store.player. That room goes back to your 8 KB.
  • A new resident in that save slot starts with nothing. The game can give them the id the deleted resident had, so do not treat an id as proof of who a resident was.
  • store.player.set needs a player in the game. With a town loaded and no player yet, game.player() is nil, store.player.get returns nil and store.player.set raises an error.
  • store.client is one per browser, whatever town is loaded.
  • Every store.set, store.player.set and store.client.set writes your mod's file in the save folder before it returns.
  • The browser keeps the save folder at the game's next save sync. That is a game save. For the cloud, mod data uploads at most every 2 minutes. A tab closed before that sync loses the last sets.
  • The main part of your script runs before the town loads. There, store.get answers nil and store.set is refused. Read and save from events.
  • store.town() tells you which town the store belongs to. Use it to reload what you keep in locals when the player moves to another town.
  • On a visit to a friend's town and in The City, store starts empty and is not kept.
  • A browser keeps mod data for 16 towns. A 17th drops the data of the other town that was written least recently.
  • Removing a mod asks the player whether to keep its data or delete it. Installing the mod again finds data that was kept.
main.lua
-- what you keep in locals is read at the first event in each town
local town, days = false, 0
local function fresh()
  if store.town() == town then return end
  town = store.town()
  days = store.get("days") or 0
end

-- day_started can fire twice for one day when the game was not saved, so
-- count a day by its date, not by the call
on("day_started", function(e)
  fresh()
  local today = string.format("%04d-%02d-%02d", e.year, e.month, e.day)
  if store.get("last_day") ~= today then
    store.set("last_day", today)
    days = days + 1
    store.set("days", days)      -- this town's store
  end
  store.client.set("seen", true) -- this browser's, whatever town
end)

Moving your data forward #

When you change the shape of what you save, raise data_version. In each town the game calls your data_migrated handler once. e.from is the version the town's data was saved at, and e.to is the new one.

It does not run at the restart. It runs as the town loads, before any other event in that town.

"data_version": 2,
"scripts": { "main": "main.lua", "events": ["data_migrated", "day_started"] }