Opponent AI
Every computer opponent follows a tactic: the rules it uses to pick its next move. The base game ships many tactics (the standard one is called "Standard"). You can build your own in two ways:
| Approach | Good for |
|---|---|
| Tune a built-in tactic with scoring tables | Making an opponent a bit more aggressive, defensive or fond of one of your moves |
Write on_decide in Lua |
Reactions the tables cannot express: “back off when the player winds up an attack”, “wait two seconds between combos”, “change style at half health” |
Both go through sf2.ai.register, and both fall back to a native tactic when you let them, so a mistake never leaves an opponent frozen. New to mods? Start with How mods work.
Register a tactic and give it to an opponent
Section titled “Register a tactic and give it to an opponent”sf2.ai.register returns a tactic handle. Hand that handle to an opponent’s tactic field. Opponents are written inline inside a battle, see Story battles.
local sf2 = require("sf2")
-- A patient version of the standard tactic that remembers recent strikes.local patient = sf2.ai.register { id = "patient", template = "Standard", -- inherit everything from the native Standard tactic memory = { strikes = 2, round_factor = 0.25 },}
-- Inside a fight of sf2.battles.register { ... fights = { { opponents = ... } } }:local opponent = { fighter = "core:warrior-templates/default", tactic = patient }Register the tactic before the battle that uses it. template names the native tactic to inherit. Leave it out and your tactic starts empty, which is only a good idea if you supply every list yourself.
Tune the scoring tables
Section titled “Tune the scoring tables”A tabular tactic scores each possible response and picks among the best. You can replace parts of the template’s tables:
- Value tables (
counter_attack,dodge,block,safe_attack,table_attack,cautious_movement,dodge_missiles,dodge_magic) hold numbers such asbase,health_factoranddistance_factor. They are native scoring factors, not percentages. - Weighted lists (
animation_weights,quick_attacks,evades,expected_wait) list moves the tactic may choose. A row names a move handle (move) or an existing animation (animation) plus avaluetable.
local sf2 = require("sf2")
-- uppercut is a move handle returned by sf2.moves.register (see the moves guide).local brawler = sf2.ai.register { id = "brawler", template = "Standard", quick_attacks = { { move = uppercut, value = { base = 5 } } },}A list you supply replaces the inherited list. If you replace the attack list with one unsuitable move, the opponent may have nothing sensible to do. Keep a known-good template, change one thing, and test in a fight.
Help the AI use a new move
Section titled “Help the AI use a new move”The built-in tables have no row for a move your mod adds. Set tactic_equivalent on the move to the name of a native move with similar timing and range, for example "FrontKick" for a quick grounded kick. Wherever a table offers FrontKick as a response, it also offers your move. You can also set tactic_distance (or tactic_when) on the move so the AI only considers it at the right range. See Edit and create moves.
Decide with Lua
Section titled “Decide with Lua”Add on_decide to the tactic. The game calls it whenever the opponent could pick a move, at most ten times a second. Keep template = "Standard" so the game can decide when you do not.
on_decide = function(memory, event) ... endmemoryis a table that belongs to this one opponent and this tactic. Store cooldowns and counters there. Variables at the top of your script are shared by every fighter that uses the tactic, so do not keep per-fighter state in them. Memory is not saved.eventdescribes the moment:event.selfandevent.opponent(health, position, current animation),event.frame,event.seconds, andevent.actions, the list of moves that are legal right now.- Return one of the entries from
event.actionsto play it,"wait"to do nothing until the next decision, ornilto let the native tactic choose. You cannot invent an action, only pick from the list.
Each entry in event.actions has a name, a type ("move", "attack" or "none"), a priority, the inputs that start it ({ control = "Kick", press = "tap" }) and timing (how long the clip nominally runs). The full field list is in on_decide.
Example: wait between attacks
Section titled “Example: wait between attacks”local sf2 = require("sf2")
local patient = sf2.ai.register { id = "patient_attacker", template = "Standard", on_decide = function(memory, event) memory.next_attack = memory.next_attack or 0 if event.seconds < memory.next_attack then return "wait" end
local best for _, action in ipairs(event.actions) do if action.type == "attack" and (not best or action.priority > best.priority) then best = action end end if not best then return nil end -- nothing to attack with: native AI decides
memory.next_attack = event.seconds + 1 -- at least one second until the next attack return best end,}Example: pick a move by what it does, not what it is called
Section titled “Example: pick a move by what it does, not what it is called”Matching move names breaks when another mod renames a move. Matching the inputs and timing keeps working. This tactic chooses the shortest kick tap that does not loop:
local sf2 = require("sf2")
local kicker = sf2.ai.register { id = "quick_kicker", template = "Standard", on_decide = function(memory, event) local best for _, action in ipairs(event.actions) do local timing = action.timing if timing and not timing.looped then for _, input in ipairs(action.inputs) do if input.control == "Kick" and input.press == "tap" then if not best or timing.nominal_frames < best.timing.nominal_frames then best = action end break end end end end return best -- nil lets the native tactic decide when no kick is available end,}timing.nominal_frames is the clip’s length at normal speed, not a promise of how long recovery takes: interruptions, looping and slow motion all change it.
Example: react to what the opponent is doing
Section titled “Example: react to what the opponent is doing”event.opponent.animation lists the live windows of the move the other fighter is playing. That lets the AI back away from an attack without knowing its name:
local sf2 = require("sf2")
local cautious = sf2.ai.register { id = "evasive", template = "Standard", on_decide = function(memory, event) local animation = event.opponent and event.opponent.animation if not animation then return nil end
local attacking = false for _, interval in ipairs(animation.intervals) do if interval.type == "attack" then attacking = true; break end end if not attacking then return nil end
for _, action in ipairs(event.actions) do if action.type == "move" then for _, input in ipairs(action.inputs) do if input.control == "Back" then return action end end end end return nil end,}This is a policy, not a guarantee. The game still applies its own rules about when a move may be interrupted.
Keep your AI safe
Section titled “Keep your AI safe”- A decision that errors, returns an action that is no longer legal, or runs too long (200,000 instructions) switches your callback off for that opponent and the native tactic takes over. The first failure is logged. Other opponents keep their own AI.
- Only the standard tabular controller calls
on_decide. - Always check that
event.opponentandevent.opponent.animationexist before reading them. event.back_wall_distancetells you how far the wall is behind the fighter, so an AI can make room for a wall attack.- An opponent that has just bounced off a wall (its wall-hit reaction) may get away by itself before you are asked: once the bounce lets it act, with the player within reach, the game usually starts a wall jump (or another wall escape it can play there) and
on_decideis not called that time. This keeps the player from comboing it against the wall forever, whatever its tactic. - To make an opponent stand still for a while (a pause between a boss’s stages, say) without writing a tactic, a behavior on that opponent can call
fighter:set_ai_held. It holds any tactic, the game’s own or yours.
Companion AI
Section titled “Companion AI”Independent fighters (actors) use tactics too. An observation of an actor includes event.self.actor = { id, definition, owner, team }; a main fighter has no actor field, so test for it before reading team. The Combat scripting guide shows how to summon actors and give each its own memory.
Next steps
Section titled “Next steps”- Edit and create moves adds the moves your tactic can choose.
- Combat scripting changes a fight while it is running.
- Reference:
sf2.ai.register,sf2.ai.name,on_decide.