Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dataFile = "/cookie"
- local machineOutput, tankInput = 0
- function boot()
- term.clear()
- term.setCursorPos(1,1)
- local data = { }
- -- check file data
- if fs.exists(dataFile) then
- local handle = assert(fs.open(dataFile, "r"), "Couldn't load "..dataFile) -- this file is opened in read mode
- local input = handle.readAll() -- input is now the serialized table
- handle.close()
- -- -- load data
- data = textutils.unserialize(input) -- this will decode our table
- machineOutput = tonumber( data.machineOutput )
- tankInput = tonumber( data.tankInput )
- -- -- create data file
- else
- print("Create "..dataFile.." file")
- storeData()
- end
- end
- function storeData()
- local data = { }
- data.machineOutput = machineOutput
- data.tankInput = tankInput
- local output = textutils.serialize(data)
- local handle = assert(fs.open(dataFile, "w"), "Couldn't save "..dataFile) -- this will give you an error if something's gone wrong
- handle.write(output) -- this is where the magic happens, we're storing the entire "target" table
- handle.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment