Edit didn't see you guys were all ready talking on it I am a very slow typist .
Well belidos you had to ask didn't you!
Hear we go and may god have mercy on your mind.
What you need is a stripped down version of gamedata.lua most of gamedata.lua is to deal with the fact most of the stuff that starts with g_ that Lee uses is not saved as it changes between runs. plus that there was no way to tell what variables people would create.
the simplest way is to put the data you want is to throw up to the next level under one table.
g_stuff_to_throw_up = {}
g_stuff_to_throw_up[water] = 2 -- extra
then you can use something like this with out worrying about changing it when you think of new stuff to keep. and because it the table starts with g_ it will be preserved in the normal in level saves.
local file = assert(io.open("belidos_stuff", "w"))
if file ~= nil then
save("g_stuff_to_throw_up",_G[g_stuff_to_throw_up]) -- this will save g_stuff_to_throw_up and everything it contains see ***i
file:close()
end
to save the data you of cause will need the save routine.
***if you don't want to put it all in one table just add additional saves after the first
function bS (o) -- covert to safe strings
if type(o) == "number" then
return tostring(o)
else -- assume it is a string
return string.format("%q", o) -- using %q rather than %s to cater for embended stuff
end
end
function save (name, value, saved) -- based on example in manual
saved = saved or {} -- initial value
file:write(name, "=")
if type(value) == "number" or type(value) == "string" then
file:write(bS(value), "\n")
elseif type(value) == "table" then
if saved[value] then -- value already saved?
file:write(saved[value], "\n") -- use the previous name
else
saved[value] = name -- save name use next time
file:write("{}\n") -- create new table
for k,v in pairs(value) do -- save its fields
local fieldname = string.format("%s[%s]", name, bS(k)) -- create new base entry
save(fieldname, v, saved) -- recursion be very careful
end
end
else
file:write("Error ".. name," -- cannot save a " .. type(value).."\n") -- this will fail as bad line on load
end
end
The reason for the _G is tha lua stores "everything" in a metatable call _G
Warning :The save function is recursive and may cause bleeding from your ears when you try to follow it.
If you use this approach you will end up with a file full of assign commands which in theory you should be able to reload with a single dofile call.
I say in theory as I have failed to get it to work that way so I process one line at a time like this.
local file = assert(io.open(""belidos_stuff" , "r")) -- open the file for read and assign it to file
If file
funkstring= file:read() -- read the fist line
while funkstring
funk = load(funkstring) -- convert the line into an executable function
if funk and type(funk) == "function" then funk() end -- check the load returned something and it is a function then execute it
funkstring= file:read() -- read the next line
end
file:close()
end
You will notice the load technique I use does not require you to code the variable name the save save the name and contents for you. If I got it right this if executed at the beginning of a level will not do anything if the file is missing.
I do not guarantee this will work if you put it in verboten as I wrote most of this off the top of my head bar the save function which I grabbed from gamedata.lua and tweeked to make fit.
There are other ways not using the save function, and if you want to use the save function without it all to one table you can make multiple calls to it.
QED
Important notes
I do not guarantee this will work if you put it in verboten as I wrote most of this off the top of my head bar the save function which I grabbed from gamedata.lua and tweaked to make fit.
Be aware the load function is built into lua and actually turns a string into an executable function it can contain anything that a function can but it works outside the normal scoping rules.
The first parameter in save defines the base name used in the restore , the second the actual base variable it grabs the data from to save. If you want to be overly clever you can restore to a different name than you save from provided you specified the change in the save.
Quod cito fit, cito perit.