Skip to content

sf2.world

Stages and places: register locations, tag stages, choose the dojo, change scenes, and take control of the camera and arena markers. Get sf2 with local sf2 = require("sf2").

Create a new location (a stage) from layers, images and optional tags.

Signature: sf2.world.register(definition)

Parameters

Parameter Type Required
definition LocationDefinition Required

Fields of LocationDefinition

Field Type Required Meaning
id string Required —
color string Optional Backdrop colour as “#rrggbb”. Default “#000000”.
wall number Optional Wall distance. Default 200.
floor number Optional Floor height. Default 80.
width number Optional Arena width. Default 1936.
height number Optional Arena height. Default 512.
min_width number Optional Narrowest the arena may be shown. Default: width.
position_y number Optional —
friction_force number Optional —
grid_size number Optional —
music AudioHandle Optional —
music_choices AudioHandle[] Optional —
dojo boolean Optional —
layers LocationLayer[] Required Layers from back to front; order is kept.
tags ("day"|"dusk"|"night"|"dark"|"indoor"|"fire"|"lamps"|"otherworld"|"rain"|"snow"|"mist"|"heat"|"foliage"|"petals"|"water"|"raid")[] Optional Stage tags for this stage. Up to 16.

Returns: A location handle.

When: During loading.

local arena = sf2.world.register {
id = "training_arena",
color = "#1b2230",
layers = {
{ images = { {
sprite = sf2.assets.sprite("core:textures/locations/battlefield/battlefield_bg1.back_1"),
width = 1936, height = 1024,
} } },
{ fighters = { player_x = 868, enemy_x = 1068 } },
},
}

layers run back to front; the one layer with a fighters table is where fighters stand, so earlier layers are behind them and later ones in front. Defaults suit the example; set sizes and walls for your art and check framing in game. For a stage players can pick as their dojo, set dojo = true and use sf2.world.select_dojo. See menus and settings for curves and tags.

Get a location’s qualified ID as a string.

Signature: sf2.world.name(location)

Parameters

Parameter Type Required
location LocationHandle Required

Returns: The qualified ID string.

When: During loading.

local arena_name = sf2.world.name(arena) -- for a fight's location field

A plain string is an error.

Give a stage tags such as “night” or “fire” so effects can target it.

Signature: sf2.world.tag(stage, tags)

Parameters

Parameter Type Required
stage LocationHandle|string,A game stage name such as "dock", or a location handle from world.register. Required
tags ("day"|"dusk"|"night"|"dark"|"indoor"|"fire"|"lamps"|"otherworld"|"rain"|"snow"|"mist"|"heat"|"foliage"|"petals"|"water"|"raid")[],1-16 stage tags to add. Required

Returns: Nothing.

When: During loading.

sf2.world.tag("dock", { "lamps" })
sf2.world.tag(arena, { "dusk", "fire", "heat" })

stage is a game stage name or a location handle from sf2.world.register. Tags only add to a stage’s existing ones; an unknown tag is an error. See stage tags.

Use a different location wherever the game would load one, outside versus, optionally only while a setting is on.

Signature: sf2.world.replace { location, replacement, setting?, choice? }

Parameters

Parameter Type Required
definition LocationReplacement Required

Fields of LocationReplacement

Field Type Required Meaning
location LocationHandle|string Required The location to replace: a core:locations/<name> ID or a location handle.
replacement LocationHandle|string Required The location loaded instead: a core:locations/<name> ID or a location handle.
setting SettingHandle|SettingChoiceHandle Optional A switch that turns the replacement on or off, or a choice setting together with choice. Default: always on.
choice string Optional With a choice setting: the id of the choice the replacement applies to.

Returns: nil.

When: Entrypoint. Changing the setting takes effect at the next fight or dojo visit.

local se = sf2.settings.toggle { id = "se_arena", label = "Special Edition arena" }
sf2.world.replace {
location = "core:locations/arena",
replacement = "core:locations/arena_se",
setting = se,
}

location and replacement are each a core location ID such as "core:locations/arena" (the locations catalog lists them) or a handle from sf2.world.register. Leave setting out to replace the location whenever the mod is enabled. setting is a switch from sf2.settings.toggle, or a choice from sf2.settings.choice with choice, the id of the choice the replacement applies to:

local stages = sf2.settings.choice {
id = "stages", label = "Stages",
choices = { { id = "classic", label = "Classic" }, { id = "special_edition", label = "Special Edition" } },
}
sf2.world.replace {
location = "core:locations/arena",
replacement = "core:locations/arena_se",
setting = stages, choice = "special_edition",
}

Where it applies. Every fight that is not versus: story, events, tournaments, Underworld and any other mode. It also applies to the dojo backdrop (the dojo and the shop’s preview) whenever the player’s dojo resolves to location, so replacing core:locations/dojo changes the default dojo but not a dojo the player picked. Local and online versus, training and replays keep the arena players chose.

Only the stage changes. The fight’s opponents, rules, rewards and music stay the same. The replacement’s own walls and floor apply, so a narrower or wider arena changes how much room the fighters have.

  • A replacement that is not installed is skipped, and the original location loads.
  • One mod may give the same location several replacements behind different settings or choices; the first one whose setting is on (or whose choice is picked) wins. Listing the same location and setting (and choice) twice is an error.
  • Two mods replacing the same location conflict, and the second fails to load. See Combine mods.
  • At most 128 replacements per mod.

Add a menu button that lets the player choose which dojo to use.

Signature: sf2.world.dojo_picker { id = "...", button = sprite, button_pressed = sprite, title = key, choices = { { location = ..., name = key, preview = sprite }, ... } }

Parameters

Parameter Type Required
definition DojoPickerDefinition Required

Fields of DojoPickerDefinition

Field Type Required Meaning
id string Required 1-64 lowercase letters, digits, _ or -; the dojo-menu button is named modid.id.
button string|SpriteHandle Required The dojo-menu button image.
button_pressed string|SpriteHandle Optional Shown while the dojo-menu button is held; default: the button image.
title string|LocalizationHandle Optional Heading; defaults to CHOOSE YOUR DOJO.
choices DojoPickerChoice[] Required 1-64 choices in display order.

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

When: During loading.

sf2.world.dojo_picker {
id = "dojo_picker",
button = sf2.assets.sprite("sprites/dojos/button"),
title = sf2.text.get("dojo_picker.title"),
choices = {
{ location = "core:locations/dojo_india24",
name = sf2.text.get("dojo_name.india"),
preview = sf2.assets.sprite("sprites/dojos/india") },
},
}

The button appears in the dojo side menu (like sf2.ui.dojo_button) and opens a full-screen chooser; no Lua runs while it is open. Picking saves the choice as sf2.world.select_dojo does. A mod may declare one picker, and choices whose location is not installed are left out. It sends no dojo_button story event.

Choose which dojo location the game uses for the player.

Signature: sf2.world.select_dojo(location)

Parameters

Parameter Type Required
location LocationHandle|string,A core location ID or dojo location handle. Omit to restore the player's choice. Optional

Returns: Nothing.

When: After the profile has loaded, usually from a UI callback.

sf2.world.select_dojo(arena) -- a registered dojo = true location
sf2.world.select_dojo("core:locations/dojo_india24") -- an installed core dojo
sf2.world.select_dojo() -- back to the player's own dojo

The choice is saved with the profile and never changes ordinary battle locations. The new backdrop appears on the next dojo or shop entry, not in an open scene. If the chosen mod is later disabled, the native dojo is used and the choice is kept. Non-dojo locations are errors. Read it back with sf2.world.selected_dojo.

Get the dojo the player has chosen, if any.

Signature: sf2.world.selected_dojo()

Returns: Saved qualified location ID, or nil for the default.

When: After the profile has loaded.

local using_this_arena = sf2.world.selected_dojo() == sf2.world.name(arena)

This is the saved preference, even if that location is currently unavailable.

Ask the game to open a scene, such as the map or the dojo.

Signature: sf2.world.open_scene(destination)

Parameters

Parameter Type Required
destination "map"|"shop"|"profile"|"dojo" Required

Returns: true if the request was accepted or the scene is already open; false if navigation is unavailable or a quest handled it instead.

When: After a menu scene has loaded; for custom menu buttons.

local function on_click(view, id)
if id == "shop" and sf2.world.open_scene("shop") then
sf2.ui.close(view)
end
end

true does not mean the scene has loaded; listen for scene_enter (story events). It is refused during a scene change, fight preparation or blocked input, and is an error from UI on_close. Do not retry in a loop.

Move or zoom the camera you own.

Signature: sf2.world.try_set_camera(camera, settings)

Parameters

Parameter Type Required
camera CameraHandle Required
settings CameraSettings Optional

Fields of CameraSettings

Field Type Required Meaning
center_x number Optional Camera centre X in arena units, -10000 to 10000. Default follows the duel.
offset_y number Optional Vertical pan, -1000 to 1000; positive raises the view. Default 0.
zoom number Optional Zoom scale, 0.25 to 4. Default is the normal zoom.

Returns: true when changed, or false, reason if the camera was released or expired. Invalid settings are errors.

When: After acquiring the camera.

local ok, why = sf2.world.try_set_camera(camera, { center_x = 600, offset_y = -20, zoom = 1.2 })
if not ok then sf2.log.warn(why) end
sf2.world.try_set_camera(camera) -- back to native framing

Each call replaces every earlier setting; omitted fields return to defaults. Takes effect next frame.

Give the camera back to the automatic follow or another script.

Signature: sf2.world.release_camera(camera)

Parameters

Parameter Type Required
camera CameraHandle Required

Returns: true the first time, false on later releases.

When: Cleanup: round end, fight end, UI close.

if camera then sf2.world.release_camera(camera) end
camera = nil

Passing nil does nothing, and it never clears another script’s ownership.

Check whether you still own the camera.

Signature: sf2.world.is_camera_active(camera)

Parameters

Parameter Type Required
camera CameraHandle Required

Returns: true while you own the camera, false after release or expiry.

When: After acquiring.

if camera and not sf2.world.is_camera_active(camera) then camera = nil end

Move, resize, re-sprite or recolor an existing marker.

Signature: sf2.world.try_set_marker(marker, { rect = ..., sprite = ..., color = ... })

Parameters

Parameter Type Required
marker ArenaMarkerHandle Required
changes ArenaMarkerChanges Required

Fields of ArenaMarkerChanges

Field Type Required Meaning
rect ArenaRect Optional New position and size.
sprite string|SpriteHandle Optional New artwork.
color string Optional New tint as #RRGGBB or #RRGGBBAA.

Returns: true, or false, reason for a closed marker or unusable sprite. A bad field or empty table is an error.

When: After creation; use on_tick for movement that follows combat pause.

region.x = region.x + 2
local ok, why = sf2.world.try_set_marker(marker, { rect = region, color = "#ff332299" })
if not ok then sf2.log.warn(why) end
local inside = fighter:overlaps_rect(region)

It does not move the sensor: update the rectangle you pass to overlaps_rect yourself. Omitted fields stay as they are; sprite turns a filled rectangle into art, and your Lua picks the frame order. Giving the sprite or rectangle the marker already has changes nothing and costs nothing, but for an animation it is still tidier to call this only when the frame changes. Fields apply in the order rect, sprite, color, so a failure leaves earlier ones applied. A closed marker cannot be restarted.

A marker made with a node keeps following it: a new rect is measured from that same node, and node and of are not accepted here.

Remove a marker. Removing it twice is safe.

Signature: sf2.world.remove_marker(marker)

Parameters

Parameter Type Required
marker ArenaMarkerHandle Required

Returns: true if the marker was active, otherwise false.

When: After creation.

if marker then sf2.world.remove_marker(marker); marker = nil end

Passing nil does nothing. Forgotten markers are removed at round end.

Check whether a marker still exists (not whether the camera can see it).

Signature: sf2.world.is_marker_active(marker)

Parameters

Parameter Type Required
marker ArenaMarkerHandle Required

Returns: true while the marker exists, otherwise false.

When: After creation.

if marker and not sf2.world.is_marker_active(marker) then marker = nil end