92 posts
Posted 16 September 2014 - 04: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.
3057 posts
Location
United States of America
Posted 16 September 2014 - 04: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 on 16 September 2014 - 02:52 PM
92 posts
Posted 17 September 2014 - 04: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 on 17 September 2014 - 02:31 PM
3057 posts
Location
United States of America
Posted 17 September 2014 - 04:57 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
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.
92 posts
Posted 17 September 2014 - 06: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 on 17 September 2014 - 04:21 PM
3057 posts
Location
United States of America
Posted 17 September 2014 - 06:29 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
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()
92 posts
Posted 17 September 2014 - 06:48 PM
automagically
Would you like to teach me the magic behind it? :)/>
Like those functions, I was planning on writing something like that for tomorrow
3057 posts
Location
United States of America
Posted 17 September 2014 - 08:41 PM
92 posts
Posted 17 September 2014 - 10:01 PM
thanx