Jump to content




Saving and Recieving Data From File


  • You cannot reply to this topic
2 replies to this topic

#1 elfin8er

  • Members
  • 133 posts

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/>

#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 30 October 2012 - 04:05 AM

Check the tutorial section.....

http://www.computerc...ng-from-a-file/

You'll want to use the fs and/or io API's..

#3 ChunLing

  • Members
  • 2,027 posts

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()






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users