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

Saving and Recieving Data From File

Started by elfin8er, 30 October 2012 - 02:45 AM
elfin8er #1
Posted 30 October 2012 - 03:45 AM
So I've been trying to figure out for the past few days how I could save data to a file to receive at a later time. What I'm trying to do is make an adventure game where users can save their progress to a file. If possible, I would like it so that each user saves their data on a different file. Example: My file would be elfin8ersavedata. The program would then save different data on each line of the program. For an example, Line 1 would have the players ID, line 2 would have the players username, line 3 would have the users password, line 4 would have the users maxhp, line 5 would have the users hp, line 6 would have the users maxmp, line 7 would have the users mp, ect. Then, when the program needed to access these files, the program would read each line, and change a variable to match up with that line.

Am I making any sense at all? How would I do this?

I'm kinda new to computercraft, so the easier explained the better :P/>/>
Luanub #2
Posted 30 October 2012 - 04:05 AM
Check the tutorial section…..

http://www.computercraft.info/forums2/index.php?/topic/380-snippet-using-fsopen-reading-writing-from-a-file/

You'll want to use the fs and/or io API's..
ChunLing #3
Posted 30 October 2012 - 07:10 PM
If you're storing a bunch of program data, then put all of it in a table and use textutils.serialize on it before writing it to a file. Then read it back with textutils.unserialize.
local hndl = fs.open("programdata","r") if hndl then
	programdata = textutils.unserialize(hndl.readAll(programdata)) hndl.close()
else --figure out some other way to populate the table
end
-- put the part of your program that goes between loading and saving here
hndl = fs.open("programdata","w") hndl.write(textutils.serialize(programdata)) hndl.close()