Both ussless and the most useful link: https://www.google.com/
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Variables and world reloading
Started by KingOfAllChunks, 05 March 2014 - 10:44 AMPosted 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/
Both ussless and the most useful link: https://www.google.com/
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
Posted 05 March 2014 - 05:03 PM
What Apemanzilla posted should work…
Posted 05 March 2014 - 07:30 PM
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…add these:And then to write the variable to a file…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 load…save("any_filename",variable)
variable = load("same_filename_from_before")
Posted 05 March 2014 - 07:55 PM
This is called a for loop, and has nothing to do with storing variables.where for i=1 you dont say "local"
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.
Posted 05 March 2014 - 09:05 PM
That's why humans learn from their mistakes. Good to know! ThanksThis is called a for loop, and has nothing to do with storing variables.where for i=1 you dont say "local"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.
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.
Posted 06 March 2014 - 08:47 AM
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 solutionadd these:And then to write the variable to a file…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 load…save("any_filename",variable)
variable = load("same_filename_from_before")
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…
Posted 06 March 2014 - 05:26 PM
whell….. i guess i would need to pull some events then…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…
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
Posted 06 March 2014 - 05:55 PM
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…How is the variable changing in the first place, if you don't even know when it's doing it?
Posted 06 March 2014 - 06:25 PM
You know when it changes, If your using a monitor_touch event to alter it.
Posted 07 March 2014 - 11:31 AM
what if the person who touched the schreen didnt touch on the coordinates where i test for?You know when it changes, If your using a monitor_touch event to alter it.
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?