Lua basics
Eclipse mods use Lua, a small, friendly scripting language. You do not install Lua; Eclipse runs your scripts itself. This page explains the notation used throughout the wiki. If you have written any code before, skim it.
Load the API and keep a value
Section titled “Load the API and keep a value”local sf2 = require("sf2")local price = 5sf2.log.info("My mod is loading")localcreates a variable, a name for a value.require("sf2")hands you the Eclipse API. Every script begins with this line.sf2.log.info(...)calls a function: the text in parentheses is its argument. The message appears in the game log.- A line starting with
--is a comment. Eclipse ignores it.
Types of value
Section titled “Types of value”| Type | Example | Common mistake |
|---|---|---|
| Text (a string) | "training_blade" |
Text without quotes is read as a variable name. |
| Number | 5, 0.25 |
"5" in quotes is text, not a number. |
| Yes or no (a boolean) | true, false |
Write them in lowercase, without quotes. |
| Nothing | nil |
This is different from false and from 0. |
| Table | { level = 1 } |
Separate the fields with commas. |
| Function | function() ... end |
Close the function with end. |
An integer is a whole number. Each field in the reference says which type it accepts and the allowed range.
Tables describe content
Section titled “Tables describe content”A table groups named fields:
local settings = { id = "training_blade", level = 1,}sf2.log.info(tostring(settings.level)) -- logs 1Read a field with a dot: settings.level. A trailing comma is fine. When you
call a function with a single table, you can leave out the parentheses, so these
two lines mean the same thing:
sf2.items.register({ category = "weapon", id = "blade" })sf2.items.register { category = "weapon", id = "blade" }Most Eclipse functions take exactly one table. That is why mods are mostly
lists of name = value lines.
Tables also hold ordered lists:
local names = { "Blade", "Staff", "Spear" }local first = names[1] -- Lua counts from 1, not 0Keep lists dense: no gaps and no nil in the middle. Order sometimes matters, for
example in the reward slots of a fight.
Handles and IDs
Section titled “Handles and IDs”A handle is a value the API gives you that stands for one thing in the game,
such as an item or a sprite. You pass it on to other functions. The same thing
can usually also be written as an ID, a string such as
"core:items/weapon/weapon_katana".
local icon = sf2.assets.sprite("sprites/weapon") -- icon is a handlelocal blade = sf2.items.get("core:items/weapon/weapon_katana") -- also a handle"sprites/weapon" is text (an asset ID). icon is the handle you get back.
Wherever a field asks for a handle, you can usually write the ID string instead.
How mods work explains this once, properly.
Functions and callbacks
Section titled “Functions and callbacks”A function holds code to run later. A callback is a function you give to Eclipse so the game calls it when something happens, such as a hit:
local function describe(fighter) if fighter.health > 0 then sf2.log.info("Fighter health: " .. tostring(fighter.health)) endend..joins text.if ... then ... endruns code only when the condition is true.==compares two values. A single=assigns.and,orandnotcombine conditions. Onlyfalseandnilcount as false;0and""count as true.
Use a dot for API functions (sf2.log.info(...)) and a colon for methods
on a fighter (fighter:change_health(0.05)). The colon passes the fighter along
automatically. Mixing them up is a common mistake.
Always give a new local a value, even if it is nil. Write
local choice = nil, not just local choice. Inside a loop or a function that
is called repeatedly, a bare local can start with a value left over from an
earlier pass.
for _, name in ipairs(names) do local match = nil -- not just "local match" if name == "Staff" then match = name endendCallbacks are explained in Combat scripting. The fighter you receive in a callback only works until the callback returns, so do not store it for later.
Split a larger script
Section titled “Split a larger script”Your entry script can load other files from your mod’s scripts folder with
require:
local helpers = require("helpers") -- scripts/helpers.luasf2.log.info(helpers.message)-- scripts/helpers.luareturn { message = "My mod is loading" }Use require("combat.helpers") for scripts/combat/helpers.lua. A mod can load
only its own modules. Files, the network and other programs are not available to
mod scripts.
Read errors one at a time
Section titled “Read errors one at a time”If Lua reports a syntax error, look at the file and line it names for a
missing comma, quote, brace or end. If an Eclipse function reports a wrong
type or a missing field, compare your table with that function’s page in the
reference. Fix the first error, restart, and try again. Later errors are often
the result of the first one. Troubleshooting has more.
Next: Make your first mod with complete files you can copy.