Your first Lua script mod

Mod scripts are Lua 5.4. A script listens to events, such as a villager talk or a new day, and answers with data. For a talk, the answer is a line for the villager to say. Here you get a script running and read what it does.

What you need #

  • The game in a desktop browser (Chrome, Edge, Firefox or Safari), so you can open the developer console. The game runs from your own disc file. See Your disc.
  • A code editor. VS Code with the free Lua extension (by sumneko) is a good choice.
  • Some Lua. If you know any scripting language, the examples are enough to learn from.

1. Start from the script kit #

Download the Script kit from GitHub and unzip it. Its mod.json:

mod.json
{
  "schema": 1,
  "api": 1,
  "id": "yourname.my-script",
  "name": "My Script",
  "version": "1.0.0",
  "authors": ["yourname"],
  "description": "A small script that counts days, picked up items and fish, gives each resident one apple, and has villagers notice it.",
  "scope": "client",
  "content": {
    "messages": "messages.json"
  },
  "scripts": {
    "main": "main.lua",
    "events": ["villager_talked", "day_started", "item_picked_up", "fish_caught", "scene_entered", "command_finished"]
  },
  "permissions": ["give_item", "show_line"],
  "settings": [
    {
      "name": "notice",
      "type": "bool",
      "label": "Villagers notice an apple",
      "description": "When this is on, a villager says something about an apple in your pockets, once a day each.",
      "default": true
    }
  ],
  "optional_depends": {
    "example.harvest-post": ">=1.0.0"
  },
  "assets_original": true
}

Change yourname to your own name in id and authors. The parts that matter for scripts:

  • scripts.main: the Lua file the game runs.
  • scripts.events: every event your script listens to. Calling on() for an event not in this list is an error.
  • content.messages: optional. A file of lines your script can read with message().

2. Read the script #

main.lua
-- My Script: the everyday events in one file, a little saved data, two
-- commands, a setting and an optional dependency. Every event used here is
-- listed in mod.json under "scripts" > "events". log() lines show in the Mod
-- log, at the bottom of Settings, Mods, under For mod authors. They are also
-- in the browser console, as "[Mods] yourname.my-script: ...". There are more
-- events (item_bought, letter_read, hour_changed and others): see the scripts
-- guide. Every handler gets one table, e, with the event's fields.

-- The main chunk runs once, when the game starts, BEFORE any town is loaded.
-- Register handlers here. Do not read the store here: store is kept per town,
-- so store.get answers nil until a town is loaded. Read it inside events.
log("loaded")

local APPLE = 0x2800 -- the apple's item number, from the item list
local HARVEST = "example.harvest-post" -- named in mod.json "optional_depends"

-- Another mod this one can use but does not need. mod.json names it in
-- "optional_depends" with a version range, so the game loads it first when it
-- is there and loads this mod either way. remote.version answers its version,
-- or nil, a message and a code when it is absent. remote.has says whether it
-- offers a function, and remote.call calls it. Ask inside an event.
local function fair_today()
  local version, why, code = remote.version(HARVEST)
  if not version then
    log("Harvest Post is not here:", code) -- dependency-absent or dependency-version
    return false
  end
  if not remote.has(HARVEST, "fair_today") then return false end
  local fair = remote.call(HARVEST, "fair_today") == true
  log("Harvest Post", version, "is here, fair today:", fair)
  return fair
end

-- A new day in the game (also when the game starts on a new day). A restart
-- later on a day the town already saw does not fire it again, so the day is
-- counted by date, not by call. The whole day is known here: the town, its
-- calendar and the player. So this is where to ask about today.
on("day_started", function(e)
  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)
    store.set("days", (store.get("days") or 0) + 1)
  end
  log("day", today, "days seen in this town:", store.get("days"))
  -- The fair line. show_line puts it in your thought window once you are
  -- free in town.
  if fair_today() then
    local id, why, code = cmd.show_line(message("fair"))
    if not id then log("no fair line:", why, code) end
  end
end)

-- You got an item: picked up, caught with the net or the rod, or dug up. It
-- is already in your pockets, or in the wallet for Bells. A fish or a bug
-- also fires fish_caught or bug_caught, so count a catch in one place only.
on("item_picked_up", function(e)
  local n = (store.get("picked") or 0) + 1
  store.set("picked", n)
  log(string.format("picked item 0x%04X (%d so far)", e.item, n))
end)

-- You landed a fish. e.item is the fish, and e.pocketed says whether it went
-- into your pockets (item_picked_up fired for it just before). Fish are
-- counted here, per player, and the first fish of each day gets a line.
on("fish_caught", function(e)
  local n = (store.player.get("fish") or 0) + 1
  store.player.set("fish", n)
  log(string.format("caught fish 0x%04X (%d so far, pocketed: %s)", e.item, n, tostring(e.pocketed)))
  if store.player.get("fish_day") ~= game.day_number() then
    store.player.set("fish_day", game.day_number())
    local id, why, code = cmd.show_line(message("first_fish"))
    if not id then log("no fish line:", why, code) end
  end
end)

-- You started talking to a villager. Return text to have them say it first,
-- or nil to leave the talk alone. A line that is the same whatever happens
-- needs no script: put it in a talk.json pool, as the lines kit does. A script
-- is for a line that depends on something. Here a villager notices an apple
-- in your pockets, once a day each. game.pockets() lists what you hold. The
-- list of who noticed is kept in memory, so a restart empties it.
local noticed = {}      -- villagers who noticed an apple, by id
local noticed_on = nil  -- the day that list is for

local function holding(item)
  for _, p in ipairs(game.pockets()) do
    if p.item == item then return true end
  end
  return false
end

-- "notice" is a setting in mod.json, which the player turns on or off on
-- the Mods tab. settings.get reads it. This is a Just you mod, so a setting's
-- "scope" changes nothing a visit sees, and it is left out.
-- The handler returns a talk table, and every line names its place: here
-- "before", said before the villager's hello. The others are "hello" (instead
-- of the hello), "topic" and "after" (when you chat), and "conversation" for
-- the whole talk (with an ask, the player's pick comes to villager_answered).
-- A plain string with no place is refused. A line can be a list of pages:
-- line = { "Page one.", "Page two." }.
on("villager_talked", function(e)
  if not settings.get("notice") then return nil end
  if noticed_on ~= game.day_number() then
    noticed, noticed_on = {}, game.day_number()
  end
  if noticed[e.villager.id] or not holding(APPLE) then return nil end
  noticed[e.villager.id] = true
  return { place = "before", line = message("apple") }
end)

-- A command: every resident of the town gets one apple, once. A handler
-- cannot change the game itself. It queues a command (mod.json names it in
-- "permissions"), the game applies it a moment later, and command_finished
-- says how it went. store.player is kept per player, so "gift" is each
-- resident's own. command_finished comes for show_line too, so it checks
-- e.command first.
local waiting = false -- an apple is queued and has not finished
local resting = nil   -- the day today's cap ran out, or true to stop asking

local function give_apple()
  if waiting or resting == true or resting == game.day_number() then return end
  if store.player.get("gift") then return end
  local id, why, code = cmd.give_item(APPLE)
  if id then
    waiting = true
  else
    log("apple not queued:", why, code)
    if code == "command-day-cap" then resting = game.day_number() end
  end
end

-- The game entered a scene (outdoors, a house, a shop and so on). e.name is
-- nil for a scene with no published name.
on("scene_entered", function(e)
  log("scene", e.scene, e.name or "(no name)")
  give_apple()
end)

-- Mark the apple as given only here, when the game says it was. A refusal has
-- a code that says whether to try again: the full list is in the commands
-- guide, https://playaconline.com/modding/guides/commands/#codes
on("command_finished", function(e)
  if e.command ~= "give_item" then return end -- a line reports via "shown"
  waiting = false
  if e.ok then
    store.player.set("gift", true)
    log("apple given, via", e.via)
  elseif e.code == "command-day-cap" then
    resting = game.day_number() -- today's gifts are used up: tomorrow
  elseif e.code == "command-no-room" then
    log("no room for the apple now: trying again in the next scene")
  elseif e.code == "command-city" or e.code == "command-visiting" then
    log("the apple waits until you are home")
  else
    resting = true -- a refusal that will not change: stop asking
    log("apple refused:", e.code, e.message)
  end
end)
  • The file's top level runs once, when the game starts. Call on() there. Calling it later, inside a handler, is an error.
  • on(event, fn) runs fn each time the event happens.
  • A villager_talked handler returns a talk table that says where the line goes: { place = "before", line = "Hi!" } is said before the villager's usual hello. The other places are hello, topic, after and conversation. Return nil to leave the talk alone.
  • The kit's talk handler says its line only when you carry an apple. game.pockets() lists what you hold. That is what a script is for: a line that depends on something in the game, or a conversation that asks the player something (ask). Plain lines need no script. Put it in a talk.json pool, as the writer path does, and the villager can say it when you choose to chat, by villager or personality, season, time of day and weather.
  • store keeps small values between sessions, per town. The top level runs before any town is loaded, so store.get answers nil there. Read and save it inside events, as the kit does.
  • log() writes a line you can read in the Mods tab's Mod log and in the browser console.

3. Install it and read the log #

  1. ZIP the folder. In the game, choose Settings, Mods, Install from a file, Choose a file. Then choose Restart now.
  2. Open the browser's developer console:
    • Chrome and Edge: F12 or Ctrl+Shift+J.
    • Chrome on a Mac: Cmd+Option+J.
    • Safari: Cmd+Option+C. Switch on Show features for web developers in Safari's settings first.
  3. Type [Mods] in the console's filter box. Your lines look like [Mods] yourname.my-script: loaded.
  4. Pick something up, go indoors and talk to a villager. Watch the lines appear. The kit's apple is in your pockets by then, so the villager notices it.

4. More events and commands #

The kit uses five events. There are 20 in all: villager_talked, day_started, item_picked_up, scene_entered, data_migrated, hour_changed, item_bought, item_sold, fish_caught, bug_caught, letter_read, villager_moved_in, visitor_arrived, command_finished, question_answered, timer_fired, villager_answered, button_pressed, button_released and player_acted.

A handler can also queue a command (cmd.send_letter, cmd.give_item, cmd.show_line, cmd.ask, cmd.take_item, cmd.take_bells, cmd.save, cmd.set_weather, cmd.play_sound, cmd.set_shop_goods, cmd.set_shop_open, cmd.set_hat, cmd.set_shirt or cmd.react). A command changes the game. You declare it in mod.json under "permissions", and the game applies it safely at the end of a frame's update. The scripts guide and the events reference list every call, event and limit.

Get the playac-mod tool (optional) #

playac-mod is a command line tool that checks a mod folder with the game's own rules, reloads it in the game as you save, and packs the ZIP. It needs Node.js 18.17 or newer (free, from nodejs.org; node --version says which you have). Nothing on this path needs it: installing a ZIP in the Mods tab runs the same checks.

terminal
npm install -g https://github.com/Play-AC-Online/playac-mods/releases/download/cli-v1.1.0/playac-mod-1.1.0.tgz
cd my-mod            # your mod's folder
playac-mod check     # every rule the game checks, each problem with its file
playac-mod dev       # serve the folder to the game's developer mode (see Testing)
playac-mod pack      # the ZIP to install or share, the same bytes every time

Every command and option is on The playac-mod tool.

Next #

  • The Scripts guide covers every call, every event and its fields, the limits and what a script cannot do.
  • The Cookbook has complete script mods to read and copy.
  • Limits has the memory, time and log limits in one table.