nSun

CC counter

Apr 3rd, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.05 KB | None | 0 0
  1. local dataFile = "/cookie"
  2. local machineOutput, tankInput = 0
  3. function boot()
  4.     term.clear()
  5.     term.setCursorPos(1,1)
  6.     local data = { }
  7.     -- check file data
  8.     if fs.exists(dataFile) then
  9.         local handle = assert(fs.open(dataFile, "r"), "Couldn't load "..dataFile) -- this file is opened in read mode
  10.         local input = handle.readAll() -- input is now the serialized table
  11.         handle.close()
  12.     -- -- load data
  13.         data = textutils.unserialize(input) -- this will decode our table
  14.         machineOutput = tonumber( data.machineOutput )
  15.         tankInput  = tonumber( data.tankInput  )
  16.     -- -- create data file
  17.     else
  18.         print("Create "..dataFile.." file")
  19.         storeData()
  20.     end
  21. end
  22.  
  23. function storeData()
  24.     local data = { }
  25.     data.machineOutput  = machineOutput
  26.     data.tankInput = tankInput
  27.     local output = textutils.serialize(data)
  28.     local handle = assert(fs.open(dataFile, "w"), "Couldn't save "..dataFile) -- this will give you an error if something's gone wrong
  29.     handle.write(output) -- this is where the magic happens, we're storing the entire "target" table
  30.     handle.close()
  31. end
Advertisement
Add Comment
Please, Sign In to add comment