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 }
]on("villager_talked", function(e)
if not settings.get("greet") then return nil end
return { place = "before", line = "Fruit is " .. settings.get("price") .. " Bells today!" }
end)nameis 1 to 32 characters froma-z 0-9 _, starting with a letter.labelis 1 to 60 characters.descriptionis up to 200.boolhas adefaultof true or false.numberhas whole numbers formin,maxanddefault.choicehas 2 to 16choicesand adefaultthat is one of them.texthas amax_lengthof 1 to 200 and a one-linedefault.- 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 setting | Client setting | |
|---|---|---|
| Whose it is | The town's. | The player's own. |
| A friend visit | Compares it. Both towns need the same value. | Never compares it. |
| A visitor who borrows the host's mods | Plays 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 browser | Yes, 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
clientfor 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 noscope, 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
scopeoff 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)-- "hello" is a client setting: it only decides what this player hears
on("villager_talked", function(e)
if settings.get("hello") == false then return nil end
return { place = "before", line = "Fruit is " .. settings.get("price") .. " Bells today!" }
end)Per-town data #
storeis 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.playeris one per mod, per town, per player. A town has up to 4 residents, and they sharestore. Keep instore.playerwhat one resident earns alone. It lives inside the same 8 KB.game.player().idis 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.setneeds a player in the game. With a town loaded and no player yet,game.player()isnil,store.player.getreturnsnilandstore.player.setraises an error.store.clientis one per browser, whatever town is loaded.- Every
store.set,store.player.setandstore.client.setwrites 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.getanswersnilandstore.setis 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,
storestarts 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.
-- 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"] }on("data_migrated", function(e)
-- version 1 kept "n" as text; version 2 keeps a number
if e.from < 2 and type(store.get("n")) == "string" then
store.set("n", tonumber(store.get("n")) or 0)
end
end)