Here is a quick solution that I wrote up for you. This list of functions allows you to save/read variables to/from a file using tables.
Keep in mind, this script will not allow you to save tables!
Saving variables would look something like this:
local variables = {1, 2, 3, 4, "Five."}
saveVariables(variables, "/save")
The contents of "save" would look like this:
1.0
2.0
3.0
4.0
Five.
Loading from "save":
local variables = loadVariables("/save")
Keep in mind that all variables read from a saved file will have a value of string. You'll have to cast variables back to what you'd like their types to be.
The code below has been edited thanks to the suggestion of Kingdaro below.
-- This function takes a table that contains all arguments that
-- you wish to save.
function saveVariables(variablesTable, savePath)
-- Get a handle on the file we are to save to.
local fileHandle = fs.open(savePath, 'w')
if fileHandle then
fileHandle.write(textutils.serialize(variablesTable))
fileHandle.close()
else
error("File handle could not be obtained during saving.")
end
end
-- This function returns a table containing all of the variables read
-- from the given file path in a linear fashion (top to bottom).
function loadVariables(loadPath)
local variablesTable = {}
-- Get handle on the file we are to load from.
local fileHandle = fs.open(loadPath, 'r')
if fileHandle then
local fileContents = fileHandle.readAll()
fileHandle.close()
variablesTable = textutils.unserialize(fileContents)
return variablesTable
else
error("File handle could not be otained during loading.")
end
end