Skip to content

Combat scripting

Combat scripting lets your mod change a fight while it is being fought: halve the first hit you take each round, heal on a critical hit, dash, summon a companion, throw a projectile, draw a warning zone on the floor or zoom the camera. It is built from three pieces:

  • A behavior is a plain Lua table holding your callbacks. You write it where you use it.
  • A callback is a function the game calls at a moment such as “a hit is about to land” or “the round started”. The callbacks are listed in the callback reference.
  • Fighter methods are what a callback uses to change something (fighter:change_health(0.1)). They are listed in the fighter reference.

New to Lua or to how mods are laid out? Read Lua basics and How mods work first.

This behavior halves the first hit the fighter takes in each round:

local sf2 = require("sf2")
local ward = {
parameters = { multiplier = 0.5 }, -- a setting every attachment can change
state = { lifetime = "round", fields = { ready = true } }, -- private memory, reset each round
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,
}
sf2.text.register { id = "perk.ward.name", language = "eng", value = "Opening Ward" }
sf2.text.register { id = "perk.ward.text", language = "eng", value = "Halves the first hit you take each round." }
sf2.perks.register {
id = "opening_ward",
display_name = sf2.text.get("perk.ward.name"),
description = sf2.text.get("perk.ward.text"),
behavior = ward,
parameters = { multiplier = 0.4 },
}

How it works:

  • parameters are values each attachment can override. Write a starting value and the type follows from it (0.5 is a number, true a boolean).
  • state is memory for each active effect. Once you declare it, callbacks receive self (with self.params and self.state) instead of parameters. lifetime is "round", "fight" or "saved".
  • A callback receives (self_or_parameters, fighter, event). fighter is the fighter the behavior is attached to. event describes what happened.
  • A plain table is registered for you the first time you use it. Pass the same table to another perk, rule or actor and it is shared.
Attached to How See
A perk or enchantment behavior = ... in sf2.perks.register Perks and enchantments
A weapon or other item perk = { behavior = ... } in the item Weapons
A fight A rule: { kind = "behavior", behavior = ..., target = "player" } Battle rules
A summoned fighter behavior = ... in sf2.actors.register Actors below
  • Keep callbacks short. The fighter value and its methods stop working the moment the callback returns. Keep numbers and strings for later, never the fighter.
  • The event is a copy. Changing it changes nothing. Use fighter methods to change the game.
  • Errors are logged, not fatal. The game carries on, but anything your callback already did stays done.
  • Callbacks do not run in the dojo punchbag, title-screen sparring, local versus or online versus.
  • Order. Fight rules run first, then the player’s perks and enchantments, then the opponent’s.
I want to… Callback
Set something up or reset it on_fight_begin, on_round_begin
Run every frame (timers, cooldowns) on_tick (60 times a second)
Make my hits stronger or weaker on_damage_dealing, on_post_hit
Reduce the hits I take on_damage_resolving
Make a hit unblockable on_hit_pre_crit
Count or react to damage already done on_damage_dealt, on_damage_received
React to blocks and critical hits on_block, on_critical
Know when a move started or ended on_animation_start, on_animation_end
Follow combos and the style meter on_combo_changed, on_style_changed
Know which direction the player picked on a button on_control_direction
Finish up on_round_end, on_fight_end

Hit callbacks can carry event.attack, a copy describing what made contact: kind ("fighter", "actor", "projectile" or "native_child"), the model_name, the animation_name and the contact point. Always check it for nil. In the hit events, damage, health_before, health_after, blocked and critical describe the victim, even in on_damage_dealt. Damage is measured in health units where 1 is a normal fighter’s full health.

Fighter methods are grouped by what they do. Each name links to its reference entry.

Group Methods
Look fighter:snapshot returns copied health, position, animation and the fight clock for both fighters
Damage scale_outgoing_damage, add_outgoing_damage, scale_incoming_damage, add_damage_shield, set_hit_blocked
Health and charge change_health, add_magic_charge, and the same on fighter.opponent
Movement and moves move_by, play_move, change_form
Controls and HUD set_control_blocked, set_button_cooldown, show_status_icon
Looks set_model_color, set_cloak
Flags set_flag switches a named label that move conditions can test
Round end_round asks for a win or loss for the player; set_phase moves the fight to another phase; set_reward_bonus raises the win’s rewards. All three are for the fight’s round controller

Methods that change things return nothing on success. Methods that can be refused, such as end_round, return false, reason. A mistake in your script (wrong type, called at the wrong time) is a Lua error.

local sf2 = require("sf2")
local vampire = {
parameters = { heal = 0.05 },
on_critical = function(parameters, fighter, event)
fighter:change_health(parameters.heal)
end,
}

fighter:move_by(x, y) shifts the fighter by a distance in arena units (positive x is right, positive y is down). It is not a velocity, and it does not create an attack or invulnerability, so use modest distances and test near walls.

local sf2 = require("sf2")
local dash = {
state = { lifetime = "round", fields = { cooldown = 0 } },
on_tick = function(self, fighter, event)
self.state.cooldown = math.max(0, self.state.cooldown - event.delta_frames)
if self.state.cooldown > 0 then return end
local view = fighter:snapshot()
if not view or not view.round_active or not view.opponent then return end
-- Dash away from the opponent when they get within 120 units.
local gap = view.opponent.position.x - view.self.position.x
if math.abs(gap) < 120 then
local away = gap > 0 and -1 or 1
local accepted, reason = fighter:move_by(away * 150, 0)
if accepted then
self.state.cooldown = 180
elseif reason then
sf2.log.warn(reason)
end
end
end,
}

The game adds up the offsets requested in a step and applies one translation at the end of it. A request is not a completion receipt: if the fighter dies or the round ends first, it is dropped.

Example: make a move start from an ability

Section titled “Example: make a move start from an ability”

fighter:play_move plays one of your registered moves on demand and returns a receipt whose status is "queued", "applied" or "failed". "applied" means the animation started, not that it hit. You may keep the receipt between ticks; do not save it. The full pattern (a registered move plus a cooldown) is on the moves guide.

These change the hit in progress, so each one is only available in the callback named here:

The fighter reference also covers checks you can make from any callback (animation_is, debuff_count, has_perk), round-long changes (set_attribute_bonus, set_perk_disabled), a charge or stack meter at the foot of the screen (set_meter), direction picks on a button (set_control_directions, with on_control_direction), and a way to hear about the hits your actors land (forward_actor_hits).

A projectile is a native child object such as a thrown dart. Define it once with sf2.projectiles.register, spawn it from a callback with fighter:spawn_projectile, and optionally steer it every tick. Register the flight move first (see Edit and create moves); then:

local sf2 = require("sf2")
-- flight is a move handle from sf2.moves.register with a compatible animation.
local dart = sf2.projectiles.register {
id = "dart",
name = sf2.mod.id .. ".dart", -- prefix with your mod ID
core_skeleton = "SkeletonMissile",
item = sf2.items.get("core:items/ranged/RANGED_C2_Z2_MONK_SHURIKEN"),
start_move = flight,
lifetime_frames = 120, -- 1-600, default 180
}
local thrower = {
state = { lifetime = "round", fields = { cooldown = 0 } },
on_tick = function(self, fighter, event)
self.state.cooldown = math.max(0, self.state.cooldown - event.delta_frames)
if self.state.cooldown > 0 then return end
local receipt = fighter:spawn_projectile(dart, 60, 0) -- offset from the fighter
if receipt.status == "failed" then sf2.log.warn(receipt.error) end
self.state.cooldown = 90
end,
}

Contact and damage come from the attack in the flight move; Lua only decides when and where. To guide a live projectile, ask for it again each tick with fighter:projectiles(), then use projectile:snapshot, projectile:move_by and projectile:remove. Hold on to IDs, never to projectile references.

To count hits from your projectiles, test event.attack in a damage callback:

-- A field inside a behavior table:
on_damage_dealt = function(self, _, event)
local attack = event.attack
if attack and attack.kind == "projectile" and attack.projectile_owner == sf2.mod.id then
self.state.hits = self.state.hits + 1 -- declare an integer state field "hits"
end
end,

Limit: a mod may have 16 live children (64 per fight). A rejected spawn returns a failed receipt.

An actor is an independent fighter your mod adds to a running fight, with its own health, equipment, team and AI. Register a fighter to be the body, then register the actor with sf2.actors.register and spawn it with fighter:spawn_actor:

local sf2 = require("sf2")
local helper = sf2.fighters.register {
id = "helper",
fighter = "core:warrior-templates/default",
level = 1, tactic = "Standard", skeleton = "Skeleton",
items = { sf2.items.get("core:items/weapon/WEAPON_KNIVES") },
}
local ally = sf2.actors.register {
id = "ally",
character = helper,
team = "owner", -- or "opponent"
ai = true, -- false leaves it idle so your Lua steers it
lifetime_frames = 900, -- 1-36000, default 1800
max_health = 1, -- 1 is a full normal fighter
}
local summoner = {
state = { lifetime = "round", fields = { summoned = false } },
on_tick = function(self, fighter)
if self.state.summoned then return end
self.state.summoned = true
local receipt = fighter:spawn_actor(ally, 180, 0)
if receipt.status == "failed" then sf2.log.warn(receipt.error) end
end,
}

Spawning needs a living main fighter in an offline round. You can have 4 actors per mod and 8 per fight; a rejected spawn comes back as a failed receipt. Get live actors again each callback with fighter:actors(), then use actor:snapshot, actor:set_target, actor:move_by, actor:change_health, actor:play_move or actor:remove.

Actors think and react separately:

  • Decisions come from the fighter’s tactic. Give it one from Opponent AI; an on_decide tactic keeps separate memory for each actor.
  • Reactions come from an optional behavior on the actor. Each spawned actor gets its own copy of the state (round or fight lifetime only). It receives on_actor_spawn, on_actor_end, on_tick, the hit, damage, block and critical callbacks and the animation callbacks. It does not get the round or combo callbacks. Inside it fighter.source is "actor" and fighter.actor gives callback-scoped commands.

When a round is decided, all actors are retired first and the round finishes with just the two main fighters. The camera never follows an actor automatically.

Use these for hazards, capture zones and warning art. A sensor asks whether a fighter touches a rectangle. A marker draws a coloured rectangle or sprite on the arena. They are independent, so your Lua decides what happens when someone enters the zone and moves the marker yourself.

A rectangle is { x, y, width, height } in arena coordinates (the same ones fighter:snapshot() returns): x and y are the minimum corner, positive y is down, and width and height are positive and at most 4000.

local sf2 = require("sf2")
local zone = { x = -160, y = -420, width = 320, height = 440 }
local marker -- a Lua variable, not saved state
local hazard = {
state = { lifetime = "round", fields = { timer = 0 } },
on_tick = function(self, fighter, event)
if not marker then
marker = fighter:mark_rect(zone, "#ff332266") -- translucent red; nil if refused
end
self.state.timer = self.state.timer + event.delta_frames
if self.state.timer % 60 == 0 and fighter:overlaps_rect(zone) then
fighter:change_health(-0.02) -- direct loss: no block, armor or reaction
end
end,
on_round_end = function()
if marker then sf2.world.remove_marker(marker); marker = nil end
end,
}

fighter:overlaps_rect tests the fighter’s collision shape only. It ignores attack windows and invulnerability, and touching the edge counts. fighter:mark_rect and fighter:mark_sprite create markers during an active callback. Change one with sf2.world.try_set_marker (move, resize, swap sprite or tint), check it with is_marker_active and remove it with remove_marker. A script may have 16 markers and the game 64; markers vanish at round end. Direct health loss skips the normal hit calculation, so decide whether that is what you want.

A marker can also ride on a fighter’s body, for charges, burns or curse marks. Add a node (a body point such as "chest") and of = "enemy" or "self" to the rectangle; its x and y are then measured from that node and the marker follows it through every move, fall and jump:

-- spark is a sprite from sf2.assets.sprite, loaded while the mod loads.
local mark = fighter:mark_sprite(spark, {
node = "stomach", of = "enemy", x = -10, y = -10, width = 20, height = 20,
})

For a cinematic moment you can take over the fight camera. You choose where it centres (center_x), its vertical pan (offset_y, positive raises the view) and its zoom (0.25 to 4). Only the drawing changes. Collisions, damage and the HUD behave as usual.

local sf2 = require("sf2")
local camera
local closeup = {
on_tick = function(_, fighter)
if camera and sf2.world.is_camera_active(camera) then return end
local view = fighter:snapshot()
if not view then return end
local failure
camera, failure = fighter:acquire_camera {
center_x = view.self.position.x, offset_y = -40, zoom = 1.3,
}
if failure then sf2.log.warn(failure) end
end,
on_round_end = function()
if camera then sf2.world.release_camera(camera); camera = nil end
end,
}

fighter:acquire_camera fails when someone else owns the camera, so check the result. Only one script owns the camera at a time and no one is ever pushed off it. Change the view with sf2.world.try_set_camera; each call replaces every earlier setting, and calling it without settings returns to automatic framing. Release it with sf2.world.release_camera. Ownership also ends when the round or fight ends. The viewport and arena-edge limits still apply, so a request may be clamped.

For effects that zoom or shake the screen on a hit, a screen grade in the visual effects cookbook is often simpler than owning the camera.

  • Fighter methods work only inside a callback of an active offline round. Training, title sparring and local versus are excluded; offline mod raids work.
  • Some methods are available in only one callback (scale_incoming_damage only in on_damage_resolving, set_hit_blocked only in on_hit_pre_crit). The reference entry for each states when it works.
  • Fighter, actor and projectile references expire when the callback returns. Keep IDs and numbers instead.
  • Test with pause, surrender, both facing directions and the end of a round, not only the happy path.