|
| 1 | +# Mod System API |
| 2 | + |
| 3 | +The Mod System API allows you to work with mods in ScheduleLua, including managing dependencies, and exporting/importing functionality between mods. |
| 4 | + |
| 5 | +## Mod Management Functions |
| 6 | + |
| 7 | +### GetMod |
| 8 | + |
| 9 | +**Signature:** `table GetMod(string modName)` |
| 10 | + |
| 11 | +**Description:** Gets information about a loaded mod by its folder name. |
| 12 | + |
| 13 | +#### Parameters |
| 14 | + |
| 15 | +- `modName` (string): The folder name of the mod to get information about |
| 16 | + |
| 17 | +#### Returns |
| 18 | + |
| 19 | +A table containing information about the mod, or nil if the mod is not loaded. The table has the following structure: |
| 20 | + |
| 21 | +```lua |
| 22 | +{ |
| 23 | + name = "My Mod", -- Display name from manifest |
| 24 | + folder = "my_mod", -- Folder name |
| 25 | + version = "1.0.0", -- Version from manifest |
| 26 | + description = "...", -- Description from manifest |
| 27 | + author = "Your Name", -- Author from manifest |
| 28 | + dependencies = {...}, -- Table of dependency folder names |
| 29 | + api_version = "0.1.2" -- API version from manifest |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +#### Example |
| 34 | + |
| 35 | +```lua |
| 36 | +local mod = GetMod("economy_mod") |
| 37 | +if mod then |
| 38 | + Log("Mod name: " .. mod.name) |
| 39 | + Log("Mod version: " .. mod.version) |
| 40 | + Log("Mod author: " .. mod.author) |
| 41 | +end |
| 42 | +``` |
| 43 | + |
| 44 | +### GetAllMods |
| 45 | + |
| 46 | +**Signature:** `table GetAllMods()` |
| 47 | + |
| 48 | +**Description:** Gets information about all loaded mods. |
| 49 | + |
| 50 | +#### Returns |
| 51 | + |
| 52 | +An array of mod information tables, each with the same structure as returned by `GetMod()`. |
| 53 | + |
| 54 | +#### Example |
| 55 | + |
| 56 | +```lua |
| 57 | +local mods = GetAllMods() |
| 58 | +Log("Loaded mods: " .. #mods) |
| 59 | +for _, mod in ipairs(mods) do |
| 60 | + Log("- " .. mod.name .. " v" .. mod.version .. " by " .. mod.author) |
| 61 | +end |
| 62 | +``` |
| 63 | + |
| 64 | +### IsModLoaded |
| 65 | + |
| 66 | +**Signature:** `boolean IsModLoaded(string modName)` |
| 67 | + |
| 68 | +**Description:** Checks if a mod is loaded by its folder name. |
| 69 | + |
| 70 | +#### Parameters |
| 71 | + |
| 72 | +- `modName` (string): The folder name of the mod to check |
| 73 | + |
| 74 | +#### Returns |
| 75 | + |
| 76 | +`true` if the mod is loaded, `false` otherwise. |
| 77 | + |
| 78 | +#### Example |
| 79 | + |
| 80 | +```lua |
| 81 | +if IsModLoaded("required_mod") then |
| 82 | + Log("Required mod is loaded!") |
| 83 | +else |
| 84 | + LogError("Required mod is not loaded!") |
| 85 | +end |
| 86 | +``` |
| 87 | + |
| 88 | +## Function Import/Export |
| 89 | + |
| 90 | +### ExportFunction |
| 91 | + |
| 92 | +**Signature:** `void ExportFunction(string name, function func)` |
| 93 | + |
| 94 | +**Description:** Exports a function from the current mod, making it available for other mods to import. |
| 95 | + |
| 96 | +#### Parameters |
| 97 | + |
| 98 | +- `name` (string): The name to export the function as |
| 99 | +- `func` (function): The function to export |
| 100 | + |
| 101 | +#### Example |
| 102 | + |
| 103 | +```lua |
| 104 | +ExportFunction("CalculateTax", function(amount, rate) |
| 105 | + rate = rate or 0.15 |
| 106 | + return amount * rate |
| 107 | +end) |
| 108 | +``` |
| 109 | + |
| 110 | +### ImportFunction |
| 111 | + |
| 112 | +**Signature:** `function ImportFunction(string modName, string functionName)` |
| 113 | + |
| 114 | +**Description:** Imports a function exported by another mod. |
| 115 | + |
| 116 | +#### Parameters |
| 117 | + |
| 118 | +- `modName` (string): The folder name of the mod that exports the function |
| 119 | +- `functionName` (string): The name of the exported function to import |
| 120 | + |
| 121 | +#### Returns |
| 122 | + |
| 123 | +The exported function, or nil if the mod is not loaded or the function is not exported. |
| 124 | + |
| 125 | +#### Example |
| 126 | + |
| 127 | +```lua |
| 128 | +local CalculateTax = ImportFunction("economy_mod", "CalculateTax") |
| 129 | +if CalculateTax then |
| 130 | + local tax = CalculateTax(1000) |
| 131 | + Log("Tax amount: " .. tax) |
| 132 | +end |
| 133 | +``` |
| 134 | + |
| 135 | +### GetModExport |
| 136 | + |
| 137 | +**Signature:** `any GetModExport(string modName, string exportName)` |
| 138 | + |
| 139 | +**Description:** Gets an exported value (function or other value) from a mod. |
| 140 | + |
| 141 | +#### Parameters |
| 142 | + |
| 143 | +- `modName` (string): The folder name of the mod that exports the value |
| 144 | +- `exportName` (string): The name of the exported value to get |
| 145 | + |
| 146 | +#### Returns |
| 147 | + |
| 148 | +The exported value, or nil if the mod is not loaded or the value is not exported. |
| 149 | + |
| 150 | +#### Example |
| 151 | + |
| 152 | +```lua |
| 153 | +local TAX_RATE = GetModExport("economy_mod", "TAX_RATE") |
| 154 | +if TAX_RATE then |
| 155 | + Log("Current tax rate: " .. TAX_RATE) |
| 156 | +end |
| 157 | +``` |
| 158 | + |
| 159 | +## Best Practices |
| 160 | + |
| 161 | +1. Always check if required mods are loaded before trying to use them: |
| 162 | + |
| 163 | +```lua |
| 164 | +if not IsModLoaded("required_mod") then |
| 165 | + LogError("This mod requires 'required_mod' to be installed") |
| 166 | + return |
| 167 | +end |
| 168 | +``` |
| 169 | + |
| 170 | +2. Check if imported functions exist before using them: |
| 171 | + |
| 172 | +```lua |
| 173 | +local func = ImportFunction("mod", "func") |
| 174 | +if func then |
| 175 | + func() |
| 176 | +else |
| 177 | + LogError("Failed to import function 'func' from 'mod'") |
| 178 | +end |
| 179 | +``` |
| 180 | + |
| 181 | +3. Use namespaces for your mod's global data to avoid conflicts: |
| 182 | + |
| 183 | +```lua |
| 184 | +MY_MOD = { |
| 185 | + version = "1.0.0", |
| 186 | + settings = {} |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +4. Document your exported functions for other mod authors: |
0 commit comments