Code: Select all
-- This function flags a variable to be saved by Mudlet's variable persistence system.
-- Usage: remember("varName")
-- Example: remember("table_Weapons")
-- Example: remember("var_EnemyHeight")
-- Variables are automatically unpacked into the global namespace when the profile is loaded.
-- They are saved to "SavedVariables.lua" when the profile is closed or saved.
function remember(varName)
if not _saveTable then
_saveTable = {}
end
_saveTable[varName] = _G[varName]
end
--- This function should be primarily used by Mudlet. It loads saved settings in from the Mudlet home directory
--- and unpacks them into the global namespace.
function loadVars()
if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else _sep = "\\" end
local l_SettingsFile = getMudletHomeDir() .. _sep .. "SavedVariables.lua"
local lt_VariableHolder = {}
if (io.exists(l_SettingsFile)) then
table.load(l_SettingsFile, lt_VariableHolder)
for k,v in pairs(lt_VariableHolder) do
_G[k] = v
end
end
end
-- This function should primarily be used by Mudlet. It saves the contents of _saveTable into a file for persistence.
function saveVars()
if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else _sep = "\\" end
local l_SettingsFile = getMudletHomeDir() .. _sep .. "SavedVariables.lua"
for k,_ in pairs(_saveTable) do -- Update all variables
remember(k)
end
table.save(l_SettingsFile, _saveTable)
end