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

How to save variables extremely efficiently - ProgSave

Started by Geforce Fan, 17 May 2013 - 04:29 PM
Geforce Fan #1
Posted 17 May 2013 - 06:29 PM
Ever got frustrated with the large ammounts of files you need for a table, let's say? Well, you don't need to be frustrated anymore. This tutorial will teach you how to save in ProgSave!
What is ProgSave? ProgSave works by actually making a program that sets the variables you need. Here's an example


health = 100
score = 0
psave = fs.open("savegame", "w") ;
psave.writeLine("health = "..health.." score = "..score") ;
psave.close()
So as you can see, this actually makes a PROGRAM that sets the variables, hence the name ProgSave.
And to load the variables, all you have to do is…

shell.run('savegame') ;
Now here's a more advanced piece of code, one that deletes the save file if it exists before saving to prevent a crash.



health = 100
score = 0
if fs.exists("savegame") == true then
  fs.delete("savegame") ;
end
psave = fs.open("savegame", "w") ;
  psave.writeLine("health = "..health.." score = "..score") ;
psave.close()
Now, we step it up even more and allow the user to run the save to launch the program, but also launch it within the program

loadingfromgame = true
health = 100
score = 0
if fs.exists("savegame") == true then
  fs.delete("savegame") ;
end
psave = fs.open("savegame", "w") ;
  psave.writeLine("health = "..health.." score = "..score if loadingfromgame = true then shell.run('game') end ") ;
--note: you cannot use the same string there as the string you used  in writeLine.
psave.close()
loadingfromgame = false


Of corse, this doesn't cover tables. That's what this half of the tutorial is about
In this example, we will have a table of an inventory with a stone sword and some dirt.

local inv = {
[1] = "stonesword",
[2] = "dirt",
[3] = "none",
} ;
--now to save this
psave = fs.open("savegame", "w") ;
  psave.writeLine("local inv = { [1] "..inv[1]..", [2] "..inv[2].." [3] "..inv[3].." } ;") ;
psave.close()
That's about all
Tell me if there are any errors or typos in my program.
Also please feel free to ask questions if you don't understand something in this post. You can also PM me.
D3matt #2
Posted 18 May 2013 - 02:11 AM
Or you could just serialize a table?
Geforce Fan #3
Posted 18 May 2013 - 11:18 AM
Or you could just serialize a table?
but this tutorial is for ProgSave :(/>
Pharap #4
Posted 18 May 2013 - 11:31 PM
It's ok, but I would say it needs work.
I won't list the problems with it though.
I will however suggest you look up the .ini file format and ponder why I think using an ini format would be more manageable.
jesusthekiller #5
Posted 19 May 2013 - 04:58 PM
Yup. And notice that this method makes global variables. And global = bad.
D3matt #6
Posted 20 May 2013 - 01:40 AM
Or you could just serialize a table?
but this tutorial is for ProgSave :(/>
A tutorial to write your program? You didn't really explain how it works, if you explained how it worked it might be more fitting. Otherwise I think it would be better suited to the Programs section since that's pretty much what it is.
LBPHacker #7
Posted 20 May 2013 - 08:37 AM
Don't forget that you can load that kind of config files into a local table with this method:
local loadConfig = function(path)
    local func, err = loadfile(path)
    if not func then return false, err end
    local env = {}
    setfenv(func, env)()
    return env
end

local crapsInConfig = loadConfig("crapconfig")

Actually, that's how os.loadAPI works.
Geforce Fan #8
Posted 16 June 2013 - 03:42 PM
Yup. And notice that this method makes global variables. And global = bad.
What's wrong with global variables?
Lyqyd #9
Posted 16 June 2013 - 03:53 PM
This tutorial does not meet the minimum expectations of quality we have for tutorials. In addition, this tutorial does not cover a useful tutorial subject, but instead seems to be a promotional piece for one of the author's ideas, written in a tutorial-like style.

Locked and moved to General.