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

Saving Variables?

Started by Gorni, 19 February 2012 - 10:38 AM
Gorni #1
Posted 19 February 2012 - 11:38 AM
Hi everyone, im pretty new to computer craft and to coding.

i made a little program that lists the items of several chests. my problem is, i play on a smp server and everytime the chunk unloads the program is reset and the variables containing the stored amounts is reset.

is there any possible way to save the variables and reload them on the next restart, so i dont have to manually input them every time i start the program?

thanks and greets

gorni
Advert #2
Posted 19 February 2012 - 12:38 PM
You can save them via file writing:


local filehandle = io.open("saveddata", "w") -- open to write
filehandle:write("Hello, World!") -- Write something
filehandle:close() Very important; this tells the computer that you're done with the file, and it doesn't need to keep it open anymore.

filehandle = io.open("saveddata", "r") -- Opens for reading

print(filehandle:read("*l")) -- Reads one line
filehandle:close()
Gorni #3
Posted 19 February 2012 - 01:07 PM
thanks a lot, im gonna try to implement that. :)/>/>
Gorni #4
Posted 19 February 2012 - 01:20 PM
ok tried it, the write works fine but the read has a problem. it always tells me: "unsupported format"
with exactly your code… any idea what went wrong?

edit:
forget it…i wrote 1 instead of an l in the print(filehandle:read("*l")) line…
Gorni #5
Posted 19 February 2012 - 01:51 PM
i still need some more help, to read multiple variables from a file.

for example:

copper = 1
iron = 2

local filehandle = io.open("saveddata", "w")
filehandle:write(copper)
filehandle:write(iron)
filehandle:close()

copper = 0
iron = 0

filehandle = io.open("saveddata", "r")

copper = (filehandle:read(l))
iron = (filehandle:read(???)) -- the ??? stands for i got no idea what to write.
filehandle:close()

print(copper)
print(iron)



if i save it like this it will write the 2 numbers in 1 line in the savedata file and the copper read will give me out 12.
questions are: how do i write them seprately and how do i get the corresponding stuff for each saved number?
do i need to make a file for every variable or is it possible to save/read it all in/from 1 file?
Advert #6
Posted 19 February 2012 - 01:58 PM
i still need some more help, to read multiple variables from a file.

for example:

copper = 1
iron = 2

local filehandle = io.open("saveddata", "w")
filehandle:write(copper)
filehandle:write(iron)
filehandle:close()

copper = 0
iron = 0

filehandle = io.open("saveddata", "r")

copper = (filehandle:read(l))
iron = (filehandle:read(???)) -- the ??? stands for i got no idea what to write.
filehandle:close()

print(copper)
print(iron)



if i save it like this it will write the 2 numbers in 1 line in the savedata file and the copper read will give me out 12.
questions are: how do i write them seprately and how do i get the corresponding stuff for each saved number?
do i need to make a file for every variable or is it possible to save/read it all in/from 1 file?

You're almost there!
The correct syntax for reading a filehandle is as follows:

filehandle:read("*l") -- this will read a line from the file, it will return the next line each time it is called
filehandle:read("*a") -- this will read the remainder of the file.

So, let's say you have a file with the following contents:


A
B
C
If you open it, the first filehandle:read("*l") will return A, the second will return B, and the third will return C.
If you use filehandle:read("*a"), it will return the entire file, e.g AnBnC (with newlines)
Casper7526 #7
Posted 19 February 2012 - 02:00 PM
– writing to a file
file = io.open("variables", "w")
file:write(variable1.."n")
file:write(variable2.."n")
file:close()

– reading them from a file
file = io.open("variables", "r")
variable1 = file:read()
variable2 = file:read()
file:close()
Advert #8
Posted 19 February 2012 - 02:01 PM
– writing to a file
file = io.open("variables", "w")
file:write(variable1.."n")
file:write(variable2.."n")
file:close()

– reading them from a file
file = io.open("variables", "r")
variable1 = file:read()
variable2 = file:read()
file:close()

Whoops, forgot about the necessity of needing newlines when reading!
Emong #9
Posted 19 February 2012 - 02:02 PM
All you have to do is use
filehandle:write(copper .. "n")
for each variable you write to the file.
That will make the program save them each on seperate lines so the read function will read in the numbers one at a time.

EDIT: Well, I got beaten pretty badly while I was thinking of the best way the phrase that.
Gorni #10
Posted 19 February 2012 - 02:12 PM
thx a lot everyone, now it works as intended. :)/>/>
DarkNinja2462 #11
Posted 22 February 2012 - 10:23 PM
Where exactly do I create the file?
Do I create it in /rom/
or in /rom/programs/
Advert #12
Posted 22 February 2012 - 10:34 PM
Where exactly do I create the file?
Do I create it in /rom/
or in /rom/programs/

You create it in /; /rom/* is read-only.
ScootD #13
Posted 28 April 2012 - 07:59 PM
You can save them via file writing:


local filehandle = io.open("saveddata", "w") -- open to write
filehandle:write("Hello, World!") -- Write something
filehandle:close() Very important; this tells the computer that you're done with the file, and it doesn't need to keep it open anymore.

filehandle = io.open("saveddata", "r") -- Opens for reading

print(filehandle:read("*l")) -- Reads one line
filehandle:close()

i am trying to build an email system that sends messages between client computers and servers, i am using the file writing code you posted to save the email, so far it works but i cant figure out how to save the file to a specific folder. for example, the file will be saved in the folders "email/recipient's computer number/ 'file name' "
how do i set it up so that it will save within the folders?
P.S.the recipients computer number folder will be named by a variable containing the number

EDIT: after a little more searching on the wiki i found the answer, this is how i fixed it.

fs.makeDir("email/"..to)
local filehandle = io.open("/email/"..to.."/test", "w")
filehandle:write(from.."\n")
filehandle:write(msg)
filehandle:close()
for this example the variable to is equal to 1
fs.makeDir("email/"..to) creates the directories email/1/ (because to=1)
and local filehandle = io.open("/email/"..to.."/test", "w") tells io.open that the file is located in /email/1/ and is named test
tfoote #14
Posted 07 June 2012 - 01:17 AM
Can someone give me pointers or sourse code to making an e-mail system. I am working on one myself and would like to save the e-mail(s) as a .txt file.
Questions:
1) can i do it?
2) can lua read and print the file name?
3) can lua read and print the contents?
THANKS!
Pinkishu #15
Posted 07 June 2012 - 01:51 AM
Can someone give me pointers or sourse code to making an e-mail system. I am working on one myself and would like to save the e-mail(s) as a .txt file.
Questions:
1) can i do it?
2) can lua read and print the file name?
3) can lua read and print the contents?
THANKS!

maybe you should make your own thread for that