Skip to content

sf2.profile

Read what the player has done and owns: their level, items, perks, equipment, Armory loadouts and fight record, give them items, spend the consumables they hold, switch their gear to a saved loadout, stop them selling one your story still needs, and unlock armor and helm looks. Get sf2 with local sf2 = require("sf2").

Read the player’s current level.

Signature: sf2.profile.level()

Returns: The level as an integer.

When: After the profile has loaded.

sf2.ui.update(view, "level", { text = "Player level: " .. sf2.profile.level() })

This is the player’s level, not an opponent’s.

Look up whether the player owns or has equipped an item, and how many.

Signature: sf2.profile.item(item)

Parameters

Parameter Type Required
item ItemHandle|string Required

Returns: A table with type, subtype, present, owned, count, equipped and upgrade.

When: After the profile has loaded.

local nunchaku = sf2.items.get("core:items/weapon/weapon_nunchaku")
local item = sf2.profile.item(nunchaku)
if item.equipped then
sf2.log.info("Nunchaku equipped")
elseif item.owned then
sf2.log.info("Nunchaku in inventory")
end

owned means the player has it now (count > 0); a record can exist with present = true, owned = false. type and subtype come from the catalog, so they are set even for unowned items. upgrade is nil without a record. An unknown handle raises an error. The table is a snapshot.

Count how many pieces of an item the player holds.

Signature: sf2.profile.item_count(item)

Parameters

Parameter Type Required
item string|ItemHandle Required

Returns: A whole number: the pieces held, 0 when the player has none.

When: After the profile has loaded, in or out of a fight.

local elixir = sf2.items.register {
category = "consumable",
id = "healing_elixir",
display_name = sf2.text.get("item.healing_elixir"),
icon = sf2.assets.sprite("sprites/healing_elixir"),
stack = 9,
shop = { price = { coins = 250 } },
}
local function elixir_label()
return "Healing elixir x" .. sf2.profile.item_count(elixir)
end

item is an item handle, a qualified ID such as sf2.mod.id .. ":items/consumable/healing_elixir", or this mod’s local ID ("consumable/healing_elixir"). It works for any item: for equipment it is the number of copies owned. It is the same number as count in sf2.profile.item. Raises an error when no save is loaded.

Check whether the player has learned a perk, and at what upgrade level.

Signature: sf2.profile.perk(perk)

Parameters

Parameter Type Required
perk PerkHandle|string Required

Returns: A table with learned and upgrade (nil unless learned). An upgrade of 0 is valid.

When: After the profile has loaded.

local cobra = sf2.perks.get("core:perks/PERK_COBRA")
local perk = sf2.profile.perk(cobra)
if perk.learned then
sf2.log.info("Cobra upgrade: " .. perk.upgrade)
end

It ignores temporary combat effects and equipment enchantments.

List everything the player has equipped.

Signature: sf2.profile.equipment()

Returns: A fresh array of tables with item, type, subtype, count, owned, upgrade and enchantments (perk ID strings).

When: After the profile has loaded.

local function wearing_enchantment(perk_id)
for _, item in ipairs(sf2.profile.equipment()) do
if item.owned then
for _, perk in ipairs(item.enchantments) do
if perk == perk_id then return true end
end
end
end
return false
end
local storm = wearing_enchantment("core:perks/perk_hermitstorm")

IDs are lower case, so compare in lower case. Do not rely on array positions; check type and subtype. Zero-quantity records are listed with owned = false. It reads the profile only, not a fight’s temporary loadout, so query again after equipment changes.

List the three gear loadouts the player saved in the Armory.

Signature: sf2.profile.loadouts()

Returns: A fresh array of three tables, one per Armory slot in order, each with index, name, items (item ID strings) and complete.

When: After the profile has loaded.

-- One button per Armory loadout, for a menu of your own.
local function loadout_buttons(on_pick)
local buttons = {}
for _, loadout in ipairs(sf2.profile.loadouts()) do
buttons[#buttons + 1] = {
id = "loadout_" .. loadout.index,
kind = "button", width = 360, height = 48,
text = loadout.name,
enabled = #loadout.items > 0, -- nothing saved in this slot yet
on_click = function(view) on_pick(view, loadout.index) end,
}
end
return buttons
end

The Armory in the shop keeps three loadouts: sets of gear (weapon, armor, helm, ranged weapon and magic) the player saves with Save current and puts on with Equip. This function reads them; switch to one with sf2.profile.equip_loadout. Only the player can save a loadout, in the Armory.

Every slot is listed, so sf2.profile.loadouts()[2] is always slot 2. An empty slot has an empty items list and complete = false.

  • name is the title the Armory shows for the slot (such as “Loadout 1”), in the player’s language. Loadouts have no names of their own.
  • items holds the IDs of the saved pieces in Armory order, the same IDs sf2.profile.item takes, so you can ask about one piece, for example sf2.profile.item(id).owned. A piece whose mod is no longer installed is left out.
  • complete is true when the player can still wear every saved piece. It is false when a piece is left out (or no longer owned); equipping such a loadout puts on only the pieces that are still there.
  • Selling a piece in the shop takes it out of every loadout, so items stops listing it.

Changing the returned tables changes nothing in the save.

Switch the player’s gear to one of the loadouts they saved in the Armory.

Signature: sf2.profile.equip_loadout(index)

Parameters

Parameter Type Required
index integer Required

Returns: true when the player’s gear changed; false when nothing changed: the slot is empty, the player already wears it, or a screen change is in progress.

When: After the profile has loaded, outside fights, or during a fight while a pause_fight view or a Prepare screen holds it.

-- A "Prepare" pop-up over a running fight: the fight waits while the player picks.
local function open_prepare()
local rows = {}
for _, loadout in ipairs(sf2.profile.loadouts()) do
rows[#rows + 1] = {
id = "loadout_" .. loadout.index,
kind = "button", width = 360, height = 48,
text = loadout.name,
enabled = #loadout.items > 0,
on_click = function(view)
sf2.profile.equip_loadout(loadout.index)
sf2.ui.close(view) -- the fight goes on in the new gear
end,
}
end
rows[#rows + 1] = { id = "back", kind = "button", width = 360, height = 48, text = "Keep my gear",
on_click = function(view) sf2.ui.close(view) end }
sf2.ui.open {
mount = "modal",
pause_fight = true, -- freeze the fight while this is open
root = { kind = "column", width = 360, height = 56 * #rows, gap = 8, children = rows },
}
end

index is the slot number, 1 to 3, as sf2.profile.loadouts lists it. Anything else is an error.

It does exactly what the Armory’s Equip button does: each saved piece the player still owns goes on, a type saved with its default item goes back to it, and pieces the player no longer owns are skipped while the rest still change (selling a piece also takes it out of the loadouts). Types the loadout does not name stay as they are. The change is saved with the profile, and completing a set shows the game’s set-bonus notice, as in the Armory.

Outside a fight (the map, a menu before a fight) it changes the profile, and the next fight uses the new gear.

During a fight it works only while a view opened with pause_fight = true or a Prepare screen holds the fight, and only while a round is being fought. The player’s fighter is rebuilt in the new gear as the fight resumes: it keeps its share of health, its place and facing, its rounds won and its magic charge, and later rounds keep the new gear. Anywhere else in a fight the call is an error, as it is when the player fights as a character the fight or a mod gives them (including after fighter:change_form) rather than as their own fighter.

It is an error in versus and online play.

Ask whether the player has ever beaten a particular opponent.

Signature: sf2.profile.has_defeated(warrior)

Parameters

Parameter Type Required
warrior WarriorHandle|FighterHandle|string Required

Returns: true if the profile has a recorded or provable win against warrior.

When: After the profile has loaded.

local function hermit_beaten()
return sf2.profile.has_defeated("core:warrior-templates/hermit_swords")
end

warrior is a fighter handle or a qualified ID; other mods’ IDs need a declared dependency. A win is recorded under the opponent’s warrior ID and its warrior template ID. Core opponents have no warrior IDs of their own, so ask about their template; a template is true after a win against any opponent built from it. In a fight with several opponents, only the last one beaten is recorded. Losses, surrenders and draws record nothing.

Most Underworld raid bosses share the generic core:warrior-templates/default template, so give your own raid bosses their own warrior or template ID.

Read saved progress for a fight without changing it.

Signature: sf2.profile.fight(fight)

Parameters

Parameter Type Required
fight FightHandle|string Required

Returns: A table with present, wins and losses. A fight with no record returns false, 0, 0.

When: After the profile has loaded.

sf2.story.on("battle_result", function(event)
local progress = sf2.profile.fight("core:fights/zone_1/tournament/3")
if event.outcome == "win" and progress.wins >= 1 then
sf2.log.info("The first tournament prerequisite is complete.")
end
end)

These are fight win and loss counts, not rounds, and the game can reset them.

Give the player an item, or several pieces of a stackable consumable.

Signature: sf2.profile.give_item(item, count?)

Parameters

Parameter Type Required
item string|ItemHandle Required
count integer Optional

Returns: true when granted; false when no profile is loaded, a screen change is in progress, or the player already holds a full stack.

When: After the profile has loaded, for example in a story event handler.

local blade = sf2.items.get(sf2.mod.id .. ":items/weapon/boss_blade")
local elixir = sf2.items.get(sf2.mod.id .. ":items/consumable/healing_elixir")
sf2.story.on("fight_end", function(event)
if event.fight == sf2.mod.id .. ":fights/boss_1" and event.result == "win" then
if not sf2.profile.item(blade).owned then
sf2.profile.give_item(blade)
end
sf2.profile.give_item(elixir, 2) -- two pieces, as far as the stack allows
end
end)

count defaults to 1. A count above 1 needs a stackable consumable (an item registered with stack, see sf2.items.register). A stackable consumable is given up to its stack: pieces that do not fit are dropped, and the call returns false only when not even one fits. Use sf2.profile.item_count to see how many the player holds.

It does not check ownership of equipment; use sf2.profile.item first if that matters. Use either a fight reward or give_item for one reward, not both, or it is awarded twice.

Use up pieces of a consumable the player holds.

Signature: sf2.profile.take_item(item, count?)

Parameters

Parameter Type Required
item string|ItemHandle Required
count integer Optional

Returns: true when the pieces were taken; false when the player holds fewer, no profile is loaded or the save cannot change right now.

When: After the profile has loaded, in or out of a fight: from a menu button, a story event or a combat callback.

local elixir = sf2.items.get(sf2.mod.id .. ":items/consumable/healing_elixir")
-- A battle rule behavior: at the start of a round below half health, the player
-- drinks one healing elixir, if they hold one, and gets a quarter of their health back.
local auto_elixir = {
on_round_begin = function(_, fighter)
local now = fighter:snapshot()
if now and now.self.health < 0.5 and sf2.profile.take_item(elixir) then
fighter:change_health(0.25)
end
end,
}

count defaults to 1 and can be up to 999. Nothing is taken unless the player holds at least count pieces, so check the result before you apply the effect.

Only consumable items can be taken; equipment, chests and other items raise an error. When the last piece goes, the item leaves the inventory: the shop shows it as not owned again, and sf2.profile.item reports present = false. The change is saved with the profile.

Taking an item applies no effect by itself. Your script decides what a piece does, for example with fighter:change_health or fighter:add_damage_shield.

Stop the player selling an item, and tell them why.

Signature: sf2.profile.keep_item { item, reason }

Parameters

Parameter Type Required
definition KeepItem Required

Fields of KeepItem

Field Type Required Meaning
item string|ItemHandle Required The item to keep.
reason string Required 1..160 characters, shown when the player tries to sell it.

Returns: nil.

When: After the profile has loaded, for example in a story event handler.

local katana = sf2.items.get("core:items/weapon/weapon_katana")
sf2.story.on("item_acquired", function(event)
if event.item == "core:items/weapon/weapon_katana" then
sf2.profile.keep_item { item = katana, reason = "Sensei still needs this katana." }
end
end)

reason is shown when the player presses Sell on the item, so write it for them. The save remembers the item until sf2.profile.release_item releases it, even while the player doesn’t own it. Calling it again for the same item replaces your reason. If several mods keep one item, each must release it; a mod that is turned off stops blocking. Raises an error when no save is loaded.

Let the player sell an item your mod kept again.

Signature: sf2.profile.release_item(item)

Parameters

Parameter Type Required
item string|ItemHandle Required

Returns: nil.

When: After the profile has loaded, for example in a story event handler.

sf2.story.on("fight_end", function(event)
if event.fight == sf2.mod.id .. ":fights/katana_trial_1" and event.result == "win" then
sf2.profile.release_item("core:items/weapon/weapon_katana")
end
end)

It only releases your own sf2.profile.keep_item; another mod keeping the same item still blocks the sale. Releasing an item you never kept does nothing. Raises an error when no save is loaded.

Check whether the player has unlocked an armor or helm look.

Signature: sf2.profile.has_look(item)

Parameters

Parameter Type Required
item string|ItemHandle Required

Returns: true when the look is unlocked (or the player owns the item), false otherwise.

When: After the profile has loaded, in or out of a fight.

local crowMask = sf2.items.get(sf2.mod.id .. ":items/helm/crow_mask")
if not sf2.profile.has_look(crowMask) then
sf2.log.info("The Crow Mask is still locked")
end

item is an armor or helm item handle or ID. The bare body and head are always unlocked. Raises an error when no profile is loaded.

Unlock an armor or helm look for the player.

Signature: sf2.profile.unlock_look(item)

Parameters

Parameter Type Required
item string|ItemHandle Required

Returns: true when the look was locked and is now unlocked; false when it was already unlocked, or no profile is loaded.

When: After the profile has loaded, for example in a story event handler.

local kenjiForm = sf2.items.get("core:items/armor/body_kenji")
sf2.story.on("fight_end", function(event)
if event.fight == sf2.mod.id .. ":fights/kenji" and event.result == "win" then
sf2.profile.unlock_look(kenjiForm)
end
end)

item is an armor or helm item handle or ID: a core item, one of your own, or a look from sf2.looks.register. The player can then wear it from the Armory’s Looks mode. Unlocking a look gives no item and changes no stats.

A look stays unlocked for the rest of the save. The player’s own gear unlocks its look without any call: owning an armor or helm is enough, and selling it later keeps the look.

Add a dojo-menu button that lets the player change their portrait.

Signature: sf2.profile.avatar_picker { id = "...", button = sprite?, button_pressed = sprite?, title = key?, unlock_fight = fight?, choices = { { avatar = "...", name = key, rename = bool? }, ... } }

Parameters

Parameter Type Required
definition AvatarPickerDefinition Required

Fields of AvatarPickerDefinition

Field Type Required Meaning
id string Required 1-64 lowercase letters, digits, _ or -; the button is named <mod-id>.<id>.
button string|SpriteHandle Optional The dojo-menu button image; default: the player’s current portrait.
button_pressed string|SpriteHandle Optional Shown while the button is held; needs button.
title string|LocalizationHandle Optional Heading; defaults to CHOOSE YOUR AVATAR.
unlock_fight FightHandle|string Optional The button stays hidden until the player has won this fight.
choices AvatarPickerChoice[] Required 1-64 portraits in display order.

Returns: The button’s qualified name, <mod-id>.<id>.

When: During loading.

sf2.profile.avatar_picker {
id = "avatar_changer",
title = sf2.text.get("avatar_picker.title"),
-- Hidden until the player has beaten Titan in the story.
unlock_fight = "core:fights/zone_7/c3_boss_titan/6",
choices = {
{ avatar = "avatar_hero", name = sf2.text.get("avatar_name.shadow") },
-- Picking this one also calls the player "Emerald" in fights.
{ avatar = "man_titans_army_1", name = sf2.text.get("avatar_name.emerald"), rename = true },
},
}

The portrait (avatar) is the round picture that stands for the player on the fight HUD, in dialogues and on the profile. The button appears in the dojo side menu, like sf2.ui.dojo_button, and opens a full-screen chooser drawn the same way as sf2.world.dojo_picker; no Lua runs while it is open. Picking a portrait saves it to the profile at once, and the fight HUD shows it from the next fight.

  • avatar names a built-in portrait, the name a fighter’s avatar field takes, such as avatar_hero or man_titans_army_1. The warriors catalog shows the portrait of every core fighter, and the editor completes the names. Choices whose portrait is not installed are left out.
  • name is shown under the large portrait. With rename = true (default false), picking the choice also gives the player that name on the fight HUD and the versus screen. Picking a choice without rename gives the player their own name back.
  • button is optional. Without it the button shows the player’s current portrait, which changes as they pick; give your own button art to match the dojo menu. button_pressed needs button.
  • unlock_fight is optional. The button stays hidden until the profile has won that fight, as sf2.profile.fight counts it. Without it the button is always there.
  • A mod may declare one avatar picker. Its button counts toward the four dojo buttons a mod may have, and it sends no dojo_button story event.