Skip to content

Story events and quests

Story events let your script run when something happens: the player buys an item, enters the map, wins a fight, presses a map button, or a session begins. This is also how you write quests: a quest is a handler for one event that decides in Lua whether it applies and then calls the actions you want, such as revealing a battle or starting a fight.

A few terms, each used throughout this guide:

  • A callback (or handler) is a Lua function you give the game so it can call you later.
  • A subscription is one registered listener. sf2.story.on returns a handle you can keep to stop it with sf2.story.off.
  • An event table is the small table of facts the game passes to your callback. It is a copy: changing it affects nobody.
  • A quest event is one of the game’s own quest triggers, such as session or fight_end. The game’s quests react to the same events.

Examples assume local sf2 = require("sf2").

Reveal a second battle once the player beats the first. Here training and advanced are battles your mod registered (see Battles and bosses). A fight’s ID is <mod id>:fights/<battle id>_<fight name>; unnamed fights are numbered 1, 2, and so on.

local sf2 = require("sf2")
-- When a session starts: show training, and advanced too if training was already won.
sf2.story.on("session", function()
sf2.map.reveal { battle = "training" }
local progress = sf2.profile.fight(sf2.mod.id .. ":fights/training_1")
if progress.wins > 0 then
sf2.map.reveal { battle = "advanced" }
end
end)
-- After a win in training: reveal advanced and move the map to it.
sf2.story.on("fight_end", function(event)
if event.fight == sf2.mod.id .. ":fights/training_1" and event.result == "win" then
sf2.map.reveal { battle = "advanced" }
sf2.map.focus("advanced")
end
end)

Each if replaces a condition a declarative quest would have had, and each call replaces an action. sf2.map.reveal leaves an entry that already exists as it is, so running the session handler every time is safe. A battle you register does not appear on an existing save until something reveals it, so most mods reveal their first battle from a session handler.

Every event table has kind, the event’s name. Other fields depend on the event; a field the event does not carry is nil.

Event When it runs Fields
purchase After the game processes a shop purchase. item
enchantment After the forge finishes an enchantment. item, recipe
level_up After experience is processed. One event even if several levels were gained. previous_level, level
scene_enter After the map, shop, profile, dojo or a fight finishes setting itself up. scene
map_button After a button added with sf2.map.show_button is pressed. button
dojo_button After a button from sf2.ui.dojo_button is pressed. button
item_acquired After the player’s count of an item goes up. item, previous_count, count
battle_result After a fight result is processed. fight, outcome, eclipse, equipment
session A game session starts, after the profile loads. None
fight_start, raid_fight_start The player starts an ordinary or raid fight. fight, battle, mode
fight_end, raid_fight_end An ordinary or raid fight ended. fight, battle, mode, result
raid_enter, raid_end A registered offline raid attempt starts, or its whole sequence completes. fight, battle, mode (and result for raid_end)
raid_map_enter, raid_floor_changed The Underworld map opens, or moves to another floor. None
mode_reset A mode or ascension battle resets after a loss. fight, battle, mode, result
timer_end One of the game’s timers runs out. timer
delivery A purchased or forged item is delivered. item
shop_enter The player opens the shop. None

What the fields hold:

Field Meaning
item, recipe, perk Qualified IDs (plain strings, not handles). item is nil for an item missing from the catalog.
fight, battle Qualified fight and battle IDs, such as core:fights/zone_1/boss_lynx/6.
mode Qualified ID of the mode, event, raid or encounter the fight belongs to, or nil.
result "win", "loss" or "surrender" for quest events; nil when none was recorded.
outcome For battle_result: "win", "loss", "surrender", "raid_timeout" or "raid_round_timeout".
scene "map", "shop", "profile", "dojo" or "fight".
button The button’s name. Map buttons are named <mod id>.<id>.
equipment For battle_result: the player’s equipped items at the start of result processing, each with item, type and subtype. nil if no player data was supplied.

Two rules for every event: a callback cannot cancel the action or change its result (what it returns is ignored), and events are only delivered while a profile is loaded. perk_activated and perk_deactivated exist but this version of the game never raises them.

The game raises quest events for its own quests, and Eclipse delivers them to you through a quest of its own. That gives your handlers the game’s timing: the details are captured when the event happens and your handler runs when its turn comes in the game’s quest queue, on the map, together with the game’s own quests for the same event. So fight_end handlers run after combat, alongside the game’s fight-end quests. If the game closes before a handler ran, the delivery resumes at the next load with fight, battle and result restored.

Inside a quest-event handler, sf2.map.reveal and sf2.story.start_fight also work when the map is not open. Outside one they need the map.

Each action runs the game’s own quest action, so it behaves as in the game’s quests. Most return true when they ran and false when the game cannot run them right now (for example, no profile is loaded). A mistake such as an unknown battle raises an error.

What to do Function
Show a battle on the map, locked or not sf2.map.reveal { battle, locked? }
Hide or show a revealed battle sf2.map.reveal { battle, visible }
Move the map to a battle sf2.map.focus(battle)
Lock or unlock a revealed battle sf2.map.lock { battle, locked }
Start a fight, or the event’s own fight sf2.story.start_fight(fight?)
Switch Eclipse mode, or update the Eclipse battle pairs sf2.game.set_eclipse_mode, sf2.game.refresh_eclipse_battles()
Give the player an item sf2.profile.give_item(item)
Set or read one of the game’s quest variables sf2.story.set_variable, sf2.story.variable
Add or remove a map button sf2.map.show_button, sf2.map.hide_button
Show dialogue and chapter screens sf2.story.play_sequence
Stop one of the game’s quests sf2.story.suppress_quest(id)

Use the game’s quest variables only to answer or change the game’s own quests. They are shared with every mod. For your own progress use mod state.

local sf2 = require("sf2")
sf2.story.on("session", function()
sf2.map.show_button { id = "arena",
image = sf2.assets.sprite("sprites/arena_button"), x = -400, y = -200 }
end)
sf2.story.on("map_button", function(event)
if event.button == sf2.mod.id .. ".arena" then
sf2.log.info("Arena button pressed") -- open a menu here
end
end)

The button only appears; your handler decides what a click does. Map buttons are saved in the profile, so they stay after a mod stops showing them. Call sf2.map.hide_button("arena") to retire one.

fight_start and raid_fight_start work like the game’s own fight-entry quests: while you listen, the fight waits until your handlers have run. If a handler called sf2.story.start_fight, that fight starts instead. Otherwise the original fight starts after the last handler, so a handler that only watches does not get in the way. The game’s own entry warnings, such as the difficulty notice, run first; if the player declines one, your handlers are not called.

-- The first time the player picks the boss, send them to a warm-up fight.
sf2.state.register { version = 1, fields = { warmed_up = false } }
sf2.story.on("fight_start", function(event)
if event.fight == sf2.mod.id .. ":fights/boss_1" and not sf2.state.get("warmed_up") then
sf2.state.set { warmed_up = true }
sf2.story.start_fight("warm_up_1")
end
end)

To run your own presentation before one of your fights, hold the entry with sf2.story.before_fight and show a dialogue with sf2.story.play_sequence. The handler decides the fight’s fate: return true to start immediately, false to cancel, or nil to hold the entry while you show something and resume it later.

local sf2 = require("sf2")
local name = sf2.text.register { id = "boss.name", language = "eng", value = "EMBER" }
local line = sf2.text.register { id = "boss.line", language = "eng", value = "You dare enter my forge?" }
local fight = sf2.text.register { id = "boss.button", language = "eng", value = "FIGHT" }
local battle = sf2.battles.register {
id = "ember", page = sf2.map.get("core:zones/zone_1"), title = name,
fights = { { rounds = 1,
opponents = { { fighter = "core:warrior-templates/default", level = 12 } } } },
}
sf2.modes.register { id = "ember", fights = battle.fights, repeatable = true }
sf2.story.on("session", function() sf2.map.reveal { battle = battle } end)
sf2.story.before_fight(battle.fights[1], function(request)
local accepted = sf2.story.play_sequence {
steps = { { dialog = { title = name, lines = { { text = line } }, button = fight } } },
on_complete = function() sf2.story.resume_fight(request) end,
on_cancel = function() sf2.story.cancel_fight(request) end,
}
if not accepted then return false end
return nil -- hold the entry until the dialogue ends
end)

Notes:

  • Only a fight your own mod created can be guarded, one handler each. The handler runs on the progression map; fights started from other places, including retries inside a fight, skip it.
  • Never call resume_fight while the entry callback is still running (return true instead). A button press or the end of a dialogue is a good moment.
  • A held entry blocks other fight launches. It has no timeout, so always resume or cancel it. A scene change, profile change or restart invalidates held entries.
  • Remember that the player already saw the scene if it should only run once: keep a flag in mod state.

A sequence is 1 to 64 steps shown in order. Each step is either a dialog (the game’s parchment popup with a speaker name, portrait, pages of text and a final button) or an act_screen (full-screen chapter text with a number of frames per line, 60 per second). Text comes from text handles so it follows the player’s language. A sequence of one step is how you show a single dialog. A dialog page can also show a picture where its text would be, still or animated like a GIF; see picture pages.

To resume a long intro where the player left off, name a state field in position:

sf2.state.register { version = 1, fields = { intro_next = 1, intro_done = false } }
local welcome = sf2.text.register { id = "intro.welcome", language = "eng", value = "Welcome, traveller." }
local ok = sf2.text.register { id = "intro.ok", language = "eng", value = "OK" }
sf2.story.on("scene_enter", function(event)
if event.scene ~= "map" or sf2.state.get("intro_done") then return end
sf2.story.play_sequence {
position = "intro_next",
steps = {
{ act_screen = { lines = { { text = welcome, frames = 180 } } } },
{ dialog = { lines = { { text = welcome } }, button = ok } },
},
on_complete = function() sf2.state.set { intro_done = true } end,
}
end)

The next step number is saved before that step opens, so cancelling leaves the position as it was and finishing leaves it after the last step. Keep the step order of a published sequence stable.

A sequence never opens over the game’s own story. Each step waits until the game’s quests have finished and its dialogs are closed, so a sequence started on scene_enter after a fight shows after the game’s level-up or boss lines. on_step and on_complete also wait for anything the player’s button press set off, so map changes made there (such as sf2.map.reveal or sf2.game.set_eclipse_mode) apply straight away. Leaving the scene while a step is waiting cancels the sequence.

A refused request is not queued: try again from a later event such as the next scene_enter. Dialog fields (title, portrait, mirrored, ignore_back, per-page button) are listed in the reference.

battle_result reports how a fight ended and what the player had equipped, which is how to reward or count something specific:

sf2.story.on("battle_result", function(event)
if event.outcome ~= "win" or not event.equipment then return end
for _, item in ipairs(event.equipment) do
if item.type == "Weapon" and item.subtype == "Katana" then
sf2.log.info("Won with a katana: " .. (event.fight or "unknown fight"))
break
end
end
end)

It only observes, and arrives after the game’s own result presentation. It does not mean rewards or saving are finished. Match equipment on type, not array position, and do not read the inventory for what was used in the fight. For wins and losses of a fight over time, ask sf2.profile.fight.

A purchase can produce both a purchase and an item_acquired event. Listen to the one that matches your purpose so you do not count it twice. item_acquired fires when the count goes up through the game’s grant routine (purchases and rewards), not when you edit the inventory directly.

sf2.story.suppress_quest(id) stops one of the game’s quests from running, at load time:

-- Needs a dependency on core in the manifest.
sf2.story.suppress_quest("core:quests/quests.xml/difficultyalert")

It suppresses a whole quest, not single actions, and disabling the mod and restarting restores it. Quest IDs are core:quests/<source file>/<quest name> in lower case. Two mods suppressing the same quest conflict.

  • Subscriptions run in registration order. Cancelling takes effect at once; a subscription added during delivery first receives the next event.
  • If a callback errors or runs too long, that subscription is cancelled and a message is logged. The others keep running.
  • Limits: 64 active subscriptions per mod. One event and everything it triggers handles at most 128 notifications.
  • Subscriptions are not saved. Register them in your entry script every time, and keep persistent progress in mod state.
  • Local Versus does not run campaign story callbacks or give campaign rewards.