Jump to content




need suggestions on a proper way of saving A LOT ov variables


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

#1 blipman17

  • Members
  • 92 posts

Posted 16 September 2014 - 03:03 PM

Hi,

I just wanted to know if there was any way to save tables, and other variables the proper way in computercraft.
in case the server goes down or something.

I've been looking at JSON API for CC, but still...
Does anyone have a neat way of saving data?
If needed, I'll write my own.
I just want to know I'm doing it the right way.

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 16 September 2014 - 03:51 PM

Serialize/unserialize a table and write/read it to/from a file.
local tbl = { ["username"] = "password" } --#just a small example table
--#saving the table
local file = fs.open( "config", "w" ) --#open the file "config" in write mode
file.write( textutils.serialize( tbl ) ) --#write the table to the file
file.close() --#save your changes and close the file
--#loading the table
local file = fs.open( 'config', 'r' ) --#open the file 'config' in read mode
local tbl = textutils.unserialize( file.readAll() ) --#create a table from the data inside the file
file.close() --#close the file

Feel free to ask any questions about this method.

Edited by KingofGamesYami, 16 September 2014 - 03:52 PM.


#3 blipman17

  • Members
  • 92 posts

Posted 17 September 2014 - 03:27 PM

Seems like a neat way. Much cleaner than using any funky API.

Just one question about using
textutils.serialise(data)
Would that give a readable format?

And, do you need to read the whole file, unserialise the wole thing, add something, serialise and overwrite the old one?
Just to add one thing?
Seems to me like a lot of hassle, expecially if you have a large database

Edited by blipman17, 17 September 2014 - 03:31 PM.


#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 September 2014 - 03:57 PM

View Postblipman17, on 17 September 2014 - 03:27 PM, said:

Seems like a neat way. Much cleaner than using any funky API.

Just one question about using
textutils.serialise(data)
Would that give a readable format?

And, do you need to read the whole file, unserialise the wole thing, add something, serialise and overwrite the old one?
Just to add one thing?
Seems to me like a lot of hassle, expecially if you have a large database

Not exactly. When your program starts, read the file, unserialize the table, and set whatever variable you want. From then on, when you add a value to the table, all you have to do is write the serialized new version to the file. You could even use metatables to do this automagically, but that's an advanced topic...

Also, textutils.serialize has changed throughout the versions. It gives a string, which is human-readable. In 1.63 it actually saves the table like this:
{
  ["key"] = "variable",
}
Which is extremely easy to read.

#5 blipman17

  • Members
  • 92 posts

Posted 17 September 2014 - 05:21 PM

Then i'm going to write a few small functions for that. Since I want to have some API's I got to save data constantly.
Some turtle navigation stuff and such which should be crash safe.
Also , i want different computers to save their info also, on every computer, at any possible time

Edited by blipman17, 17 September 2014 - 05:21 PM.


#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 September 2014 - 05:29 PM

View Postblipman17, on 17 September 2014 - 05:21 PM, said:

Then i'm going to write a few small functions for that. Since I want to have some API's I got to save data constantly.
Some turtle navigation stuff and such which should be crash safe.
Also , i want different computers to save their info also, on every computer, at any possible time
Have fun... I attempted something like that once.

I suggest placing configurable variables inside a table, ei:
local config
--#load the config
local file = fs.open( "config", "r" )
config = textutils.unserialize( file.readAll() )
file.close()
local function saveConfig()
  local file = fs.open( "config", "w" )
  file.write( textutils.serialize( config )
  file.close()
end

--bla bla bla, something is modified
saveConfig()


#7 blipman17

  • Members
  • 92 posts

Posted 17 September 2014 - 05:48 PM

KingofGamesYami said:

automagically

Would you like to teach me the magic behind it? :)

Like those functions, I was planning on writing something like that for tomorrow

#8 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 September 2014 - 07:41 PM

To get you started:
http://lua-users.org...methodsTutorial
Some more stuff:
http://lua-users.org...MetatableEvents

#9 blipman17

  • Members
  • 92 posts

Posted 17 September 2014 - 09:01 PM

thanx





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users