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

Saving Variables

Started by Tiin57, 15 July 2012 - 02:56 PM
Tiin57 #1
Posted 15 July 2012 - 04:56 PM
How do I save a variable without using a custom api? I want to have my lock program preserve what side the redstone output is on when the world resets, but I cannot figure out how to go about it.
1lann #2
Posted 15 July 2012 - 05:08 PM
You could try writing the variables to a file
local function save(data) 
  f = io.open("rsData", "w")
  f:write(tostring(data))
  f:close()
end

local function load()
  f = io.open("rsData", "r")
  rData = f:read("*l")
  if rData == "true" then
    return true
  elseif rData == "false" then
    return false
  else
    return nil
end
Ponder #3
Posted 15 July 2012 - 05:43 PM
The standard textutils API has a serialize and a unserialize function, which can turn tables into text and vice versa. For a quick solution you could just put whatever you need to store inside a table, serialize and write it to a file.
Tiin57 #4
Posted 15 July 2012 - 05:47 PM
Thank you for your help; I have fixed it up accordingly.