Variable Saving/Loading System
Posted: Sun Dec 06, 2009 4:10 am
Vadi wanted to give you guys a crack at some new code coming next version, just to make sure it doesn't break. So, without further adieu.. Introducing the variable perstistence system. This is a way to store variables/tables/etc for later use! Currently it requires manual loading and saving, but once its been put through the works it'll be hooked up to load/save on profile open/close
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