sf2.modes
Modes are repeatable runs of fights, such as an expedition, a tournament or a raid. Register a mode with its fights, and use the other functions when you build each run’s plan yourself. Get sf2 with local sf2 = require("sf2").
sf2.modes.register
Section titled “sf2.modes.register”Register a progression sequence (mode, event or raid) or a standalone encounter, chosen by kind (default "mode").
Signature: sf2.modes.register(definition)
Parameters
| Parameter | Type | Required |
|---|---|---|
definition |
ModeDefinition|RaidDefinition |
Required |
Fields every kind shares (ModeDefinition, RaidDefinition)
| Field | Type | Required | Meaning |
|---|---|---|---|
on_prepare |
fun(request:ModeRequest,event:ModePreparation):EncounterPlan|nil |
Optional | — |
id |
string |
Required | — |
fights |
FightHandle[] |
Required | — |
repeatable |
boolean |
Optional | — |
reset_on_loss |
boolean |
Optional | — |
minimum_level |
integer |
Optional | — |
starts_at |
integer |
Optional | — |
ends_at |
integer |
Optional | — |
entry_item |
string|ItemHandle |
Optional | — |
entry_count |
integer |
Optional | — |
entry_per |
"fight"|"run" |
Optional | “fight” (default) charges the entry item every step; “run” only when a run starts at step 1. |
entry_dialog |
ModeEntryDialog |
Optional | Ask before a fight that charges the entry item: the item count, LEAVE, FIGHT! and the gem pack of the item. Needs entry_item. |
on_result |
fun(result:ModeResult):FightHandle|"complete"|nil |
Optional | — |
More fields of ModeDefinition
| Field | Type | Required | Meaning |
|---|---|---|---|
kind |
"mode"|"event" |
Optional | Default “mode”; “event” is the same with a name for timed events. |
More fields of RaidDefinition
| Field | Type | Required | Meaning |
|---|---|---|---|
kind |
"raid" |
Required | Makes an offline raid. |
hard_mode |
boolean |
Optional | — |
Fields of EncounterDefinition
| Field | Type | Required | Meaning |
|---|---|---|---|
kind |
"encounter" |
Required | Makes a standalone one-fight encounter with its own map entry. |
id |
string |
Required | — |
title |
LocalizationHandle|string |
Required | — |
description |
LocalizationHandle|string |
Optional | — |
page |
string|PageHandle |
Optional | Map page for the encounter’s battle; a new page on Map1.1 when omitted. |
x |
integer |
Optional | Default 0. |
y |
integer |
Optional | Default 0. |
icon |
string |
Optional | — |
location |
string |
Optional | — |
music |
AudioHandle|string |
Optional | Audio handle for music the mod ships, or a built-in track name. |
preview |
SpriteHandle|string |
Optional | Sprite handle for mod-supplied art, or a built-in preview name. |
opponents |
Opponent[] |
Required | 1..64 opponents, fought in order. |
player |
Opponent |
Optional | Temporary player character for this fight. |
rules |
(Rule_no_perks|Rule_require_item|Rule_equip_item|Rule_avatar|Rule_name|Rule_perk|Rule_behavior|Rule_recharge_magic_each_round|Rule_attributes|Rule_no_button|Rule_hot_ground|Rule_ring_out|Rule_regeneration|Rule_no_animation|Rule_no_health_bar|Rule_invert_joystick|Rule_random_area|Rule_light_in_the_darkness|Rule_group|Rule_random|Rule_remove_interval|Rule_resistance)[] |
Optional | Rules for the fight, each a table with a kind. |
rewards |
table<integer,InlineReward> |
Optional | Rewards keyed by wins: [0] a loss, [1] one opponent beaten, and so on. |
rounds |
integer |
Optional | — |
round_time |
integer |
Optional | — |
repeatable |
boolean |
Optional | Default true. |
on_prepare |
fun(request:ModeRequest,event:ModePreparation):EncounterPlan|nil |
Optional | — |
on_result |
fun(result:ModeResult):FightHandle|"complete"|nil |
Optional | — |
Fields of EncounterResult
| Field | Type | Required | Meaning |
|---|---|---|---|
page |
string|PageHandle |
Required | — |
battle |
BattleHandle |
Required | — |
fight |
FightHandle |
Required | — |
quest |
QuestHandle |
Required | — |
Returns: nil, except kind = "encounter", which returns a table of page, battle, fight and quest handles.
When: Entrypoint, after registering its fights.
sf2.modes.register { id = "training_trial", fights = { first, final }, repeatable = true, reset_on_loss = true,}mode. Map battles your mod owns resolve to the sequence’s current fight; a win advances it. For a single battle from sf2.battles.register, pass battle.fights as fights.
Entry cost. entry_item and entry_count charge a consumable of your mod when a step is entered. With entry_per = "run" only the first step of a run costs it, as for a ticketed gauntlet:
sf2.modes.register { id = "gauntlet", fights = battle.fights, repeatable = true, reset_on_loss = true, entry_item = ticket, entry_count = 3, entry_per = "run",}Retry. The pause screen’s Retry button starts a fight over from round one without a loss or a second entry charge. Fights of a mode with reset_on_loss = true, and of a raid, do not get it, since a restart would undo a loss that ends the run.
Entry check. Add entry_dialog to ask before a fight that charges the entry item, as Shadow Fight 2 1.9.38 did before an Ascension run. The dialog shows its portrait, title and text, then “YOU HAVE:” with the item’s picture and count, and three buttons: LEAVE, FIGHT! with the entry cost (greyed out while the player has too few), and the item’s gem pack from the shop (shop = { price = { gems = 30 }, count = 10 } reads “10 for 30”), which buys it and asks again. While the player lacks the items, the map panel keeps the fight button instead of closing.
sf2.modes.register { id = "gauntlet", fights = battle.fights, repeatable = true, reset_on_loss = true, entry_item = ticket, entry_count = 3, entry_per = "run", entry_dialog = { title = sf2.text.get("core:localization/tutorial_ascention_intro_title"), portrait = sf2.assets.sprite("core:ui/users/character_puppeteer"), text = sf2.text.get("core:localization/Ascension_chest_tooltip"), },}entry_dialog field |
Default | Meaning |
|---|---|---|
title |
Required | Text handle or text key for the title. |
portrait |
None | Sprite handle or portrait name shown at the left. |
text |
None | Text handle or text key shown above the item count. |
icon |
The item’s icon | Sprite handle or sprite name for the item’s picture in the dialog’s text and on its buttons. Use it when the item’s shop picture (a stack, a chest) is too busy beside words, such as a single ticket for a pack of tickets. |
event. Same fields as a mode, plus starts_at and ends_at (UTC) only if you want a real schedule.
raid. An offline raid. Its fights must belong to battles with type = "raid"; hard_mode is accepted only here. It is won only by defeating the boss. See the modes guide.
encounter. One standalone fight with its own map entry; opponents, rules and rewards are written inline as in sf2.battles.register. rewards is keyed by wins ([0] for a loss).
local duel = sf2.modes.register { kind = "encounter", id = "rooftop_duel", title = "Rooftop Duel", x = 300, y = 120, opponents = { { fighter = rival, level = 12 } }, rewards = { [1] = { gems = 5 } }, rounds = 2,}-- duel.battle, duel.fight, duel.page and duel.quest are handles.sf2.modes.resolve
Section titled “sf2.modes.resolve”Finish a pending mode fight setup with your chosen plan.
Signature: sf2.modes.resolve(request, plan)
Parameters
| Parameter | Type | Required |
|---|---|---|
request |
ModeRequest |
Required |
plan |
EncounterPlan |
Required |
Fields of EncounterPlan
| Field | Type | Required | Meaning |
|---|---|---|---|
opponents |
(FighterHandle|WarriorHandle)[] |
Optional | Opponent fighter handles. |
player |
FighterHandle|WarriorHandle |
Optional | Player fighter handle. Omit to inherit the blueprint player. |
level |
integer |
Optional | — |
rounds |
integer |
Optional | — |
round_time |
integer |
Optional | — |
rules |
RuleHandle[] |
Optional | Rule handles (a fight’s rules list). An empty list removes the blueprint’s rules. |
description |
string |
Optional | — |
Returns: Nothing.
When: From a UI callback while the setup is pending.
sf2.modes.resolve(request, { opponents = { selected_opponent }, level = 4 })sf2.ui.close(view)Resolving twice, after a cancel, or with an invalid plan is an error. The fight starts after your Lua returns.
sf2.modes.cancel
Section titled “sf2.modes.cancel”Cancel an unresolved mode fight setup without charging a ticket.
Signature: sf2.modes.cancel(request)
Parameters
| Parameter | Type | Required |
|---|---|---|
request |
ModeRequest |
Required |
Returns: Nothing.
When: For example in the setup view’s on_close.
on_close = function() sf2.modes.cancel(request) endCancelling twice, or after the setup was resolved, does nothing, so closing a successful choice view cannot undo the plan.
sf2.modes.is_pending
Section titled “sf2.modes.is_pending”Check whether a mode fight setup is still waiting for a plan.
Signature: sf2.modes.is_pending(request)
Parameters
| Parameter | Type | Required |
|---|---|---|
request |
ModeRequest |
Required |
Returns: true while awaiting a result.
When: Guarding buttons or late callbacks.
if sf2.modes.is_pending(request) then sf2.modes.resolve(request, {})end