sf2.story
Story events: react to what happens in the game, play dialogue and act screens, control the fights that quests start, and keep story variables. Get sf2 with local sf2 = require("sf2").
sf2.story.on
Section titled “sf2.story.on”Listen for one kind of story event.
Signature: sf2.story.on(event, callback)
Parameters
| Parameter | Type | Required |
|---|---|---|
event |
"purchase"|"enchantment"|"level_up"|"scene_enter"|"map_button"|"dojo_button"|"item_acquired"|"battle_result"|"session"|"fight_start"|"fight_end"|"raid_enter"|"raid_end"|"raid_fight_start"|"raid_fight_end"|"raid_map_enter"|"raid_floor_changed"|"mode_reset"|"timer_end"|"perk_activated"|"perk_deactivated"|"delivery"|"shop_enter" |
Required |
callback |
fun(event: StoryEvent) |
Required |
Fields of StoryEvent
| Field | Type | Required | Meaning |
|---|---|---|---|
kind |
"purchase"|"enchantment"|"level_up"|"scene_enter"|"map_button"|"dojo_button"|"item_acquired"|"battle_result"|"session"|"fight_start"|"fight_end"|"raid_enter"|"raid_end"|"raid_fight_start"|"raid_fight_end"|"raid_map_enter"|"raid_floor_changed"|"mode_reset"|"timer_end"|"perk_activated"|"perk_deactivated"|"delivery"|"shop_enter" |
Required | — |
fight |
string |
Optional | — |
outcome |
"win"|"loss"|"surrender"|"raid_timeout"|"raid_round_timeout" |
Optional | — |
eclipse |
boolean |
Optional | — |
equipment |
BattleEquipmentSnapshot[] |
Optional | — |
item |
string |
Optional | — |
recipe |
string |
Optional | — |
previous_count |
integer |
Optional | — |
count |
integer |
Optional | — |
previous_level |
integer |
Optional | — |
level |
integer |
Optional | — |
scene |
"map"|"shop"|"profile"|"dojo"|"fight" |
Optional | — |
button |
string |
Optional | — |
battle |
string |
Optional | Quest events: qualified battle ID of the event’s fight. |
mode |
string |
Optional | Quest events: qualified mode ID when the fight belongs to a mode. |
result |
"win"|"loss"|"surrender" |
Optional | fight_end, raid_fight_end, raid_end and mode_reset. |
perk |
string |
Optional | perk_activated and perk_deactivated. |
timer |
string |
Optional | timer_end: the game’s timer name. |
Returns: A subscription handle, needed only to cancel with sf2.story.off.
When: Entrypoint or any callback, even before a profile loads.
-- Fight IDs are <mod-id>:fights/<battle>_<fight name>; fights are named 1, 2, ...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" } endend)The event fields depend on the event name; see the story events guide for the full list.
sf2.story.off
Section titled “sf2.story.off”Stop listening to an event.
Signature: sf2.story.off(subscription)
Parameters
| Parameter | Type | Required |
|---|---|---|
subscription |
StorySubscription |
Required |
Returns: Nothing. Cancelling twice is harmless.
When: Anywhere, including inside the subscription’s own callback.
local subscriptionsubscription = sf2.story.on("enchantment", function(event) sf2.story.off(subscription) -- Observe only the next enchantment.end)sf2.story.is_active
Section titled “sf2.story.is_active”Check whether a subscription is still listening.
Signature: sf2.story.is_active(subscription)
Parameters
| Parameter | Type | Required |
|---|---|---|
subscription |
StorySubscription |
Required |
Returns: false after it was cancelled or its callback failed.
When: Anywhere.
if sf2.story.is_active(purchases) then sf2.story.off(purchases)endsf2.story.before_fight
Section titled “sf2.story.before_fight”Run your own code, such as a dialogue, before a fight starts: allow it, cancel it, or hold it while your UI shows.
Signature: sf2.story.before_fight(fight, on_before_fight) -> nil
Parameters
| Parameter | Type | Required |
|---|---|---|
fight |
FightHandle |
Required |
on_before_fight |
fun(request:FightEntryRequest):boolean|nil |
Required |
Returns: Nothing. Registering a fight twice, or one your mod does not own, is an error.
When: Entrypoint, after creating the fight. Runs when the player enters the fight from the map; retries and other scenes do not call it.
sf2.story.before_fight(first_fight, function(request) sf2.ui.open { id = "entry", mount = "modal", root = {id = "begin", kind = "button", width = 300, height = 80, text = "Begin"}, on_click = function(view) sf2.ui.close(view) sf2.story.resume_fight(request) end, on_back = function(view) sf2.story.cancel_fight(request) sf2.ui.close(view) end, } return nilend)Return true to start at once, false to cancel, or nil to hold. A held fight must be resumed (sf2.story.resume_fight) or cancelled (sf2.story.cancel_fight); there is no timeout, and other fight launches stay blocked meanwhile. A scene change, profile change or script unload drops it. Save your own “already shown” flag if the presentation should run once.
One mod can guard up to 128 fights, and all mods together 256; registering more is an error.
sf2.story.start_fight
Section titled “sf2.story.start_fight”Start a fight with the game’s normal entry checks and costs.
Signature: sf2.story.start_fight(fight?)
Parameters
| Parameter | Type | Required |
|---|---|---|
fight |
string|FightHandle,Omit to start the fight of the event being handled, or the selected fight. |
Optional |
Returns: true when the game’s fight action ran; false if it cannot run now. true does not promise combat loads.
When: On the map or in a story event handler. With no argument it starts the fight of the event being handled. Not callable from UI on_close.
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") endend)It clears quests still waiting in the queue before starting.
sf2.story.fight_pending
Section titled “sf2.story.fight_pending”Check whether a held fight can still be resumed.
Signature: sf2.story.fight_pending(request) -> boolean
Parameters
| Parameter | Type | Required |
|---|---|---|
request |
FightEntryRequest |
Required |
Returns: true while resumable; false after a scene or profile change or script unload.
When: Before continuing an asynchronous presentation.
if sf2.story.fight_pending(request) then sf2.story.resume_fight(request)endsf2.story.resume_fight
Section titled “sf2.story.resume_fight”Let a held fight go ahead.
Signature: sf2.story.resume_fight(request) -> boolean
Parameters
| Parameter | Type | Required |
|---|---|---|
request |
FightEntryRequest |
Required |
Returns: true when accepted; false for an invalid or used request. If the map is briefly blocked it also returns false but the request stays pending, so retry.
When: After the entry callback has returned. Close your dialogue first; not callable from UI cleanup callbacks.
sf2.ui.close(dialogue)local accepted = sf2.story.resume_fight(request)if not accepted then sf2.story.cancel_fight(request) endYour before_fight handler is skipped once; the game’s normal entry then runs and may start its own entry quests before combat.
sf2.story.cancel_fight
Section titled “sf2.story.cancel_fight”Give up on a held fight.
Signature: sf2.story.cancel_fight(request) -> nil
Parameters
| Parameter | Type | Required |
|---|---|---|
request |
FightEntryRequest |
Required |
Returns: Nothing. Cancelling twice is harmless.
When: When the player backs out. It does not close your UI.
sf2.story.cancel_fight(request)sf2.story.play_sequence
Section titled “sf2.story.play_sequence”Show story dialogs and full-screen act screens in order, optionally remembering the step reached.
Signature: sf2.story.play_sequence(definition)
Parameters
| Parameter | Type | Required |
|---|---|---|
definition |
StorySequenceDefinition |
Required |
Fields of StorySequenceDefinition
| Field | Type | Required | Meaning |
|---|---|---|---|
steps |
StorySequenceStep[] |
Required | — |
position |
string |
Optional | — |
on_complete |
fun() |
Optional | — |
on_cancel |
fun() |
Optional | — |
on_step |
fun(index:integer):boolean|nil |
Optional | — |
Returns: true when accepted; false if another sequence from this script is active or the first step is refused. An invalid definition is an error.
When: A story or fight-entry callback after the profile has loaded. A scene or profile change cancels playback.
sf2.state.register { version = 1, fields = { intro_next = 1, intro_done = false } }local welcome = sf2.text.get("welcome")local ok = sf2.text.get("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 = { title = welcome, lines = { { text = welcome } }, button = ok } }, }, on_complete = function() sf2.state.set { intro_done = true } end, }end)Each step is one dialog or one act_screen. Pass text handles from sf2.text.get, not plain strings. With position set to a state field you own, the next step number is saved before it opens, so a cancelled sequence resumes where it stopped. Finishing leaves it at #steps + 1; set it back to 1 to replay.
on_step(index) runs before each step (return false to cancel) and may run again after an interruption, so keep it safe to repeat. on_cancel and on_complete never both run. A refused request is not queued; try again from a later event. Branching and rewards belong in your own Lua.
A dialog page can show a picture instead of words: give the line picture in place of text. One frame is a still picture. For an animation such as a converted GIF, put every frame in one sprite sheet, left to right and top to bottom, and the dialog loops them at fps where the text would be, sized to fit with its shape kept.
-- assets/textures/clip.png: 24 frames, 6 to a row; assets/sprites/clip.asset covers the whole sheet.sf2.story.play_sequence { steps = { { dialog = { title = sf2.text.get("rival.name"), portrait = sf2.assets.sprite("sprites/rival"), lines = { { picture = { sprite = sf2.assets.sprite("sprites/clip"), columns = 6, frames = 24, fps = 12 } } }, button = sf2.text.get("ok"), } } },}frames is 1 to 256 (default 1), columns defaults to all frames in one row, and fps is 1 to 60 (default 12). Keep the sheet within 4096 pixels on each side so phones can load it.
Steps never open over the game’s own story: each one waits until the game’s quests have finished and its dialogs are closed, and on_step and on_complete wait for anything the player’s button press set off. A waiting sequence counts as active, and leaving the scene cancels it.
sf2.story.set_variable
Section titled “sf2.story.set_variable”Set one of the game’s saved quest variables.
Signature: sf2.story.set_variable { name, value }
Parameters
| Parameter | Type | Required |
|---|---|---|
definition |
StoryVariable |
Required |
Fields of StoryVariable
| Field | Type | Required | Meaning |
|---|---|---|---|
name |
string |
Required | A letter, then letters, digits or _; at most 64. |
value |
string|number|boolean |
Required | Plain text not starting with _ or ?. Numbers as text, booleans as “1”/“0”. |
Returns: true when set; false when no profile is loaded or a screen change is in progress.
When: After the profile has loaded.
sf2.story.set_variable { name = "DifficultyAlert", value = true }These variables are shared with every mod and the game’s quests. For your own progress use mod state.
sf2.story.variable
Section titled “sf2.story.variable”Read one of the game’s saved quest variables.
Signature: sf2.story.variable(name)
Parameters
| Parameter | Type | Required |
|---|---|---|
name |
string |
Required |
Returns: The value as a string (all values are text), or nil if never set.
When: After the profile has loaded.
if sf2.story.variable("DifficultyAlert") == "1" then sf2.log.info("The difficulty notice was already shown")endsf2.story.suppress_quest
Section titled “sf2.story.suppress_quest”Stop one of the game’s quests from running.
Signature: sf2.story.suppress_quest(id)
Parameters
| Parameter | Type | Required |
|---|---|---|
quest |
string,A quest ID such as "core:quests/quests.xml/difficultyalert". |
Required |
Returns: Nothing.
When: Entrypoint. Takes effect after Apply & Restart.
sf2.story.suppress_quest("core:quests/quests.xml/difficultyalert")The whole quest is suppressed; its saved progress is kept, and disabling your mod makes it run again. Core quest IDs are lowercase core:quests/<source-file>/<quest-name> with the .xml file name. Unknown quests, or two mods suppressing the same one, fail loading.