Perks and enchantments
A perk is an ability: a player learns it, an opponent carries it, or an item grants it. An enchantment is an effect on one piece of equipment, added at the forge. Both are powered by a behaviour: a table of Lua functions that the game calls when something happens, such as a hit or the start of a round. Because the behaviour is separate from the perk, one behaviour can power many perks and enchantments with different settings.
By the end of this guide you will have a working “opening focus” ability that gives a fighter magic charge at the start of a fight, and you will have attached it as a perk, an enchantment and a weapon power.
Write a behaviour
Section titled “Write a behaviour”A behaviour is a plain table. You do not register it by itself: write it where it is used, and the game registers it for you.
local opening_charge = { parameters = { amount = 0.25 }, on_fight_begin = function(parameters, fighter, event) fighter:add_magic_charge(parameters.amount) end,}parameterslists the settings each perk can change. Every setting is written as its default value, and the type follows from it. Hereamountis a number that defaults to0.25.on_fight_beginis a callback. It receives the settings, the fighter the perk belongs to, and an event describing what happened. The full list of callbacks and what each event contains is in Combat scripting and the callbacks reference.fighter:add_magic_chargeis one of the fighter’s methods. Note the colon.
Settings can use a longer form when you need more control:
parameters = { amount = 0.35, -- number, default 0.35 hits = { type = "integer" }, -- required, no default label = { type = "string", required = false }, -- may be left out}A plain whole number such as 1 becomes an integer. If a setting starts at 1
but can later hold 0.5, use { type = "number", default = 1 }. A setting with
no default must be supplied by every perk that uses the behaviour. Missing
settings, unknown names and wrong types are rejected.
Remember things between callbacks
Section titled “Remember things between callbacks”If a behaviour must remember something, give it state. This also changes the
first argument of every callback from parameters to self:
local ward = { parameters = { multiplier = { type = "number" } }, state = { lifetime = "round", fields = { ready = true } }, on_damage_resolving = function(self, fighter, event) if self.state.ready and event.damage > 0 then fighter:scale_incoming_damage(self.params.multiplier) self.state.ready = false end end,}This ward halves the first hit each round (for a perk that supplies
multiplier = 0.5). self.params holds the settings and self.state the values
it remembers.
state field |
Default | Meaning |
|---|---|---|
fields |
none | Named values, written like parameters. Required fields need a default. |
lifetime |
"fight" |
"round" resets every round, "fight" lasts until the fight ends, "saved" is stored in the player’s save. |
version, migrations |
1, none |
For "saved" state that changes shape between releases. See Saving and options. |
Saved state follows a player’s own equipped enchantment or learned perk.
Opponents, fight rules and actors can use round and fight state, but not saved
state. Do not keep self.state in a variable to change later: each callback
works on its own copy, and a callback that fails does not change the state.
Register a perk
Section titled “Register a perk”sf2.perks.register creates the
perk. Names and descriptions are text handles, so first add the words to
localizations/eng.toml:
perk.opening_focus = "Opening Focus"perk.opening_focus.description = "Start each fight with extra magic charge."local sf2 = require("sf2")
local opening_charge = { parameters = { amount = 0.25 }, on_fight_begin = function(parameters, fighter, event) fighter:add_magic_charge(parameters.amount) end,}
local focus = sf2.perks.register { id = "opening_focus", display_name = sf2.text.get("perk.opening_focus"), description = sf2.text.get("perk.opening_focus.description"), behavior = opening_charge, parameters = { amount = 0.35 },}| Field | Required? | Meaning |
|---|---|---|
id |
Yes | A local ID, such as opening_focus. |
display_name, description |
Yes (unless a template gives them) |
Text handles. |
behavior |
Yes (unless a template gives it) |
The behaviour table, or a handle. |
parameters |
No | Values for the behaviour’s settings. Unlisted settings keep their defaults. |
icon |
No | A sprite handle. |
kind |
No (default "single") |
"single", or "combo" for a combo perk. |
template |
No | An existing scripted perk of your mod to copy from. Your parameters are merged over the copy’s. |
upgrades, initial_upgrade |
No | Ranks of the perk; see below. |
The same behaviour table can power several perks. Pass the same table again and it is reused, so you can offer a weaker and a stronger version:
local weak = sf2.perks.register { id = "weak_focus", behavior = opening_charge, -- amount stays 0.25 display_name = sf2.text.get("perk.weak"), description = sf2.text.get("perk.weak.description"),}local stronger = sf2.perks.register { id = "opening_focus_plus", template = focus, -- copy of the perk above parameters = { amount = 0.6 },}Registering a perk does not teach it to anyone. You still have to put it somewhere a fighter will have it (see Attach it).
Use sf2.perks.get to refer to a perk
that already exists, such as core:perks/perk_cobra.
Ranks (upgrades)
Section titled “Ranks (upgrades)”A learned perk can have several ranks. upgrades is a list of 1 to 100 tables,
{ level, description?, parameters? }, with levels counting up from 1 (level 0 is
the base perk):
upgrades = { { level = 1, parameters = { amount = 0.4 } }, { level = 2, parameters = { amount = 0.5 } },}Each upgrade overrides the base perk’s settings on its own, not the previous
rank’s. A description you give must be a text handle from your own mod. Upgrades
do not change XP, prices or the progression curve. initial_upgrade picks which
rank a newly registered perk starts at. Ranks apply to perks a player learned;
equipment and opponents use the base settings.
Register an enchantment
Section titled “Register an enchantment”An enchantment is a perk that players get from the forge. Add an
enchantment table to turn the registration into one:
local charged_weapon = sf2.perks.register { id = "charged_weapon", display_name = sf2.text.get("perk.opening_focus"), description = sf2.text.get("perk.opening_focus.description"), behavior = opening_charge, parameters = { amount = 0.35 }, enchantment = { recipe = "simple", item_types = { "weapon" } },}enchantment field |
Required? | Meaning |
|---|---|---|
recipe |
Yes | "simple", "medium" or "complex", which sets the cost and the rolled power. |
item_types |
Yes | A list of "weapon", "armor", "helm", "ranged" and "magic". No repeats. |
perk |
No | A scripted perk of your mod to offer as the enchantment. It supplies the behaviour and text, so leave out behavior when you use it. |
For example, offer the perk from earlier as an enchantment too:
sf2.perks.register { id = "focus_enchantment", enchantment = { recipe = "medium", item_types = { "weapon", "helm" }, perk = focus },}The player still has to forge it and equip the item. Making it available from the forge is covered in Forge and economy.
Attach it
Section titled “Attach it”A registered perk needs somewhere to live:
- On an item. Give a weapon a permanent perk with the
perkfield when you register it, or with theperkslist ofsf2.items.patch. See Weapons. - On an opponent. List it in the opponent’s
perksin a battle. - On a fight. A fight rule such as
{ kind = "perk", perk = focus, target = "player" }gives it to a fighter for that fight only. - In the perk tree.
sf2.perks.patchwith abranchreplaces the choices offered at one level (1 to 52):
sf2.perks.patch { branch = { level = 2, entries = { { perk = focus, action = "unlock" } }, -- or "upgrade" for a perk with ranks },}A branch does not teach the perk by itself or change XP or prices. Two mods replacing the same level conflict.
Player callbacks only run for learned, active perks after the game’s usual filtering, so test with a real profile. A perk that is both learned and found on equipment runs once per event.
Scale with the forge
Section titled “Scale with the forge”An enchantment made at the forge rolls a strength: the tier the player picked, or where a random roll landed in its range. A behavior with a state table can read it as self.strength, from 0 (weakest) to 1 (strongest):
on_damage_dealing = function(self, fighter, event) fighter:scale_outgoing_damage(1 + 0.5 * self.strength)endself.strength is 0.5 when the game has no strength on record, such as for an enchantment given by a reward, a fixed-power forge pick, or a perk that did not come from the forge. If the same enchantment is on several equipped pieces, the highest strength is used. Behaviors without state do not get a self, so give them a state table when they need this.
Show a recharge icon
Section titled “Show a recharge icon”If a perk’s ability recharges, show it on the fight HUD with a
recharge_indicator. It displays one of two icons: a ready icon while no listed
flag is active, and a cooldown icon (with a sweep timer) while one is.
sf2.perks.patch { perk = focus, recharge_indicator = { flags = { sf2.mod.id .. ":behaviors/opening_focus:cooldown" }, ready = sf2.assets.sprite("sprites/focus_ready"), cooldown = sf2.assets.sprite("sprites/focus_cooldown"), },}A flag is a named marker on a fighter. Set one from a behaviour with
fighter:set_flag and give it a
duration so the sweep follows it. A flag set by your own behaviour is named
"<mod id>:behaviors/<perk id>:<key>"; a game perk’s flag keeps its own name,
such as "HermitStormRecharge". The indicator is only a display. It never blocks
a move, so gate the ability itself on the same flag. The icon is drawn at its
natural size, so set pixels_per_unit in the sprite file to size it. Only one
indicator can exist per perk.
Show a perk’s charge in a meter
Section titled “Show a perk’s charge in a meter”Some enchantments build toward something: a count of hits, a charge that fills
with damage, or an effect that runs for a while. Add a meter to show that state
in the fight meter at the foot of the screen. The meter reads the perk’s own flags
and variables, so a game perk that keeps a count in its XML needs no Lua behavior:
sf2.perks.patch { perk = sf2.perks.get("core:perks/PERK_GUST_SET_WIND_MAKER"), meter = { phases = { -- After a launch, the cooldown fills back up. { flag = "GustEffectCooldown", show = "passed", color = "#4E7A6A" }, -- Otherwise the gust is ready. { color = "#9FE3C8" }, }, },}List the meter’s phases in order of priority; the first that applies is shown.
A phase can follow a flag’s timer, a perk variable against a maximum, or show a
full bar. Split the meter into cells with segments to count hits or stacks. See
sf2.perks.patch for every field.
Hide a perk’s icons on the opponent
Section titled “Hide a perk’s icons on the opponent”Some icons give away what the opponent is doing, such as the cloak icon of an
invisible fighter. Add opponent_icons = false to hide a perk’s HUD icons on the
opponent’s side only. This covers both the game’s own icons for that perk and its
recharge indicator. The player’s side keeps them, and the perk works as before:
sf2.perks.patch { perk = sf2.perks.get("core:perks/PERK_INVISIBILITY"), opponent_icons = false,}You can give it in the same sf2.perks.patch call as a recharge_indicator.
See sf2.perks.patch for the details.
Stop moves while a perk’s effect lasts
Section titled “Stop moves while a perk’s effect lasts”Some effects should also keep the fighter they land on from fighting back in
certain ways. forbid_moves names a flag and the kinds of move it forbids: while
that flag is on a fighter, it cannot start a ranged throw, a magic cast or a boss
ability, whichever you list. It holds for the player and for computer opponents
alike, and moves already under way finish:
-- Time Shift's slow: the slowed fighter also throws, casts and uses abilities no more.sf2.perks.patch { perk = sf2.perks.get("core:perks/PERK_TIME_SHIFT_SET_TIME_SHIFTER"), forbid_moves = { flag = "IsSlowedDown", moves = { "ranged", "magic", "ability" } },}A game perk’s flag keeps the name its XML gives it; Time Shift puts
IsSlowedDown on the fighter it slows. See
sf2.perks.patch for which moves count
as each kind.
Limits
Section titled “Limits”- A perk’s callbacks only run if they are attached to something active.
- Temporary attribute changes made by a game perk remove exactly the amount they added, even if the underlying value has since changed.
- Ranks do not reach equipment or opponent copies of a perk.
- Perks and enchantments do not set their own forge prices. The forge’s shared costs apply.
Next, see Combat scripting for the callbacks and fighter methods you can use inside a behaviour.