Commands for letters, items, lines, questions and takes

A script never changes the game while it runs. It queues a command. The game carries the command out at one safe point in each frame, after the game's update and before the frame is drawn. It waits until the player is free, which means not in a talk, a menu, a cutscene, a door or a scene change. The result comes back to your script as a command_finished event.

Permissions #

List every command your script uses in mod.json "permissions". The game refuses any command the list leaves out. Players see the list on the Mods tab, for example "Can send you letters". A mod without the key can change nothing.

mod.json
{
  "schema": 1,
  "api": 1,
  "id": "yourname.daily-letter",
  "name": "Daily Letter",
  "version": "1.0.0",
  "authors": ["yourname"],
  "scope": "client",
  "scripts": { "main": "main.lua", "events": ["day_started", "command_finished"] },
  "permissions": ["send_letter"],
  "assets_original": true
}

The commands #

CommandDoes
cmd.send_letter{ text, item?, from?, paper? }Puts a letter in the player's mailbox, with a present if you give one. When the mailbox is full, the post office keeps the letter, as it does for the game's own letters. The save holds an ordinary letter, so it keeps working without your mod. cmd.send_letter{ template = "welcome" } sends one of your own letters.json letters instead, with its greeting, sign-off, present and paper. Name the letter by its id or by its full name, "yourname.my-mod:welcome".
cmd.give_item(item)Puts an item into the first empty pocket. When the pockets are full, the item comes by letter instead. It also takes your mod's own new item by its full id, "yourname.my-mod:thing". A letter cannot carry a mod's own item, so full pockets refuse that one.
cmd.show_line(text)Shows a line in a message box with no one speaking. See where a line shows.
cmd.ask{ text, choices }Asks a question with 2 to 4 answers in the same window. The pick comes back as a question_answered event.
cmd.take_item{ item, count? }Asks the player to hand over items from their pockets. The game writes the question. See taking items or Bells.
cmd.take_bells{ amount }Asks the player to pay Bells from their wallet. The game writes the question.
cmd.save()Saves the game, where the player could save at Gyroid. See saving the game.
cmd.set_weather(kind, intensity?)Sets the town's weather for the rest of the game day: clear, rain or snow, light or heavy. See the weather.
cmd.play_sound(name)Plays one of the game's own sound effects for this player only, by its name in sounds.json. See sounds.
cmd.set_shop_goods(shop, goods)Sets a shop's goods for the rest of the game day, as one more block after your shops.json blocks for that shop. nil clears it. See the Shops guide.
cmd.react(name)Plays a reaction from the reactions bar on the player's own character: hooray, dance, hug, thanks, ouch, trip, spin, hide or pose. See reactions.
cmd.set_shop_open(shop, open)Opens (true) or shuts (false) a shop for the rest of the game day. nil clears it.
cmd.set_hat(picture)Puts a picture from your mod on the player's hat. nil takes it off. See the hat.
cmd.set_shirt(picture)Puts a picture from your mod on the player's shirt. nil takes it off. See the shirt.

Each call returns the command's number, except the two shop commands, cmd.set_hat, cmd.set_shirt and cmd.react: they are set at once and return true. A call that is refused right away returns nil, a message and a code. The shop commands need "scope": "world", since a visitor sees the shop, and so does set_weather. To keep data of your own, use store. To save the player's game, see saving the game.

main.lua
on("day_started", function(e)
  local id, why, code = cmd.send_letter{
    text = "Good morning! Here is a little something for your day.",
    item = 0x2800,          -- or "0x2800"
    from = "A friend",      -- up to 8 characters
  }
  if not id then log("no letter today:", why, code) end
end)

on("command_finished", function(e)
  -- worked:  e = { id, command, ok = true, via }; via is "mailbox" or "post_office"
  -- refused: e = { id, command, ok = false, code, message }
  if not e.ok then log(e.command, "#" .. e.id, "failed:", e.code, e.message) end
end)
main.lua
on("villager_talked", function(e)
  if store.player.get("asked") then return nil end
  store.player.set("asked", true)
  cmd.ask{ text = "Tea or cake?", choices = { "Tea", "Cake" } }
end)

on("question_answered", function(e)
  -- e = { id, choice, text }: choice counts from 1; { id } alone when closed without a pick
  if e.choice == 2 then cmd.show_line("Cake it is!") end
end)

Where a line or question shows #

cmd.show_line, cmd.ask, cmd.take_item and cmd.take_bells all use the message box the game shows when the player tries to pick something up with full pockets and reads "My pockets are full!". It is not a conversation with a villager:

  • No one is speaking. There is no name tag, and the box is tinted light blue.
  • It waits until the player is free: walking around, not talking to anyone, not in a menu. It never cuts into a conversation.
  • The player stops while the box is up and walks on once it closes.
  • One line or question shows at a time. The next one waits for the box to close.

What comes back #

  • command_finished when it worked is { id, command, ok = true, via }. via is "mailbox", "post_office", "pockets", "mail", "shown", "taken", "saved", "set", "unchanged" or "played".
  • command_finished when it was refused is { id, command, ok = false, code, message }.
  • question_answered is { id, choice, text }. choice counts from 1. When the window closed with no pick, the event is { id } alone.

Taking items or Bells #

cmd.take_item and cmd.take_bells let a mod run a quest or a sale: "bring me three apples", "a peach costs 300 Bells". A mod never takes on its own word. The player agrees each time.

  1. Your script queues the take. You give the item and the count, or the amount. You give no text.
  2. When the player is free, the game asks its own question in that message box. It reads Give 3 x apple to the mod "Your Mod"? or, for the fruit contest of the Harvest Post example, Pay 100 Bells to the mod "Harvest Post"?. The item's name, the count and the amount come from the game. The mod's name is the name in its mod.json, shown between quotes. A script cannot change any of it.
  3. The answers are No, keep it and Yes, give or Yes, pay. The cursor starts on No.
  4. On yes, the items leave the pockets or the Bells leave the wallet. command_finished has ok = true and via = "taken".
  5. On no, or when the window closes with no pick, nothing is taken. command_finished has code = "command-declined".
main.lua
-- mod.json: "permissions": ["take_bells", "give_item", "show_line"]
local PRICE = 300

on("villager_talked", function(e)
  if store.player.get("peach") then return nil end
  if (game.bells() or 0) < PRICE then
    return { place = "before", line = "A peach from my garden is " .. PRICE .. " Bells. Come back when you have them!" }
  end
  local id, why, code = cmd.take_bells{ amount = PRICE }
  if not id then log("no sale:", why, code) end
  return { place = "before", line = "A peach from my garden? Let me wrap one up." }
end)

on("command_finished", function(e)
  if e.command ~= "take_bells" then return end
  if e.ok then
    -- e.via is "taken": the player said yes and the Bells left the wallet
    store.player.set("peach", true)
    cmd.give_item(0x2803)
  elseif e.code == "command-declined" then
    cmd.show_line("Maybe another day.")
  else
    log("take_bells failed:", e.code, e.message)
  end
end)

What can be taken:

  • List take_item or take_bells in "permissions". Players see "Can take items from your pockets, only when you say yes each time" before they install.
  • item is an item number, "0x2800" or your mod's own "author.mod:thing". count is 1 to 15 and is 1 when left out.
  • amount is a whole number of Bells, 1 to 99,999.
  • Only plain pocket slots count. A wrapped present and an item held for a villager's errand are never taken. An item number never matches a mod item that stands on that piece.
  • The goods of a villager's errand are refused by number too. So is anything give_item refuses: money bags, turnips, tickets, grab bags and fossils.
  • Bells come from the wallet only. The bank account and money bags are never touched. The wallet never goes below zero.
  • When the player does not hold enough, the take is refused with command-not-held and no question is shown. Check game.pockets() or game.bells() first.

How often, and where:

  • Each game day, per mod and per town, the game shows 6 take_item questions and 6 take_bells questions. A take counts when its question is shown, whatever the answer. A mod cannot ask again and again.
  • On a visit to a friend and in The City, both are refused. The pockets there belong to a save the host's mods were never installed on.
  • A mod lent for a visit acts only on that visit, so a lent mod can never take anything.
  • Every take that happened is written to the Mod log, with the question the player agreed to.
  • A take sends no question_answered. Its one result is command_finished.

Saving the game #

cmd.save() saves the player's town, as the Autosave setting does, from any event: a save each time the player steps outside, after a catch, or on a button of the mod's own. It takes no arguments. The save waits until the player is free, like every command, and the game then decides whether it may write.

  • The game saves only where the player could save at Gyroid: outdoors in their own town, walking or standing, with no menu, talk, cutscene or door. Anywhere else the save is refused with a code, and nothing is written.
  • It never changes the town being played. The game writes a settled copy of the town, so a save cannot lose or double an item, however often it runs.
  • command_finished has via = "saved" when the game wrote the town, and via = "unchanged" when nothing changed since the last save, so nothing was written.
  • Indoors it is refused with command-indoors. Inside a house the furniture still holds what is kept in its drawers, so the save would have that house with its storage empty. Save once the player is outside.
  • command-busy: the player is not in free control. command-dream: a dream town is never saved. command-not-ready: no save is loaded yet. command-save-failed: the write itself failed, for example when the browser's storage is full.
  • Two saves by mods, of all mods together, are at least 10 seconds apart, and one of your saves waits at a time. A save asked for sooner is refused at the call with command-too-soon.
  • Each game day, per mod and per town, the game writes 32 saves. Only a save that wrote the town counts. Every save also uploads the town to the cloud, so do not save on a timer of your own: the Autosave setting is there for that.
  • On a visit to a friend and in The City it is refused. A visitor's game does not hold the town it stands in.
  • It saves what Gyroid's save saves, except a villager's mood and clothes, which reach the save at the next change of scene. A save in a script never replaces Gyroid: the player still saves and quits there.
  • List save in "permissions". It arrived in API 1.1, so a playac-mod older than 1.1.0 does not know it. Players see "Can save your game" before they install.
main.lua
-- mod.json: "permissions": ["save", "show_line"],
--           "scripts": { "main": "main.lua", "events": ["scene_entered", "command_finished"] }
on("scene_entered", function(e)
  if e.name ~= "town" then return end
  -- the save waits until the player is free outdoors, then the game writes it
  local id, why, code = cmd.save()
  if not id then log("no save:", code) end
end)

on("command_finished", function(e)
  if e.command ~= "save" then return end
  if e.ok and e.via == "saved" then
    cmd.show_line("Saved!")
  elseif not e.ok then
    log("save refused:", e.code, e.message)
  end
end)

Sounds #

cmd.play_sound(name) plays one of the game's own sound effects: the jingle of getting an item, the menu cursor's tick, a page turning in the message window. A mod names a sound from the sounds list, never a number.

main.lua
-- mod.json: "permissions": ["play_sound"]
on("bug_caught", function(e)
  local id, why, code = cmd.play_sound("item_get") -- a name from sounds.json
  if not id then log("no sound:", why, code) end
end)
  • List play_sound in "permissions" (new in API 1.1). Players see "Can play the game's sound effects, heard only by you" before they install.
  • Every sound in the list is one the game plays once and stops on its own. None loops, and none is one a scene starts and stops itself.
  • Only this player hears it. On a visit the friend does not hear it, and nothing reaches the town or the save. In The City it is refused.
  • 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, and never waits behind a line.
  • At most 2 sounds in one event, inside the 4 commands. At most 10 a minute for each mod, counting the ones still waiting: more is refused with command-sound-cap. There is no daily limit.
  • While the player plays an NES game the sound belongs to it, and a sound is refused with command-no-sound.
  • command_finished says via = "played".

The weather #

cmd.set_weather(kind, intensity) sets the town's weather for the rest of the game day, for a rainy festival, a snow day or a sunny contest. kind is "clear", "rain" or "snow", and for rain or snow intensity is "light" (when left out) or "heavy". Heavy rain is a storm, heavy snow a blizzard. The Button Tricks example steps through them on the D-pad.

  • The game changes the weather the way it changes it on its own: the last drops or flakes fall, then the new weather fades in, with its light and its sound. Villagers open umbrellas, and fish and bugs follow the weather, as on any rainy or snowy day. Heavy rain brings lightning in June, July and August, as the game's own storms do.
  • It lasts until the day changes. Then the game rolls its own weather again. A day after rain or snow can bring a rainbow and a gyroid in the ground, as after a real rainy day. A reload the same day keeps your weather.
  • Only weather the game itself could roll on that date is allowed. Clear is always allowed. Rain falls from February 25 to November 15 and snow from November 1 to February 24, light or heavy, and neither on the days the game keeps clear: January 1 to 7, April 1 to 8, 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 never rains. A weather the game would not have today is refused with command-weather, at the call when it can tell then.
  • command_finished has via = "set" when the weather changed, and via = "unchanged" when the town already had it, 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: putting back the weather you read changes nothing. While a change of weather is still under way, the command waits for it to end.
  • Each game day, per mod and per town, the weather changes 4 times. Only a change counts.
  • It is refused on a visit and in The City. The host's game decides the weather, and a visitor sees the host's weather through the game itself, with or without your mod. A host's change reaches a visitor already in the town.
  • List set_weather in "permissions", with "scope": "world" (new in API 1.1). Players see "Can change your town's weather for the day" before they install. A mod that only sets the weather never has to match on a visit.
main.lua
-- mod.json: "scope": "world", "permissions": ["set_weather"],
--           "scripts": { "main": "main.lua", "events": ["hour_changed", "command_finished"] }
on("hour_changed", function(e)
  if e.hour == 8 then
    local id, why, code = cmd.set_weather("rain")
    if not id then log("no rain today:", code) end -- command-weather in the snowy months
  elseif e.hour == 12 then
    cmd.set_weather("clear")
  end
end)

on("command_finished", function(e)
  if e.command == "set_weather" and not e.ok then
    log("weather refused:", e.code, e.message)
  end
end)

The hat #

cmd.set_hat("hats/stripes.png") puts a picture from your mod on the pointy hat of the player playing now, in place of the design or the shirt the hat wears. cmd.set_hat(nil) takes it off. It is set at once and shows from the next frame. New hat shapes and hair need 3D models, which mods cannot add yet. The Button Tricks example puts on a hat and a shirt of its own.

  • List set_hat in "permissions". It arrived in API 1.1. It works in a Just you mod.
  • The picture is a PNG inside your mod, by its path from the top of the mod, outside your textures folder. It is exactly 32 x 32 pixels with at most 16 colours and no see-through pixels, like an Able Sisters design. Anything else is refused with command-picture.
  • A hat from a mod beats a design on the hat. When two mods set one, the mod lower in the Mods tab list wins.
  • The hat belongs to the player who wears it, wherever they go: a hat set at home stays on when they visit a friend or go to The City. Only that player's own screen draws it: friends on a visit and players in The City see the hat the game would draw.
  • The game saves nothing about it. The hat is gone when the game restarts, and stays on when you reload the script in developer mode. Keep the pick in store.player and set it again from scene_entered, which also runs for the first scene of a session. Away from home store.player starts empty, so set the hat only when it holds a pick, as the sample does.
main.lua
-- mod.json: "permissions": ["set_hat"], "scripts": { "events": ["scene_entered"] }
-- hats/stripes.png: 32 x 32, at most 16 colours, no see-through pixels
on("scene_entered", function(e)
  local picture = store.player.get("hat")   -- nil: the game's own hat
  if picture then
    local ok, why, code = cmd.set_hat(picture)
    if not ok then log("no hat:", why, code) end
  end
end)

-- wherever the player picks one (a question, a villager, a day):
store.player.set("hat", "hats/stripes.png")
cmd.set_hat("hats/stripes.png")

The shirt #

cmd.set_shirt("shirts/stripes.png") puts a picture from your mod on the shirt of the player playing now, in place of the clothes they wear. cmd.set_shirt(nil) takes it off. It follows every rule of the hat: the same permission rules (list set_shirt), the same picture, the same command-picture refusals, the same load order, it follows the player onto a visit and into The City, and nothing is saved.

  • The picture wraps the body and the sleeves the way an Able Sisters design does, so a design drawn for a shirt looks the same here.
  • It beats the clothes the player wears. Putting on other clothes changes what is underneath, and your picture stays on top until your mod takes it off. The save keeps the real clothes.
  • The hat matches the shirt, as in the game: with no design and no mod hat on it, the hat wears your shirt picture too. Set a hat as well to give it a picture of its own.
  • Only the player's own screen draws it: friends on a visit and players in The City see the clothes the player wears.
main.lua
-- mod.json: "permissions": ["set_shirt"], "scripts": { "events": ["scene_entered"] }
-- shirts/stripes.png: 32 x 32, at most 16 colours, no see-through pixels
on("scene_entered", function(e)
  local picture = store.player.get("shirt")   -- nil: the clothes the player wears
  if picture then
    local ok, why, code = cmd.set_shirt(picture)
    if not ok then log("no shirt:", why, code) end
  end
end)

Reactions #

cmd.react(name) plays one of the reactions bar's nine on the player's own character, the same way the bar does. Friends on a visit and players in The City see it as they see one from the bar. The game is asked at the call, and the reaction plays from the next frame for at most a second and a half. Any move after the first moment walks out of it.

  • It needs "react" in "permissions" (new in API 1.1), and works in a Just you mod.
  • Only while the player is free. During a menu, a talk, a message, a door, a cutscene or a scene change it returns nil with command-busy. So does an action the player just started, so a player_acted handler gets command-busy: react later, from a timer.
  • One reaction at a time. Once one is taken the player is in it, and a second call in the same moment is command-busy.
  • Only from standing, walking or running. Sitting, lying in bed, a tool swing or holding furniture: command-cannot-react. Read game.player().state first.
  • At most 10 a minute for each mod (command-minute-cap). A refused reaction does not count.
  • It works on a visit and in The City: it moves only your own character and changes nothing saved.
  • When it plays, player_acted fires with action = "reacted" and its reaction.

Refusal codes #

A refusal carries a code that never changes. It is the third return value of cmd.* and the code field of command_finished. Test the code, not the message. The table says whether trying again can work.

CodeTry againWhen
command-shopneverA shop command the shop cannot take: the shop is not in the shop table, it has no goods or no open, or a mod later in the load order changes that shop.
command-permissionneverThe command is not in mod.json permissions.
command-argsneverWrong arguments, such as their shape, a length, the paper or the choices.
command-itemneverAn item the command cannot hand out, or an unknown item or letter id of the mod's own. Also sent in command_finished when the mod's item or letter was gone by the time the game applied the command.
command-event-capin the next eventMore than commands.per_event commands in one event.
command-queue-fulllaterThe mod's queue, or the game's, is full.
command-day-captomorrowThe per-day limit for this command in this town is used up.
command-citylaterCommands are off in The City.
command-visitinglaterOn a visit, only the town owner's game changes the town.
command-lent-homeneverA lent mod acting outside the host's town. It arrives in command_finished, never as the return of a cmd call.
command-no-roomlaterNo room now. The mailbox and the post office are full, or the pockets are full for a mod item. The command still counts against the day's cap, so do not try again in a loop.
command-not-shownneverThe line or question cannot be drawn in the window, because the text has too many pages. The command still counts against the day's cap.
command-not-heldlaterA take the player cannot meet. The pockets do not hold that many of the item in plain slots, or the wallet holds fewer Bells.
command-declinedlaterThe player said no to a take's question, or closed it with no pick. Nothing was taken.
command-client-settingneverThe event read a client setting, and a client setting cannot decide what the town gets or gives up. Make the setting world, or do not read it in an event that sends a letter, gives an item, or takes an item or Bells.
command-not-readylaterA save before the player's save has loaded, or while the page is replacing or restoring the save.
command-indoorslaterA save anywhere but the town outdoors: a house, a shop, a museum, the train. Inside a house the furniture still holds the items kept in it, so the game would save the house with its storage empty.
command-picturenevercmd.set_hat's or cmd.set_shirt's picture is not in the mod, or is not a 32 x 32 PNG with opaque pixels and at most 16 colours.
command-busylaterThe player is not free now: a menu, a talk, a message window, a door, a cutscene, a tool swing or a scene change has them. For cmd.save, also right after the game itself settled this scene for a save that did not finish, until the next change of scene. For cmd.react, also an action they just started (as in a player_acted handler) or a reaction already playing. Try again from a later moment.
command-dreamlaterA save in a dream town. A dream is never written to the player's save.
command-too-soonlaterA save within commands.save_spacing_seconds of the last save by any mod, or a second save while this mod's last one is still waiting.
command-save-failedlaterThe game could not write the save file. The town in play is unchanged.
command-weatherlaterA weather the game would not have today: snow outside the months it snows, rain outside the months it rains, any weather but the event's on an event day, or rain on the player's first day of work for Nook. The date decides, so the next day may allow it.
command-sound-caplaterA sound when this mod already played commands.sounds_per_minute sounds in the last minute, counting the ones still waiting. Wait and play it later.
command-no-soundlaterA sound while an NES game is being played: the game's sound belongs to the NES game until it ends.
command-cannot-reactlatercmd.react: the game did not take the reaction. It plays only while the player stands, walks or runs, so not while sitting, lying, swinging a tool or holding furniture, and something more important (a talk, a door, a bee) outranks it.
command-minute-caplatercmd.react: the mod already played commands.reacts_per_minute reactions this minute of the game clock. Try again next minute.

Mark a reward as given only in command_finished, when e.ok is true. A command can be refused after the call returns. A closed tab loses a command that is still waiting.

main.lua
local waiting = false  -- a letter is queued and has not finished
local rest_day = nil   -- the day whose cap ran out

local function send()
  if waiting or rest_day == game.day_number() then return end
  if store.player.get("reward") then return end   -- given, or never to be
  local id, why, code = cmd.send_letter{ template = "reward" }
  if id then
    waiting = true
  elseif code == "command-day-cap" then
    rest_day = game.day_number()
  end
end
on("day_started", send)
on("scene_entered", send)

-- the codes whose "try again" is later or in the next event
local AGAIN = {
  ["command-no-room"] = true, ["command-queue-full"] = true,
  ["command-city"] = true, ["command-visiting"] = true,
  ["command-event-cap"] = true,
}

on("command_finished", function(e)
  if e.command ~= "send_letter" then return end
  waiting = false
  if e.ok then
    store.player.set("reward", "given")
  elseif e.code == "command-day-cap" then
    rest_day = game.day_number()                  -- tomorrow
  elseif AGAIN[e.code] then
    -- the next scene_entered sends again
  else
    store.player.set("reward", "refused")         -- never: do not ask again
    log("reward not sent:", e.code, e.message)
  end
end)

The rules #

What a command may carry:

  • Items follow the shop's rules. Money, turnips, tickets, grab bags, fossils and reserved numbers are refused.
  • A letter's text is 1 to 192 characters and must fit its 6 lines.
  • from is up to 8 characters on one line. paper is 0 to 63.
  • A line is up to 400 bytes.
  • A question is up to 120 characters. Each answer is up to 16 characters.

How many:

  • At most 4 commands in one event.
  • Each game day, per mod and per town: 3 letters, 5 items, 20 lines and 10 questions. The count is kept across a reload.
  • Also each game day: 6 questions for items and 6 for Bells. See taking items or Bells.
  • And 32 saves, at least 10 seconds apart. See saving the game.
  • And 4 changes of weather. See the weather.
  • The day's caps cover all of a town's residents together (up to 4). They are a safety limit, not a game rule.
  • A command counts when the game applies it. Commands still waiting count toward the cap. A command lost with a closed tab before the game applied it never counts.
  • A command the game applied counts even when it failed. A letter refused with command-no-room for a full mailbox uses one of the day's letters. So does a line refused with command-not-shown. Try again from a later event, never in a loop.
  • At most 8 of your commands wait at once.
  • The window shows one line or question at a time. The next waits until the window closes.

Where the player is:

  • In The City, every command but cmd.set_hat, cmd.set_shirt and cmd.react is refused.
  • On a visit to a friend, letters, items, takes, saves and the weather are refused, because only a town's owner changes a town or a save. Lines, questions, sounds and reactions still run.
  • A command refused for where the player is never counts against the day's caps.
  • Called there, the command returns nil, the message and the code at once.
  • Queued at home and applied after the player left, it comes back as a refused command_finished.

Where a refusal shows:

  • A command refused when you call it is written to the Mod log with your mod, the event and the command.
  • A command refused later, when the game applies it, arrives as command_finished with ok = false, the code and the message. The refusal is also written to the Mod log.
  • Handle command_finished to act on a refusal. The Mod log is for reading, and a script cannot read it.
  • Every message is on the errors page.