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

Variables and world reloading

Started by KingOfAllChunks, 05 March 2014 - 10:44 AM
KingOfAllChunks #1
Posted 05 March 2014 - 11:44 AM
Hello pros! Today i have another question for you, this time i just cannotget it working… All i need is a computer to remember a current condition of a variable, and on the next boot up to read it ( aka i=1 ) AFRER world reload. We all know that any computers/turtles restart and lose any information which occured during the running of a program.

Both ussless and the most useful link: https://www.google.com/
apemanzilla #2
Posted 05 March 2014 - 04:42 PM
add these:

local function save(file, data)
  local file=fs.open(file,"w")
  file.write(data)
  file.close()
end

local function load(file)
  local file = fs.open(file,"r")
  local t = file.readAll()
  file.close()
  return t
end
And then to write the variable to a file…

save("any_filename",variable)
And load…

variable = load("same_filename_from_before")
Edited on 05 March 2014 - 03:43 PM
TechMasterGeneral #3
Posted 05 March 2014 - 05:03 PM
What Apemanzilla posted should work…
KingOfAllChunks #4
Posted 05 March 2014 - 07:30 PM
add these:

local function save(file, data)
  local file=fs.open(file,"w")
  file.write(data)
  file.close()
end

local function load(file)
  local file = fs.open(file,"r")
  local t = file.readAll()
  file.close()
  return t
end
And then to write the variable to a file…

save("any_filename",variable)
And load…

variable = load("same_filename_from_before")
thanks! If thats the only way, then i will do it for shure! Wasnt there something with the global variables ( where for i=1 you dont say "local" )…? Im not very familliar with this and as im learning and reading i'm still missing some stuff…
CometWolf #5
Posted 05 March 2014 - 07:55 PM
where for i=1 you dont say "local"
This is called a for loop, and has nothing to do with storing variables.

for i=number1,number2,number3 do
  --repeat this code
  --then add number3 to number1
  --and repeat until number1 is greater or equal to number2
end

All variables, both local and global get reset when the computer is restarted. Thus the only way of storing something, is by storing it to a file, rather than a variable.
KingOfAllChunks #6
Posted 05 March 2014 - 09:05 PM
where for i=1 you dont say "local"
This is called a for loop, and has nothing to do with storing variables.

for i=number1,number2,number3 do
  --repeat this code
  --then add number3 to number1
  --and repeat until number1 is greater or equal to number2
end

All variables, both local and global get reset when the computer is restarted. Thus the only way of storing something, is by storing it to a file, rather than a variable.
That's why humans learn from their mistakes. Good to know! Thanks
Bomb Bloke #7
Posted 05 March 2014 - 09:47 PM
For what it's worth, though, in a "for" loop the counter variable (eg "i") is indeed automatically localised to that loop.
KingOfAllChunks #8
Posted 06 March 2014 - 08:47 AM
add these:

local function save(file, data)
  local file=fs.open(file,"w")
  file.write(data)
  file.close()
end

local function load(file)
  local file = fs.open(file,"r")
  local t = file.readAll()
  file.close()
  return t
end
And then to write the variable to a file…

save("any_filename",variable)
And load…

variable = load("same_filename_from_before")
i wonder is it lag-free to let the computer read/write on the file in a fast loop? Im Playing FTB Ultimate and with soo many mods you just need a lag-free solution
CometWolf #9
Posted 06 March 2014 - 09:55 AM
Dosen't matter what the computer is doing, it will cause the same amount of lag, which is pretty much none. However, storing the variable if it's not been changed is obviously very redundant, and you should know when it's been changed…
KingOfAllChunks #10
Posted 06 March 2014 - 05:26 PM
Dosen't matter what the computer is doing, it will cause the same amount of lag, which is pretty much none. However, storing the variable if it's not been changed is obviously very redundant, and you should know when it's been changed…
whell….. i guess i would need to pull some events then…
CometWolf #11
Posted 06 March 2014 - 05:47 PM
How is the variable changing in the first place, if you don't even know when it's doing it?
Edited on 06 March 2014 - 04:47 PM
KingOfAllChunks #12
Posted 06 March 2014 - 05:55 PM
How is the variable changing in the first place, if you don't even know when it's doing it?
if the program is running, the variable changes from time to time when a player interacts with an advanced monitor above the computer running the problem script which i didnt post because there are no errors, just i dont know how to remember it. So i have a infinite loop and i ask if it is good to keep saving/reading every loop…
CometWolf #13
Posted 06 March 2014 - 06:25 PM
You know when it changes, If your using a monitor_touch event to alter it.
KingOfAllChunks #14
Posted 07 March 2014 - 11:31 AM
You know when it changes, If your using a monitor_touch event to alter it.
what if the person who touched the schreen didnt touch on the coordinates where i test for?
Bomb Bloke #15
Posted 07 March 2014 - 11:37 AM
That sounds like the sort of question you need to ask yourself. Under that circumstance, do you alter your variable? If not, why would you bother re-saving it?