Lua API
The globals a script can call, and the sandbox around it.
Scripts are Lua 5.4. A handler never changes the game directly. It returns data, or it queues a command
(cmd.*). The game applies a queued command at the end of a frame's update, before the frame is drawn. A
script cannot reach the network, files or the page. No event runs every frame. playac.lua has the types for your editor.
Index #
| Name | What it does |
|---|---|
on | Registers a handler for an event. |
log | Writes a line to the Mods tab's Mod log and the browser console as "[Mods] <id>: ...". |
message | A line from a messages file. |
rng | Seeded random numbers. |
game.time | The game's clock now. |
game.day_number | The game clock's day as one whole number: days since 2000-01-01. |
game.pockets | The items the player holds, in slot order: { { slot, item, mod_item }, ... |
game.bells | The wallet, or nil before a town is loaded. |
game.town | The town: { name, id, year, month, day, hour, minute, weekday, season }, or nil before a town is loaded. |
game.villagers | The town's villagers: { { id, name, species, personality }, ... |
game.player | The player playing this game: who they are, where they stand, what they hold and what they are doing, or nil before a town is loaded. |
game.calendar_today | The ids of this mod's own calendar days that fall on the town's date today, in file order. |
game.weather | The weather where the player is now: { kind, intensity }, or nil before a town is loaded. |
game.place | Where the player is now: { scene, name, island, acre, acre_x, acre_z, unit_x, unit_z }, or nil before a town is loaded. |
game.input | The player's game inputs right now: { a, b, x, y, z, l, r, start, dpad_up, dpad_down, dpad_left, dpad_right, c_up, c_down, c_left, c_right, stick_x, stick_y }. |
store.get | A value this mod saved in this town, or nil. |
store.set | Saves a string, number or boolean under a key in this town. |
store.town | Which town the store is for now, as an opaque text that changes when the town does ("v:..." away from home). |
store.client.get | A value this mod saved in this browser, or nil. |
store.client.set | Saves a value in this browser. |
store.player.get | A value this mod saved for this player in this town, or nil. |
store.player.set | Saves a value for this player in this town. |
settings.get | A setting's value: the player's, else the default. |
remote.add | Offers a function to other mods, while the script loads. |
remote.call | Calls a function another running mod offers. |
remote.has | Whether a running mod offers that function. |
remote.version | Whether a mod this one names in depends or optional_depends is present, and at what version. |
cmd.send_letter | Queues a letter in the player's own mailbox. |
cmd.give_item | Queues an item into the first empty pocket. |
cmd.show_line | Queues a line shown in a message box with no one speaking, like the game's own "My pockets are full!", once the player is free. |
cmd.ask | Queues a question with 2 to 4 answers in that same speakerless message box. |
cmd.take_item | Asks the player to hand over items from their pockets. |
cmd.take_bells | Asks the player to pay Bells from their wallet. |
cmd.save | Asks the game to save the town, as the Autosave setting does, for a mod such as a save-anywhere button. |
cmd.set_weather | Sets the town's weather for the rest of the game day: kind "clear", "rain" or "snow", and for rain or snow an intensity, "light" (when left out) or "heavy". |
cmd.play_sound | Plays one of the game's own sound effects, by its name in sounds.json: a jingle, a menu tick, a page turn. |
cmd.set_shop_goods | Sets a shop's goods for the rest of the game day, as one more block after the mod's shops.json blocks for that shop. |
cmd.set_shop_open | Opens or shuts a shop for the rest of the game day, as one more block after the mod's shops.json blocks for that shop. |
cmd.set_hat | Puts a picture from the mod's own files on the hat of the player playing this game, in place of the design or the shirt the hat wears. |
cmd.set_shirt | Puts a picture from the mod's own files on the shirt of the player playing this game, in place of the clothes they wear. |
cmd.react | Plays one of the reactions from the reactions bar on the player's own character: "hooray", "dance", "hug", "thanks", "ouch", "trip", "spin", "hide" or "pose". |
timer.after | Sets a timer that fires once, at least that many game minutes from now. |
timer.cancel | Cancels a waiting timer of this mod in this town. |
Functions #
onsince 1 #
on(event: string, fn: function) Registers a handler for an event. Call it while the mod's own script loads, not from inside a handler and not from a function another mod calls with remote.call. The event must be in mod.json's scripts.events. At most 16 handlers per event. A handler gets one table, e, holding the event's fields.
| Parameter | Type | Description |
|---|---|---|
event | string | An event name. |
fn | function | Called with one table, e, holding the event's fields. |
logsince 1 #
log(...) Writes a line to the Mods tab's Mod log and the browser console as "[Mods] <id>: ...". Arguments are joined with spaces. A line is cut at 299 bytes. At most 20 lines per event and 500 per session. A refused cmd.* or timer.* call writes a line of its own, and those lines count toward the same two limits. A script error or stop is written to the Mod log too, with a Lua traceback, whatever the limits. The log keeps the newest 400 lines of all mods. When it is full, the oldest line of the mod with the most lines goes first, so one mod cannot push out another mod's lines.
| Parameter | Type | Description |
|---|---|---|
... | any | Values to print. |
messagesince 1 #
message(key: string) -> string|nil A line from a messages file. "key" reads this mod's own line, and "author.mod:key" reads another mod's. Returns nil when there is no such line.
| Parameter | Type | Description |
|---|---|---|
key | string | "key" or "author.mod:key". |
Returns string|nil
rngsince 1 #
rng(m: integer|nil, n: integer|nil) -> number|integer Seeded random numbers. rng() gives a number in [0,1), rng(n) a whole number 1..n and rng(m, n) a whole number m..n. The seed is the mod, the town, the game day and how many numbers this mod has drawn in this town on this game day. Each call in a day therefore gives another number, and a reload cannot reroll. The count is saved with the town store at the end of a run that drew numbers, and a reload carries on from it. If the tab closes before that write, the same numbers come again, never new ones. A new game day starts the count at 0, and changing the game's clock gives other numbers. The count is kept in memory only away from home, for a lent mod and before a town loads. Inside a remote.call, the called mod draws from its own counter. math.random and math.randomseed are not available. Use rng().
| Parameter | Type | Description |
|---|---|---|
m | integer|nil | |
n | integer|nil |
Returns number|integer
The game table #
game: Read-only facts about the game.
game.timesince 1 #
game.time() -> table The game's clock now.
Returns table
| Field | Type | Description |
|---|---|---|
year | integer | |
month | integer | 1 to 12. |
day | integer | 1 to 31. |
hour | integer | 0 to 23. |
minute | integer | 0 to 59. |
weekday | integer | 0 (Sunday) to 6. |
game.day_numbersince 1 #
game.day_number() -> integer The game clock's day as one whole number: days since 2000-01-01. It goes up by one each game day, so it tells one day from the next without date maths.
Returns integer
game.pocketssince 1 #
game.pockets() -> table The items the player holds, in slot order: { { slot, item, mod_item }, ... }. Only held items are listed, so an empty slot has no entry. Returns {} when the pockets are empty or before a town is loaded.
Returns table
game.bellssince 1 #
game.bells() -> integer|nil The wallet, or nil before a town is loaded.
Returns integer|nil
game.townsince 1 #
game.town() -> table|nil The town: { name, id, year, month, day, hour, minute, weekday, season }, or nil before a town is loaded. season is spring, summer, fall or winter, the word talk pools and shops use.
Returns table|nil
| Field | Type | Description |
|---|---|---|
name | string | |
id | integer | |
year | integer | |
month | integer | |
day | integer | |
hour | integer | |
minute | integer | |
weekday | integer | |
season | string|nil | spring, summer, fall or winter. nil when the game cannot tell. |
game.villagerssince 1 #
game.villagers() -> table The town's villagers: { { id, name, species, personality }, ... }. Returns {} before a town is loaded.
Returns table
game.playersince 1 #
game.player() -> table|nil The player playing this game: who they are, where they stand, what they hold and what they are doing, or nil before a town is loaded. A town has up to 4 residents, each playing it from their own save slot. Every call reads the game as it is now, so it is cheap to call from any event, such as button_pressed or timer_fired. On a visit it is the visitor, standing in the host's town. Where the player stands and what they do (every field after id) is nil while the scene is still being built, such as in day_started as the game starts.
Returns table|nil
| Field | Type | Description |
|---|---|---|
name | string | |
id | integer | Stays the same for that resident in that town. It holds the resident's save slot. A new resident who later takes a freed slot can get the same id, and never gets the old resident's store.player data: the game deletes that when a resident is deleted and again when a new one is made in the slot. On a visit it is the visitor's own number, never equal to a resident's. |
x | number|nil | Where the player stands, east to west, in the game's units: a ground tile is 40 across, and an acre 16 tiles (640). It grows to the east. Inside a building it is the room's own. |
y | number|nil | The player's height in the same units. It changes on a cliff or a slope. |
z | number|nil | Where the player stands, north to south, in the same units. It grows to the south, down the screen. |
angle | integer|nil | Which way the player faces, in whole degrees from 0 to 359: 0 faces south (toward the camera), 90 east, 180 north and 270 west. |
acre | table|nil | The town acre the player stands in, as the map counts them. nil in any scene but the town. |
acre.x | integer | The acre's column, 1 to 5 from west to east. 0 and 6 are the town's edges. |
acre.z | integer | The acre's row, 1 to 6 from north to south: 1 is the row with the train station (A on the map), 6 the beach row (F). 0 is the edge above, and 7 and up are the sea and the island. |
scene | string|nil | The scene's name from scenes.json, like "town", as scene_entered names it. nil for a scene with no published name. |
tool | string|nil | The tool in the player's hand: "axe", "net", "rod", "shovel" or "umbrella". A golden tool counts as its kind. nil when the hand is empty or holds something else, such as a balloon, a pinwheel or a fan. |
state | string|nil | What the player is doing, in one word: "standing", "walking", "running", "sitting" (on a chair, a sofa, a rock or a stump), "lying" (on a bed), "wading" (walking over into the next acre while the screen scrolls; the town has no water a player can wade into) or "busy" (anything else, such as a tool swing, picking something up, a talk, a door or a cutscene). The list may grow: treat a word you do not know as "busy". |
game.calendar_todaysince 1 #
game.calendar_today() -> table The ids of this mod's own calendar days that fall on the town's date today, in file order. Returns {} when there are none, or before a town is loaded. Only the calling mod's days are listed.
Returns table
game.weathersince 1 #
game.weather() -> table|nil The weather where the player is now: { kind, intensity }, or nil before a town is loaded. kind is clear, rain or snow, the words shops and talk pools use: cherry blossom petals and falling leaves count as clear. Indoors it is the weather outside, on the island it is the island's, and on a visit it is the weather of the town being visited. Reading it changes nothing and needs no permission.
Returns table|nil
| Field | Type | Description |
|---|---|---|
kind | string | clear, rain or snow. |
intensity | string | light, normal or heavy for rain and snow. none when clear. |
game.placesince 1 #
game.place() -> table|nil Where the player is now: { scene, name, island, acre, acre_x, acre_z, unit_x, unit_z }, or nil before a town is loaded. scene and name are the scene, as scene_entered gives them. The acre fields are there only outdoors in the town or on the island, standing in an acre the town map shows. It names the acre and the square inside it, one of the 16 by 16 squares of an acre, the way the town map does; game.player() gives the exact position. Both are only ever the player's own: a mod never sees where another player is. On a visit it is the place in the town being visited, the same town game.town() names. Reading it changes nothing and needs no permission.
Returns table|nil
| Field | Type | Description |
|---|---|---|
scene | integer | The scene number (scenes.json). |
name | string|nil | The scene's name from scenes.json, like "town". nil for a scene with no published name. |
island | boolean | true on the island, outdoors or in a cottage. |
acre | string|nil | The acre as the town map labels it: the row letter and the column, "A-1" to "F-5". On the island "island-1" or "island-2", from the left. nil indoors. |
acre_x | integer|nil | The acre's column: 1 to 5 in town, 1 or 2 on the island. nil indoors. |
acre_z | integer|nil | The acre's row: 1 (A) to 6 (F). nil on the island and indoors. |
unit_x | integer|nil | The unit inside the acre, 0 (west) to 15 (east). nil indoors. |
unit_z | integer|nil | The unit inside the acre, 0 (north) to 15 (south). nil indoors. |
game.inputsince 1 #
game.input() -> table|nil The player's game inputs right now: { a, b, x, y, z, l, r, start, dpad_up, dpad_down, dpad_left, dpad_right, c_up, c_down, c_left, c_right, stick_x, stick_y }. Each button is true while held. The player's game inputs are the controller the game itself reads, so a keyboard key, a controller button and the on-screen touch pad are all the same button here, with the player's own key and button settings applied. A mod never sees which key was typed: the keyboard reaches it only through the buttons it plays, and nothing reaches it while the game's text editor is open (writing a letter or a name, by keyboard or by controller). It is nil while the text editor is open. Use it for a stick or a combination, such as L held while A is pressed; use button_pressed for a single press. It reads the controller when it is called, which in a button event that waited for a busy frame is a frame or two after the press.
Returns table|nil
| Field | Type | Description |
|---|---|---|
a | boolean | |
b | boolean | |
x | boolean | |
y | boolean | |
z | boolean | |
l | boolean | |
r | boolean | |
start | boolean | |
dpad_up | boolean | |
dpad_down | boolean | |
dpad_left | boolean | |
dpad_right | boolean | |
c_up | boolean | |
c_down | boolean | |
c_left | boolean | |
c_right | boolean | |
stick_x | number | The main stick, from -1 (left) to 1 (right). 0 at rest. A keyboard's movement keys tilt it fully. |
stick_y | number | The main stick, from -1 (down) to 1 (up). 0 at rest. |
The store table #
store: A small saved table per mod, per town. It holds strings, numbers and booleans, up to 8 KB per mod per town. It travels with the town (cloud save, backups, Export with mod data). No town is loaded while the main chunk runs, so get answers nil and set raises an error there. On a visit or in The City it lives in memory only. store.player is this player's part of it. store.client is a separate store per browser. A key cannot start with a control character (a byte under 0x20). Every store.set, store.player.set and store.client.set writes the mod's file in the save folder before it returns. The browser keeps the save folder in IndexedDB at the game's next save sync. That sync happens at a game save, and for the cloud at a mod-data upload at most every 2 minutes. A tab closed before that sync loses the last sets. Daily command counts and the rng counter are written the same way.
store.getsince 1 #
store.get(key: string) -> string|number|boolean|nil A value this mod saved in this town, or nil. It is nil while the main chunk runs. Integers come back as integers.
| Parameter | Type | Description |
|---|---|---|
key | string |
Returns string|number|boolean|nil
store.setsince 1 #
store.set(key: string, value: string|number|boolean|nil) Saves a string, number or boolean under a key in this town. nil deletes the key. Keys are up to 64 bytes and strings up to 1024. A set that would take this town's store over 8 KB raises an error and changes nothing. So does a set before a town is loaded, in the main chunk.
| Parameter | Type | Description |
|---|---|---|
key | string | |
value | string|number|boolean|nil |
store.townsince 1 #
store.town() -> string|nil Which town the store is for now, as an opaque text that changes when the town does ("v:..." away from home). nil before a town is loaded. Compare it to reload what you keep in locals when the player moves to another town.
Returns string|nil
store.client.getsince 1 #
store.client.get(key: string) -> string|number|boolean|nil A value this mod saved in this browser, or nil.
| Parameter | Type | Description |
|---|---|---|
key | string |
Returns string|number|boolean|nil
store.client.setsince 1 #
store.client.set(key: string, value: string|number|boolean|nil) Saves a value in this browser. nil deletes it. 8 KB per mod.
| Parameter | Type | Description |
|---|---|---|
key | string | |
value | string|number|boolean|nil |
store.player.getsince 1 #
store.player.get(key: string) -> string|number|boolean|nil A value this mod saved for this player in this town, or nil.
| Parameter | Type | Description |
|---|---|---|
key | string |
Returns string|number|boolean|nil
store.player.setsince 1 #
store.player.set(key: string, value: string|number|boolean|nil) Saves a value for this player in this town. nil deletes it. The key and value rules are the same as for store.set.
| Parameter | Type | Description |
|---|---|---|
key | string | |
value | string|number|boolean|nil |
The settings table #
settings: The mod's settings from mod.json "settings", as the player set them on the Mods tab. They are read when the game starts.
settings.getsince 1 #
settings.get(name: string) -> boolean|integer|string|nil A setting's value: the player's, else the default. It is a boolean, an integer or a string. nil for a name the mod does not declare.
| Parameter | Type | Description |
|---|---|---|
name | string |
Returns boolean|integer|string|nil
The remote table #
remote: Calls between mods. One mod offers named functions and another calls them. Values are deep copies of nil, booleans, numbers, strings and tables of them, within 64 KB, 16 levels, 4096 values and 8 results. The called function runs on the caller's budget and clock, and going over stops the caller. Its commands count toward the caller's per-event limit. Its rng draws from its own counter. A call into a mod that is already running is refused.
remote.addsince 1 #
remote.add(name: string, fn: function) Offers a function to other mods, while the script loads. Names are 1 to 32 of a-z 0-9 _, at most 32 per mod. With mod.json "exports", only the names it lists.
| Parameter | Type | Description |
|---|---|---|
name | string | |
fn | function |
remote.callsince 1 #
remote.call(mod: string, name: string, ...) -> any Calls a function another running mod offers. Its errors come back as yours, and it keeps running. When the mod is one of this mod's optional_depends and it is absent, nothing is called and nothing is raised: the call returns nil, a message and a code from dependency_codes.
| Parameter | Type | Description |
|---|---|---|
mod | string | |
name | string | |
... | any | Copied arguments. |
Returns any
remote.hassince 1 #
remote.has(mod: string, name: string) -> boolean Whether a running mod offers that function.
| Parameter | Type | Description |
|---|---|---|
mod | string | |
name | string |
Returns boolean
remote.versionsince 1 #
remote.version(mod: string) -> string|nil Whether a mod this one names in depends or optional_depends is present, and at what version. Returns its version, such as "1.2.0", when it loaded inside the range. Returns nil, a message and a code from dependency_codes when it did not. Ask in an event. In the main chunk an optional dependency that the cycle rule placed after this mod has loaded no script yet.
| Parameter | Type | Description |
|---|---|---|
mod | string |
Returns string|nil: The version, or nil.
The cmd table #
cmd: The commands. Each queues an effect that the game applies at the end of a frame's update, before the frame is drawn. Each needs its name in mod.json permissions. A call returns the command's id. When it is refused at once, it returns nil, a message and a code (command_codes). In The City every command but set_hat, set_shirt and react is refused at once. On a visit send_letter, give_item, take_item, take_bells, save and set_weather are refused at once. show_line, ask and play_sound still run on a visit. A command refused at once counts against no daily limit. A command queued at home and applied after the player left comes back as a failed command_finished, and its count is given back.
cmd.send_lettersince 1 #
cmd.send_letter(t: table) -> integer|nil Queues a letter in the player's own mailbox. The post office keeps it when the mailbox is full. The save holds an ordinary letter, so it loads without the mod.
| Parameter | Type | Description |
|---|---|---|
t | table | { text, item?, from?, paper? } or { template }. text is 1 to 192 characters fitting 6 lines. item is a number or "0x" and four hex digits. from is 1 to 8 characters on one line ("A friend" when left out). paper is 0 to 63. template is one of this mod's own letters.json letters, by its id or "<this mod's id>:<id>". The header, body, sign-off, present and paper then all come from the template. |
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.give_itemsince 1 #
cmd.give_item(item: integer|string|table) -> integer|nil Queues an item into the first empty pocket. With full pockets it comes by letter. Also takes this mod's own new item by its full id (author.mod:thing). A mod item is given with its identity, as a shop sells it, and full pockets refuse it.
| Parameter | Type | Description |
|---|---|---|
item | integer|string|table | An item number, "0x" and four hex digits, this mod's "author.mod:thing", or { item, from? }. |
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.show_linesince 1 #
cmd.show_line(text: string) -> integer|nil Queues a line shown in a message box with no one speaking, like the game's own "My pockets are full!", once the player is free. One line or question at a time.
| Parameter | Type | Description |
|---|---|---|
text | string | 1 to 400 bytes. |
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.asksince 1 #
cmd.ask(t: table) -> integer|nil Queues a question with 2 to 4 answers in that same speakerless message box. The pick comes back as a question_answered event.
| Parameter | Type | Description |
|---|---|---|
t | table | { text, choices }. text is 1 to 120 characters. choices is 2 to 4 answers of 1 to 16 characters. |
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.take_itemsince 1 #
cmd.take_item(t: table) -> integer|nil Asks the player to hand over items from their pockets. The game shows its own question, which names the item, the count and this mod. The mod's name stands between quotes, and a mod item is named as the pockets show it. A script cannot change that text. On yes the items leave the pockets and command_finished says via taken. On no, or when the window closes with no pick, nothing is taken and command_finished carries command-declined. It is refused with command-not-held when the pockets do not hold that many. Only plain pocket slots count. A wrapped present or an item held for a villager's errand is never taken. Items a command cannot give cannot be taken either (money bags, turnips, tickets, grab bags, fossils). It is refused on a visit and in The City, so a lent mod can never take anything.
| Parameter | Type | Description |
|---|---|---|
t | table | { item, count? }. item is an item number, "0x" and four hex digits, or this mod's own "author.mod:thing". count is 1 to 15 and is 1 when left out. An item number matches plain pieces only, never a mod item standing on that piece. |
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.take_bellssince 1 #
cmd.take_bells(t: table) -> integer|nil Asks the player to pay Bells from their wallet. The game shows its own question, which names the amount and this mod. The mod's name stands between quotes. A script cannot change that text. On yes the Bells leave the wallet and command_finished says via taken. On no, or when the window closes with no pick, nothing is taken and command_finished carries command-declined. It is refused with command-not-held when the wallet holds less than the amount, so the wallet never goes below zero. The bank account and money bags in the pockets are never touched. It is refused on a visit and in The City, so a lent mod can never take anything.
| Parameter | Type | Description |
|---|---|---|
t | table | { amount }. amount is a whole number of Bells, 1 to 99999. |
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.savesince 1 #
cmd.save() -> integer|nil Asks the game to save the town, as the Autosave setting does, for a mod such as a save-anywhere button. The game writes the home save only where the player could save at Gyroid: outdoors in their own town, in free control, with no menu, talk or scene change. It writes a settled copy of the town and never changes the town being played, so nothing is lost or doubled however often it runs. command_finished says via saved, or via unchanged when nothing changed since the last save (nothing is written). It is refused with command-indoors inside a house, a shop or any place that is not the town outdoors, command-busy when the player is not in free control, command-dream in a dream, command-not-ready before a save is loaded or while the page is replacing the save, command-too-soon within commands.save_spacing_seconds of any mod's last save, and command-save-failed when the write itself fails. It is refused on a visit and in The City, because a visitor's game does not hold the town it stands in. It does not run what Gyroid's save does on top: a villager's mood and clothes reach the save at the next change of scene or Gyroid save.
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.set_weathersince 1 #
cmd.set_weather(kind: string, intensity: string|nil) -> integer|nil Sets the town's weather for the rest of the game day: kind "clear", "rain" or "snow", and for rain or snow an intensity, "light" (when left out) or "heavy". Heavy rain is a storm, with lightning in June to August, and heavy snow is a blizzard. The game changes it as its own weather changes: the last drops or flakes fall, then the new weather fades in with its light and sound. Villagers, fish, bugs and the rain sound follow it as they follow any rainy or snowy day. The next game day the game rolls its own weather again, and a day after rain or snow can bring a rainbow and a gyroid, as after a real rainy day. A reload the same day keeps it. Only weather the game itself could roll on that date is allowed. Clear is always allowed. Rain is allowed from February 25 to November 15, and snow from November 1 to February 24, but neither on the days the game keeps clear: January 1 to 7, April 1 to 8 (the cherry blossoms), April 20, October 31 and December 31. On an event day the event's weather holds, and the player's first day of work for Nook stays dry. Those are refused with command-weather. command_finished says via set, or via unchanged when the town already has that weather as game.weather() reads it: cherry blossom petals and falling leaves count as clear, so on those days "clear" is unchanged and the petals or leaves stay, and putting back the weather a mod read changes nothing. While a change of weather is under way the command waits for it to end. It is refused on a visit and in The City, because the host's game decides the weather and a visitor sees the host's. Needs scope world.
| Parameter | Type | Description |
|---|---|---|
kind | string | "clear", "rain" or "snow". |
intensity | string|nil | For rain or snow, "light" or "heavy". nil is light. For clear, nil or "none". |
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.play_soundsince 1 #
cmd.play_sound(name: string) -> integer|nil Plays one of the game's own sound effects, by its name in sounds.json: a jingle, a menu tick, a page turn. Only this player hears it: in a visit the friend does not, and nothing reaches the town or the save. Every sound in the list is one the game plays once and stops on its own, never a sound that loops. A sound changes nothing, so it does not wait for the player to be free: it plays at the end of the next frame's update, in a talk or a menu too. command_finished says via played. At most commands.sounds_per_event in one event and commands.sounds_per_minute in any minute for each mod (command-event-cap, command-sound-cap). A name not in sounds.json is refused with command-args. While an NES game is being played the game's sound is the NES game's, and a sound is refused with command-no-sound. It is refused in The City.
| Parameter | Type | Description |
|---|---|---|
name | string | A sound's name from sounds.json, like "item_get". |
Returns integer|nil: The command's id. When it is refused at once: nil, then a message and a code.
cmd.set_shop_goodssince 1 #
cmd.set_shop_goods(shop: string, goods: table|nil) -> boolean|nil Sets a shop's goods for the rest of the game day, as one more block after the mod's shops.json blocks for that shop. It is set at the call, with no queue, no daily cap and no command_finished, and applies from the next frame, also when the shop is shut, and is gone when the day changes or the game restarts, so set it again in day_started. nil clears it. Only for a shop with goods in the shop table, and refused when a mod later in the load order changes that shop. Needs scope world.
| Parameter | Type | Description |
|---|---|---|
shop | string | A shop id from the shop table, like "nook" or "redd". |
goods | table|nil | A list of { item, price? } (item: an item number, "0x" and four hex digits, or this mod's "author.mod:thing"), as many as the shop takes in a block. Or { goods = <that list>, own_goods = false } to make them the whole stock. nil clears. |
Returns boolean|nil: true when it is set. When it is refused: nil, then a message and a code.
cmd.set_shop_opensince 1 #
cmd.set_shop_open(shop: string, open: boolean|nil) -> boolean|nil Opens or shuts a shop for the rest of the game day, as one more block after the mod's shops.json blocks for that shop. true opens it, false shuts it, nil clears it. It is set at the call, with no queue, no daily cap and no command_finished, and applies from the next frame and is gone when the day changes or the game restarts. Only for a shop with open in the shop table, and refused when a mod later in the load order changes that shop. Needs scope world.
| Parameter | Type | Description |
|---|---|---|
shop | string | A shop id from the shop table, like "flower" or "barber". |
open | boolean|nil | true opens it, false shuts it, nil clears. |
Returns boolean|nil: true when it is set. When it is refused: nil, then a message and a code.
cmd.set_hatsince 1 #
cmd.set_hat(picture: string|nil) -> boolean|nil Puts a picture from the mod's own files on the hat of the player playing this game, in place of the design or the shirt the hat wears. nil takes it off. It is set at the call, with no queue, no daily cap and no command_finished, and shows from the next frame. The picture is the path of a PNG inside the mod, like "hats/stripes.png": exactly 32 x 32 pixels, every pixel opaque, at most 16 colours, as an Able Sisters design. Keep it outside the mod's textures folder. A mod's hat beats a design on the hat. When two mods set one, the mod later in the load order wins. The hat is this player's own, by who they are and their home town, so a hat set at home stays on when they visit a friend or go to The City. Only this player's own screen draws it: friends on a visit and players in The City see the hat as the game would draw it. Nothing is saved: the hat is gone when the game restarts. A reload of the mod's script in developer mode keeps it. Keep the player's pick in store.player and set it again from scene_entered. Away from home store.player starts empty, so set the hat only when the store holds a pick: a nil there is no reason to take it off. It changes only how the player looks, so it works in a Just you mod, on a visit and in The City.
| Parameter | Type | Description |
|---|---|---|
picture | string|nil | The path of a 32 x 32 PNG in this mod, like "hats/stripes.png", or nil to take the hat off. |
Returns boolean|nil: true when it is set. When it is refused: nil, then a message and a code.
cmd.set_shirtsince 1 #
cmd.set_shirt(picture: string|nil) -> boolean|nil Puts a picture from the mod's own files on the shirt of the player playing this game, in place of the clothes they wear. nil takes it off. It is set at the call, with no queue, no daily cap and no command_finished, and shows from the next frame. The picture is the path of a PNG inside the mod, like "shirts/stripes.png": exactly 32 x 32 pixels, every pixel opaque, at most 16 colours, as an Able Sisters design, and it wraps the body and the sleeves the way a design does. Keep it outside the mod's textures folder. It beats the clothes the player wears: putting on other clothes changes what is underneath, and the picture stays on top until the mod takes it off. The hat matches the shirt, as in the game, so with no design or mod hat on the hat it wears the picture too. When two mods set one, the mod later in the load order wins. The shirt is this player's own, by who they are and their home town, so a shirt set at home stays on when they visit a friend or go to The City. Only this player's own screen draws it: friends on a visit and players in The City see the clothes the player wears. Nothing is saved: the shirt is gone when the game restarts, and the save keeps the clothes the player wears. A reload of the mod's script in developer mode keeps it. Keep the player's pick in store.player and set it again from scene_entered. Away from home store.player starts empty, so set the shirt only when the store holds a pick: a nil there is no reason to take it off. It changes only how the player looks, so it works in a Just you mod, on a visit and in The City.
| Parameter | Type | Description |
|---|---|---|
picture | string|nil | The path of a 32 x 32 PNG in this mod, like "shirts/stripes.png", or nil to take the shirt off. |
Returns boolean|nil: true when it is set. When it is refused: nil, then a message and a code.
cmd.reactsince 1 #
cmd.react(name: string) -> boolean|nil Plays one of the reactions from the reactions bar on the player's own character: "hooray", "dance", "hug", "thanks", "ouch", "trip", "spin", "hide" or "pose". It is the same reaction the bar plays, so friends on a visit and players in The City see it as they see one from the bar. It is set at the call, with no queue, no daily cap and no command_finished: the game is asked there and then, and the reaction plays from the next frame for at most 1.5 seconds; any move from 0.3 seconds on walks out of it. Only from standing, walking or running, and only while the player is free: refused (command-busy) during a menu, a talk, a message, a door, a cutscene or a scene change, and refused (command-cannot-react) when the game says no, such as while sitting, swinging a tool or holding furniture. One reaction at a time: once one is taken the player is in it, so another call in the same moment, or in a player_acted handler (the player is in the action that just started), is refused with command-busy. React from a later moment, such as a timer. At most commands.reacts_per_minute a minute for each mod (command-minute-cap). It moves only the player's own body and changes nothing saved, so it works in a Just you mod, on a visit and in The City. When it plays, player_acted fires with reacted.
| Parameter | Type | Description |
|---|---|---|
name | string | Which reaction: hooray, dance, hug, thanks, ouch, trip, spin, hide or pose. |
Returns boolean|nil: true when the game took it. When it is refused: nil, then a message and a code.
The timer table #
timer: Timers. A script asks to be called later, and the game sends it a timer_fired event. Time is counted on the game's clock in whole game minutes, so a timer keeps counting while the game is closed and fires at the first chance after it is due. A timer never fires at the title screen or at player select. It fires once play has started (the rule own_game_only). Timers belong to the town. They are kept in this mod's town store, count toward its store.bytes and survive a save and a reload. On a visit, in The City and for a lent mod they live in memory only and are gone when the town changes. Scene changes do not touch them. A mod may have timers.per_mod waiting in one town.
timer.aftersince 1 #
timer.after(minutes: integer, name: string, data: string|number|boolean|nil) -> integer|nil Sets a timer that fires once, at least that many game minutes from now. It returns the timer's id. When it is refused it returns nil, a message and a code (timer_codes). Setting a timer rewrites the town store, so it is charged to the event's budget like store.set.
| Parameter | Type | Description |
|---|---|---|
minutes | integer | Game minutes to wait, 1 to 10080 (7 days). |
name | string | What the timer is for, 1 to 40 bytes of letters, digits, _, - and . only. It comes back in timer_fired. |
data | string|number|boolean|nil | A value handed back in timer_fired. Text is up to 240 bytes with no zero byte. Empty text comes back as nil, like every empty event text. |
Returns integer|nil: The timer's id, a whole number that is never reused in this town. When it is refused: nil, then a message and a code.
timer.cancelsince 1 #
timer.cancel(id: integer) -> boolean Cancels a waiting timer of this mod in this town. It answers true when there was one. A timer that already fired, or an id from another town, answers false.
| Parameter | Type | Description |
|---|---|---|
id | integer | The id timer.after returned. |
Returns boolean
The sandbox #
Each mod runs in its own Lua 5.4 state with only these standard libraries. It takes source text only, no bytecode. The libraries are read-only. Assigning to string.<name>, rawset on a library or setmetatable on a library raises "string.<name> is read-only". getmetatable of a library or a string is the string "locked". The built-in globals (the base functions, the libraries, on, log, message, game, store) cannot be assigned, set to nil or rawset. A local may shadow one, and a mod's own globals are writable as usual. pairs(_G) lists only the mod's own globals. Work the instruction count cannot see is charged to the budget. That covers patterns (per step, and per 16 bytes of a [...] set or of a gsub replacement), comparing long strings and allocations. The run's clock is read every 100 instructions, and a run past its deadline stops. A weak-keyed table (__mode "k" without "v") keeps its keys, because weak-keyed tables made one garbage collection cost the square of their size. Weak values work as usual.
base: Lua's base functions, without the removed ones below. pcall, xpcall and setmetatable are wrapped.string: Without string.dump. Pattern matching is charged against the budget, and string.pack, packsize and unpack per 4 bytes of their format. Read-only.table: insert, remove, sort, concat, move and unpack are charged per element. insert, remove, sort and concat refuse a table with a __len metamethod. Read-only.math: The math library without random and randomseed (see removed). Use rng(). Read-only.utf8: codepoint, len and offset are charged per byte. Read-only.coroutine: resume and close are wrapped like pcall. Read-only.
Libraries that do not exist: io, os, package, debug. Removed functions: dofile (reaches files), loadfile (reaches files), load (loads code past the text-only loader), collectgarbage (controls memory the sandbox manages), print (use log()), string.dump (hands out bytecode), math.random (still a function, but calling it raises an error: use rng(), which is seeded so a reload cannot reroll), math.randomseed (still a function, but calling it raises an error: use rng(), which is seeded so a reload cannot reroll).
Wrapped functions #
pcall: Cannot catch a budget stop. Once the budget is gone it raises again.xpcall: As pcall. The message handler is skipped after a budget stop.coroutine.resume: As pcall.coroutine.close: As pcall.setmetatable: Refuses a __gc field, because finalizers could never be stopped.table.insert: Charged per element.table.remove: Charged per element.table.sort: Charged n log n.table.concat: Charged per element.table.move: Charged per element.table.unpack: Charged per result.string.byte: Charged per result.utf8.codepoint: Charged per result.utf8.len: Charged per 64 bytes.utf8.offset: Charged per 64 bytes.string.pack: Charged per 4 bytes of the format.string.packsize: Charged per 4 bytes of the format.string.unpack: Charged per 4 bytes of the format.string.format: Charged per 64 bytes of its string arguments.tonumber: Charged per 64 bytes of a string argument. A string over 200 bytes is never a number, here or in arithmetic.