File loading - Oinite12/tiwmig-mod GitHub Wiki
You may notice that index.lua
, the mod's main file, is pretty empty. Most of the modded content is instead implemented in files in the items
folder; this separation greatly assists with organization and code navigation.
index.lua
contains the following code for loading these files:
local files = NFS.getDirectoryItems(SMODS.current_mod.path .. "items") -- "items" can be replaced with the folder name containing the files
for _, file in ipairs(files) do
print("[TIWMIG] Loading item file " .. file) -- Sent in the console
local f, err = SMODS.load_file("items/" .. file)
if err then
error(err) -- Steamodded can give an error if something is wrong in any of the files
end
f() -- Run the loaded Lua code
end
First, a list of file names are obtained from the items folder. These are then each inputted into the SMODS.load_file()
function, which takes a file name and returns the code within as a function that can be run. Finally, this function is ran.
Loading the global file
global.lua
contains constants and functions used across all files. This file is loaded first before loading any other file with the following simple code:
local f,err = SMODS.load_file("global.lua")
if err then error(err) end
f()
G_TWMG
, the object that global.lua
adds values to, has a high scope, and thus the previous code can add to it without needing to define G_TWMG
directly in global.lua
.
Acknowledgements
The file-loading code is based on the file-loading code found in Cryptid.lua
for the Cryptid mod.