I'm making a program that uses a lot of variables and changes them constantly.
How to save those variables into another file from inside the program?
I'm making a program that uses a lot of variables and changes them constantly.
How to save those variables into another file from inside the program?
You have to use io.open(filename, mode)
to create a file handle, then use :write(linecontent)
and :read("*line")
to write and read in order. From there you can "load" and "save" variables by keeping track of the line orders for each variable you use:
local f = assert(io.open("quicksave.txt", "w"))
f:write(firstVariable, "\n")
f:write(secondVariable, "\n")
f:write(thirdVariable, "\n")
f:close()
local f = assert(io.open("quicksave.txt", "r"))
firstVariable = f:read("*line")
secondVariable = f:read("*line")
thirdVariable = f:read("*line")
f:close()
Best way would be to put the variables inside a table and then use textutils.serialize like this:
To save it do:
local file = fs.open("filename", "w")
file.write(textutils.serialize(your_table))
file.close()
To load it do:
local file = fs.open("filename", "r")
your_table = textutils.unserialize(file.readAll())
file.close()