Dependencies, conflicts and mod-to-mod calls
A mod can name the mods it needs, the mods it can use when they are there, and the mods it cannot run with. It can also call functions another mod offers.
depends and conflicts #
"depends": { "yourname.tea-lib": ">=1.0.0 <2.0.0" },
"conflicts": { "someone.other-shop": "*" }- Each entry is a mod id and a version range. Each key holds at most 16 entries.
- A range is
*for any version, or up to 4 comparisons split by one space. A comparison is>=,<=,>,<or=and a version. 1.2.3alone means exactly that version.^and~are not ranges here.- A mod cannot name itself. A mod id goes in only one of
depends,optional_dependsandconflicts.
What the game does with them #
- A mod loads after the mods it depends on. Otherwise the player's order in the Mods tab stands. A mod moves only as far as its dependencies need.
- A mod is not loaded when a mod it depends on is missing, switched off, the wrong version or failed to load. Its row in the Mods tab says why and names the mod it needs.
- A mod stays off when it lists a conflict with a mod that is on in a matching version. Its row gives that reason.
- Mods that need each other in a circle cannot load.
- The friend visit match uses this same order.
Optional dependencies #
optional_depends names mods yours can use but does not need. It has the same shape and the same ranges as depends, and holds at most 16 entries. Your mod always loads.
"optional_depends": { "yourname.weather": ">=1.0.0 <2.0.0" }- An optional dependency is present when it is installed, switched on, loading and inside the range.
- A present one loads before your mod, exactly as a needed one does.
- It is absent in every other case: not installed, switched off, not loading, or a version outside the range. Nothing waits for an absent one, and your mod loads in the player's order.
- The Mods tab lists them on your mod's row. One the player has on reads "Works with <name>, which you have on." One that is off or missing reads "Also works with <name>". It is plain text, never a warning.
Asking whether it is there #
remote.version(mod) answers for a mod you name in depends or optional_depends. It
returns the version, such as "1.2.0", when the mod is present. When it is not, it returns nil, a message and a code.
on("villager_talked", function(e)
-- the version when it is there, or nil, a message and a code
local version, why, code = remote.version("yourname.weather")
if not version then
return { place = "before", line = "Lovely day, I think." } -- code is "dependency-absent"
end
local sky = remote.call("yourname.weather", "today")
return { place = "before", line = "They say it will be " .. sky .. " today." }
end)| Code | When |
|---|---|
dependency-absent | The mod is not installed, is switched off, did not load, or has no running script. |
dependency-version | The mod loaded, but its version is outside the range this mod names. |
dependency-undeclared | The mod is not in this mod's depends or optional_depends. |
Calling it when it is absent #
remote.call to an absent optional dependency is not an error. It calls nothing and returns nil, a message and one of the codes above. Your script keeps running.
local sky, why, code = remote.call("yourname.weather", "today")
if sky == nil and code then
-- the weather mod is not there: nothing was called, nothing was raised
sky = "fine"
end- A function that itself returns
nilgives oneniland no code. The code tells the two apart. - A call to a mod you did not name keeps its old meaning. It raises an error when that mod has no running script.
- A present dependency that does not offer the function still raises an error. Check with
remote.haswhen you are not sure of the name.
Two mods that use each other #
- A circle through an optional dependency never stops a mod from loading.
- The game cannot load both first. The mod that is earlier in the player's order loads first, and the other loads after it.
- Both are still present.
remote.versionandremote.callwork in events for both. - Only the "loads first" promise is dropped, for the mod that went first. Its top level runs before the other mod's script exists. Ask and call in an event, not at the top of the script.
- A circle made only of needed dependencies is still refused.
Calls between mods #
A mod offers functions with remote.add(name, fn) while its script loads. Another mod calls them with remote.call(mod, name, ...). Check first with remote.has(mod, name).
-- yourname.tea-lib: offer a function while the script loads
remote.add("brew", function(kind, cups)
return { kind = kind, cups = cups, hot = true }
end)-- a mod that depends on yourname.tea-lib
on("villager_talked", function(e)
if not remote.has("yourname.tea-lib", "brew") then return nil end
local tea = remote.call("yourname.tea-lib", "brew", "green", 2)
return { place = "before", line = "Two cups of " .. tea.kind .. " tea!" }
end)- Arguments and results are copied. They can be nil, true or false, numbers, text and tables of those. The limits are 64 KB, 16 levels deep and 8 results.
- Functions cannot be passed. Changing a copy never changes the other mod's table.
- The function you call runs on your time budget. If it runs too long, your script is stopped and the other mod keeps running.
- Its commands count toward your event's cap. The cap covers your commands and those of every mod you call, together.
- Its
rng()draws from that mod's own numbers, not yours. - A call runs at once. It is not one of the other mod's events, so it does not wait behind events that mod still has queued.
- An error inside the other mod's function comes back as your error. Catch it with
pcall. The other mod keeps running. - A call back into a mod that is already running is refused, so calls cannot loop. That covers your own mod and any mod further up the chain of calls.
- A name is 1 to 32 characters from
a-z 0-9 _. A mod offers at most 32 names.
A library mod #
A mod that only offers functions to other mods still needs a scripts section, and scripts.events must list at least one event. List one and register no handler for it. An event with no
handler costs nothing and never runs your script. The script's top level, where remote.add is called, runs
once when the game starts.
"scripts": { "main": "main.lua", "events": ["day_started"] }List what your library offers in exports. Each entry is a function's name and one line saying what it
does. It holds at most 32 entries, with descriptions up to 120 characters. The key is optional and needs scripts.
"exports": {
"brew": "Makes tea: brew(kind, cups) returns { kind, cups, hot }."
}- With
exports,remote.addcan offer only the names it lists. The Mod log says when a listed name was never offered. - Each name comes with a one-line description, so the author of a mod that depends on yours knows what to call.
Dependencies and friend visits #
- A visit compares only World mods. A Just you mod never blocks a visit, even when another mod depends on it.
- A World mod that depends on a Just you mod loads only where that Just you mod is installed and on. A friend who has your World mod without it cannot load your World mod. The two towns then differ, and the visit is refused, naming your World mod.
- Visiting with the host's mods does not help, because only World mods are lent.
- A World mod should depend only on World mods, or on nothing. When a World mod needs a library, make the library
"world"too. - An optional dependency is a mod like any other for a visit. When it is a World mod that counts, it is part of what the visit compares. A friend who has your mod without it has a different set, and the visit says which mod differs.
- An optional dependency that is a Just you mod never blocks a visit. Your World mod loads on both sides, with it or without it.
- A visitor who borrows the host's mods gets every World mod of the host that counts, optional dependencies included.