PtokaX forum

Development Section => Your Developing Problems => Topic started by: Herodes on 05 June, 2004, 11:14:11

Title: Save In external .lua table
Post by: Herodes on 05 June, 2004, 11:14:11
I was wondering how to save a table like the one that follows ...  :
---  logtables.dat
LevelA = {
LevelB1 = 3,
LevelB2 = 4,
LevelB3 = "string",
LevelB4 = {},
LevelB5 = {},
LevelB6 = { [1] = "", [2] = "", [3] = "", [4] = "", [5] = "", [6] = "", [7] = "", [8] = "", [9] = "", },
LevelB7 = { [1] = "", [2] = "", [3] = "", [4] = "", [5] = "", [6] = "", [7] = "", [8] = "", [9] = "", },
};
This table would be in a separate file So that the values are saved ...
Is there some way to Save them ?
If I just do
assert(dofile("whatever.dat"), "Ur .dat is missind ...")
then I can change the values but as soon the script is restarted ... u know the story .. (it doesn't save them )

So, I'd like if any one has the time to give me some pointers ...
I've been also looking in nErBoS User Logger (http://board.univ-angers.fr/action.php?action=getlastboard&threadid=2115) and bonki's RecordBot (http://www.plop.nl/Bonki.php)
Still don't get it .. :S

[*edit*] Sry about the Title of the thread
"If its early in the morning for the most of u , for me its late at night .... very late ..." :P
The title should read :
Save table In external .dat
Title:
Post by: NightLitch on 05 June, 2004, 11:49:31
use this serialize, I got this from begining from Wombat.

-----------------------------------------------------------
function Serialize(tTable, sTableName, hFile, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(hFile, "hFile equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
write(hFile, sTab..sTableName.." = {\n" );
for key, value in tTable do
local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);
if(type(value) == "table") then
Serialize(value, sKey, hFile, sTab.."\t");
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
write(hFile, sTab.."\t"..sKey.." = "..sValue);
end
write(hFile, ",\n");
end
write(hFile, sTab.."}");
end
-----------------------------------------------------------
function SaveFile(table , tablename, file)
local hFile = openfile(file, "w");
Serialize(table, tablename, hFile);
closefile(hFile);
end
-----------------------------------------------------------
function LoadFile(file)
assert(readfrom(file),file.." is not found.")
dostring(read("*all"))
readfrom()
end

Saving:

SaveFile(table, "tablename", filename)

In your case:

SaveFile(LevelA, "LevelA", filename)


loading:


LoadFile(filename)


put this in Main()

function Main()
    LoadFile(filename)
end

/NightLitch
Title:
Post by: ((UKSN))shad_dow on 28 June, 2004, 16:57:36
lo NightLitch

can u explaine what it does ... in away a tables newbiee can understand :) thx

shad ?(
Title:
Post by: plop on 28 June, 2004, 23:35:58
QuoteOriginally posted by ((UKSN))shad_dow
lo NightLitch

can u explaine what it does ... in away a tables newbiee can understand :) thx

shad ?(
it's hard 2 explain what goes on if you don't understand tables fully.
but the moment you understand them you'll see that you understands whats going on in this function.
but it isn't needed 2 understand whats going on 2 be able 2 use this, aslong as you know how 2 trigger it 2 save things.
and nightlitch explained that perfectly.

plop