Skip to content

Saving and options

Players keep their progress in campaigns: separate saves, each with its own items, fights won and mod data. This guide shows where your mod can store things in a campaign, how to read what the player has done, and what happens to a save when your mod is updated, disabled or removed. Examples assume local sf2 = require("sf2").

Pick the place that fits what you are storing:

You want to remember… Use
Your mod’s own progress, such as “tutorial finished” or a win count Mod state
A choice the player makes once per campaign, such as a difficulty Save options
A repeatable random draw Random streams
A number that only goes up, with an achievement at a threshold Counters and achievements
Something that lasts one round or one fight A behaviour’s state (see Perks and enchantments)
Player settings shared by all campaigns Menus and settings

Declare the names, types and starting values of your saved data once, while the mod loads. Reading and writing is only possible after a profile has loaded, so do that inside a callback, not at the top of your script.

local sf2 = require("sf2")
sf2.state.register {
version = 1,
fields = {
victories = 0, -- integer, starts at 0
finished = false, -- boolean, starts false
nickname = { type = "string", required = false }, -- absent until set
},
}
sf2.story.on("fight_end", function(event)
if event.result == "win" then
sf2.state.set { victories = sf2.state.get("victories") + 1 }
end
end)

A field written as a plain value takes its type from it: a whole number is an integer, any other number is a number, and true/false and strings are booleans and strings. The long form { type, default?, required? } covers fractions that start whole ({ type = "number", default = 1 }) and optional fields. A plain type word such as victories = "integer" is an error, because a plain value is always the starting value.

  • sf2.state.get(name) reads, sf2.state.set { ... } writes several fields at once (all or none), and sf2.state.unset(name) returns a field to its default.
  • Up to 64 fields. Names are 1 to 64 letters, digits or underscores. Strings hold up to 2,048 characters. Tables, functions and handles cannot be saved.
  • On the title screen’s fighter preview, state is not bound. It becomes available when the selected campaign loads.
  • A write outside a fight, such as marking a story dialogue as seen, is saved to disk within about a second. A write during a fight is saved with the fight’s result.

When a new version of your mod changes the shape of its data, raise version and say how to carry old data over:

sf2.state.register {
version = 2,
fields = { victories = 0, finished = false },
aliases = { wins = "victories" }, -- a field that was renamed
tombstones = { "old_hint" }, -- a retired name that must never be reused
migrations = {
[1] = function(old) -- steps a version-1 save up to version 2
old.finished = old.finished or false
end,
},
}

Each migration receives a copy of the old values and may change it or return a replacement table. If a step fails, the player’s previous data is kept. A save written by a newer version of your mod is never silently downgraded. Do not reuse an old field name for a different meaning or type.

A save option is a choice the player makes once for each campaign, such as a difficulty. Your mod declares the option and its choices while it loads. Eclipse asks on the title screen when the player creates a campaign, or when they open an older campaign that has no answer yet, and stores the answer inside that campaign.

sf2.saves.option {
id = "difficulty",
title = "Choose your difficulty",
default = "normal",
lockable = true,
choices = {
{ id = "relaxed", title = "Relaxed", subtitle = "For the story" },
{ id = "normal", title = "Normal" },
{ id = "brutal", title = "Brutal", description = "Foes hit twice as hard." },
},
}

An option has 2 to 6 choices and a default; a mod may declare up to 4 options. lockable = true adds a Locked for this save switch so the player can commit to a choice for good. Titles are plain text, shown as written. See sf2.saves.option for every field.

Read the answer from any callback after the profile loads:

sf2.story.on("scene_enter", function(event)
if event.scene == "map" then
sf2.log.info("Difficulty: " .. sf2.saves.get("difficulty"))
end
end)

Until a campaign has an answer, sf2.saves.get returns the default and sf2.saves.is_chosen returns false. sf2.saves.set changes the answer (an error if it is locked), sf2.saves.lock locks it permanently, and sf2.saves.is_locked asks. To let the player revisit the question, put a dojo button on a screen and open Eclipse’s own question screen:

local button = sf2.ui.dojo_button { id = "difficulty",
image = sf2.assets.sprite("sprites/difficulty/button") }
sf2.story.on("dojo_button", function(event)
if event.button == button then sf2.saves.show("difficulty") end
end)

Answers stay in the campaign when the mod is disabled and are used again when it returns. An answer that your mod no longer offers counts as no answer, so the player is asked again. Each campaign also gets a permanent random seed (sf2.saves.seed()) for choices that must repeat per campaign. The seed is shared by all mods and is not secret.

Use an option to change fights (sf2.battles.on_prepare) or prices (sf2.game.economy).

Eclipse has two kinds of repeatable randomness. Pick by what should decide the result.

A seeded stream when the result should follow from things you already know, such as the campaign, the fight and the opponent. The same inputs always roll the same values, and nothing is saved:

-- Inside sf2.battles.on_prepare, where `fight` and `enemy` exist:
local roll = sf2.random.stream(sf2.saves.seed(), fight.id, enemy.index)
local kind = roll:pick { "bleeding", "frenzy", "stun" }
if roll:number() < 0.25 then
sf2.log.info("This opponent also gets a second enchantment.")
end

The seed is 1 to 16 numbers or strings, in order. A stream has :integer(min, max), :number() (from 0 up to, but not including, 1) and :pick(list). It works anywhere, including while loading. See sf2.random.stream.

A saved sequence when each roll should continue from the last one, such as “which fight comes next in this run”. Its position lives in an integer state field, so reloading continues it:

sf2.state.register { version = 1, fields = { route_rng = 12345 } } -- the default is the seed
-- Inside a callback after the profile loads, such as a mode's on_result:
local pick = sf2.random.integer("route_rng", 2, 3) -- 2 or 3, equally likely
local rare = sf2.random.number("route_rng") < 0.1 -- 10% chance

Use a separate field for each unrelated choice. Draws are written to the profile at once and are not undone if your script fails later. Neither kind touches Lua’s math.random or combat randomness, and neither is for secrets.

The sf2.profile functions read the loaded campaign: its level, what it owns and wears, the perks it has learned, and which fights it has won. Call them after a profile has loaded. Every call returns a fresh table; changing it does not change the save.

local sf2 = require("sf2")
local nunchaku = sf2.items.get("core:items/weapon/weapon_nunchaku")
sf2.story.on("scene_enter", function(event)
if event.scene ~= "map" then return end
local lynx = sf2.profile.fight("core:fights/zone_1/boss_lynx/6")
local weapon = sf2.profile.item(nunchaku)
sf2.log.info("Level " .. sf2.profile.level()
.. ", lynx wins " .. lynx.wins
.. ", nunchaku " .. (weapon.equipped and "equipped" or weapon.owned and "owned" or "missing"))
end)
Function Tells you
sf2.profile.level() The player’s level.
sf2.profile.fight(fight) present, wins and losses for a fight.
sf2.profile.has_defeated(fighter) Whether the player ever beat an opponent, matched by fighter or its blueprint.
sf2.profile.item(item) owned, count, equipped, upgrade, type and subtype.
sf2.profile.equipment() Everything equipped, with each item’s enchantments.
sf2.profile.loadouts() The three gear sets the player saved in the Armory, and whether each can still be worn.
sf2.profile.equip_loadout(index) Puts one of those sets on, as the Armory’s Equip does. In a fight it needs a pausing pop-up.
sf2.profile.perk(perk) Whether a perk is learned, and its upgrade.
sf2.profile.give_item(item) Gives the player one copy of an item, as a quest would.

Things to know:

  • Other namespaces, including core, need a dependency in your manifest.
  • An unknown or forged ID raises an error. It never looks like “not owned”.
  • owned means the player has the item right now, not that they ever bought it. A fight’s win count can be reset by the game, so it is not a permanent history. has_defeated is permanent: Eclipse records each beaten opponent and its blueprint.
  • give_item does not check ownership. Check with sf2.profile.item first, and use either a fight reward or give_item for one prize, not both.

A counter is a whole number saved with the player’s profile. It belongs to your mod and can only go up. An achievement watches one counter and unlocks when it reaches a threshold. They appear in Eclipse’s Profile screen; they are not Steam or platform achievements.

local wins = sf2.counters.register { id = "wins", maximum = 1000000 }
sf2.achievements.register {
id = "first_win", counter = wins, threshold = 1,
title = sf2.text.register { id = "ach.first_win", language = "eng", value = "First Blood" },
description = sf2.text.register { id = "ach.first_win.desc", language = "eng", value = "Win a fight." },
icon = sf2.assets.sprite("sprites/medal"),
}
sf2.story.on("fight_end", function(event)
if event.result == "win" then sf2.counters.add(wins, 1) end
end)

A counter does not listen to anything by itself: call sf2.counters.add from the callback where the thing happens, and be careful not to count the same event in two places. Counters start at zero and cap at maximum (default 1,000,000,000). sf2.counters.get reads one. Achievements have no reward field and no promised unlock popup. Renaming a counter ID starts fresh progress.

On the title screen, Campaign creates, selects, renames or deletes campaigns. Each is a separate save with its own progress, including mod state. Enabled mods and installation-wide settings are shared by every campaign. A campaign last played with mods on is marked Modded and lists them.

Eclipse keeps up to five automatic backups of each campaign. One is made the first time a different Eclipse version opens it, and whenever it loads with a different set of enabled mods or mod versions. To restore one, choose a campaign, then Backups, pick a date and confirm Restore. Your current save is backed up first, so you can undo a restore. Back up now makes one on demand. If a save cannot be read at all, opening it shows the backup list instead, and Eclipse never silently starts a new profile over a damaged one.

A player can mark one campaign as their main save (Make main on the campaign list, or when a new campaign asks). While they are logged in to Eclipse online, the main save syncs between their devices, so they can carry on with the same campaign on a phone and a PC. A save that ever had the cheat menu cannot be the main save.

For a mod, a synced save is the same save: your saved state, items and quests travel inside it, along with the list of mods it was played with. The other device needs your mod installed to use them; without it, the rules in the next section apply there too, including save_binding. Keep your saved data readable by older and newer versions of your mod (see Upgrading saved data), because two devices may run different versions of it.

When a campaign was last played with a mod that is off or missing, Eclipse asks before loading:

Choice What happens
Turn on & enter Turns on the installed mods and their dependencies, restarts and opens the campaign. Offered when at least one listed mod is installed.
Enter anyway Opens the campaign without those mods. Their content stays in the save.
Cancel Back to the campaign list.

A mod whose manifest has save_binding = "required" owns the saves made while it is on, which suits a full overhaul. A save made with it opens only while it is on, and a save made without it will not open while it is on. Opening it with the wrong mods shows This save belongs to other mods, with a button that switches them and restarts. Use this sparingly; it takes away the player’s choice.

Everything a mod owns stays in the save while the mod is unavailable, and comes back when it is re-enabled:

  • Inventory. Mod items keep their counts, upgrades and enchantments, but are left out of active inventory and equipment. If a missing item was equipped, the fighter shows a normal default item without overwriting the saved choice.
  • Progress. Mod state, saved battles and in-progress quests are kept.
  • Rewards. A pending reward naming a missing mod’s item stays unclaimable until the mod returns.

If a mod fails to load, read its Details and fix it. The player’s save is left as it was.

A save finds your content by stable IDs. Choose your manifest id and your local content IDs before you publish, and keep them the same in every update.

  • Keep an item’s ID even if its display name changes. If it must change, call sf2.items.retire { id = old, replaced_by = new } so old saves find the new item. Retiring without replaced_by reserves the old ID and keeps the record as unavailable; it does not erase ownership or give a replacement.
  • Keep fights and battles in the same order with the same names (Battles and bosses).
  • Change saved fields only through the schema, alias, tombstone and migration rules above.
  • Test before shipping: open a campaign from the previous version with owned and equipped items, disable and re-enable the mod, and restart Eclipse after changing files.

The save also records mod versions for diagnostics. A changed version does not invalidate the player’s save.