Skip to content

sf2.saves

Save options are choices the player makes once per save, such as a difficulty or a path. Declare them, show a chooser, and read what was picked. Get sf2 with local sf2 = require("sf2").

Read the loaded save’s choice for one of your options.

Signature: sf2.saves.get(id)

Parameters

Parameter Type Required
id string Required

Returns: The choice’s id, or the option’s default if none was made.

When: After the profile has loaded.

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

An id your mod did not declare is an error.

Change the save’s choice, for example from your own settings menu.

Signature: sf2.saves.set(id, choice)

Parameters

Parameter Type Required
id string Required
choice string Required

Returns: nil.

When: After the profile has loaded.

sf2.saves.set("difficulty", "brutal") -- inside a button's on_click

choice must be one of the option’s choice IDs. Changing a locked option is an error.

Open the screen for one of your save options so the player can change or lock it.

Signature: sf2.saves.show(id)

Parameters

Parameter Type Required
id string Required

Returns: true when the screen opened; false when it cannot open now.

When: Typically a dojo button click. Opens only on the dojo, map, shop or profile 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)

It is the screen a new save shows. Picking a choice applies it and closes the screen; Back closes without a change.

Declare a per-save choice.

Signature: sf2.saves.option { id, title, description?, choices, default, lockable? }

Parameters

Parameter Type Required
definition SaveOptionDefinition Required

Fields of SaveOptionDefinition

Field Type Required Meaning
id string Required 1-64 ASCII letters, digits or _. At most 4 options per mod.
title string|LocalizationHandle Required A string, or a text handle from sf2.text.get shown in the player’s language. 1-64 characters.
description string|LocalizationHandle Optional A string, or a text handle from sf2.text.get shown in the player’s language. Up to 512 characters.
choices SaveOptionChoice[] Required 2-6 choices, in display order.
default string Required A choice id; used until the save has a choice.
lockable boolean Optional Default false.

Returns: The option’s id.

When: Entrypoint.

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." },
},
}

Each choice takes id (what Lua reads and the save stores), title, and optional subtitle and description. Every text field, on the option and on its choices, takes a string or a text handle from sf2.text.get; a text handle shows in the player’s language, and the length limits apply to its English text. A mod can declare at most 4 options, each with 2 to 6 choices. See saving and options.

Check whether the player has picked a value or is still on the default.

Signature: sf2.saves.is_chosen(id)

Parameters

Parameter Type Required
id string Required

Returns: true when the save holds a choice; false on the default.

When: After the profile has loaded.

sf2.story.on("scene_enter", function(event)
if event.scene == "map" and not sf2.saves.is_chosen("difficulty") then
sf2.saves.show("difficulty")
end
end)

Check whether a save option is locked.

Signature: sf2.saves.is_locked(id)

Parameters

Parameter Type Required
id string Required

Returns: true when locked.

When: After the profile has loaded.

local locked = sf2.saves.is_locked("difficulty")

Lock the save’s current choice for good.

Signature: sf2.saves.lock(id)

Parameters

Parameter Type Required
id string Required

Returns: nil.

When: After the profile has loaded.

-- Ask the player to confirm first: a lock cannot be undone.
sf2.saves.lock("difficulty")

The current choice (the default if none) is kept and sf2.saves.set then fails. There is no unlock.

Get the save’s random seed, stable for the life of the save.

Signature: sf2.saves.seed()

Returns: An integer from 1 to 2,147,483,647.

When: After the profile has loaded.

local roll = sf2.random.stream(sf2.saves.seed(), "my_fight_id")

Combine it with something stable, such as a fight ID, so each fight rolls differently but always the same way for this save. It is shared by every mod and is not for security. See sf2.random.stream.