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

Save Variables From Something That Is Not An Input?

Started by TylerTharp, 04 August 2013 - 08:43 PM
TylerTharp #1
Posted 04 August 2013 - 10:43 PM
Today while working on a few programs, I had an idea that would make my programs more efficient. I need to some how make it to where my code saves variables from the program itself. Take for example, my harvesting program. When a player leaves the chunk, the program restarts because no one is in that chunk. I would have many uses from this but do not know where to start. It would need to save variables for like minutes, seconds, etc. I do not now where I would put this code, or where to even begin making it. Can someone help me out?

Here is my harvesting program for a reference:

local wait_time = 30*60
local time_remaining = 0
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.green)
while true do
  term.clear()
  term.setCursorPos(1,1)
	    time_remaining = wait_time
  opt = rs.setO
	    print("RUNNING NS TECH CODE - EDITING PROHIBITED")
	    print("NS Tech AutoHarvest v1.0")
	    print("---------------------------------------------------")
    while not sleep(1) and time_remaining > 0 do
		  term.setCursorPos(1,4)
		  term.clearLine()
		  local minutes = math.floor(time_remaining/60)
		  local seconds = math.floor(time_remaining%60)
		  print("Next harvest in " .. minutes .. " minutes and " .. seconds .. " seconds.")
		  time_remaining = time_remaining - 1
end
  while true do
  rs.setOutput("bottom", true)
  sleep(8)
  rs.setOutput("bottom", false)
  os.reboot()
  end
end
Grim Reaper #2
Posted 05 August 2013 - 02:57 AM
This is often referred to as variable persistence. Basically, you're taking the value held by a variable and writing it to a file for reading and using later.

To do this, you'll need to obtain a file handle using fs.open in write mode so that you can write your variables to the file. After that, you're going to need to write the variables to the file in a format that your program will be able to interpret later for usage. This format is going to be something that you'll decide on so that you can write your program to create the file and read it properly later. When you've finished writing your variables, simply close the file handle.

Once you've written your variables to a file in a format of your choice, you'll need to instruct your program to obtain a handle on the same file using fs.open but in read mode this time. You'll need to use the aforementioned handle to read in the variables that you've saved in the format you chose and, finally, close the handle.

There a lot of scripts/apis that deal with variable persistence, so you might want to look at a couple of those to really get an idea of how you want to go about solving your problem.

Here is a link to how to use the fs.open function to gain file handles in different modes and what functions can be invoked on these handles: link.\



An example format for saving your variables might be to throw all of the variables into a table and use textutils.serialize to get a string version of the table containing your variables for writing to your variable file. Once that's done, you can simply read the contents of the file back and use textutils.unserialize on the string read from the file for your table of variables. Another idea, if you have a specific order in which you want to write the variables to the file, then you can just write the values and read them back in the same order as you wrote them.