This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dom123's profile picture

Saving Variables

Started by dom123, 16 January 2014 - 03:47 PM
dom123 #1
Posted 16 January 2014 - 04:47 PM
I am trying to write an interactive program that will be based completely off variables, how do I save variables in-between chunk loading and unloading and server restarts?
CometWolf #2
Posted 16 January 2014 - 04:55 PM
You'll need to store them to a file on the computer using the fs api

local file = fs.open(fileName,fileMode) -- use "w" fileMode to write files. Note that using "w" wipes the file clean when you open it.
file.writeLine(variable) --writes a line containing variable to the file
file.close() --closes the file when you are done with it
local file = fs.open(fileName,fileMode) -- use "r" filemode to read files
file.readLine() --this will return one line of the file each time you call it
file.close() -- and close when you are done
Those are the basics which should be all you need, but there's a few more things you can do with it which you can read about here
http://computercraft.info/wiki/Fs_%28API%29