Author Topic: Save In external .lua table  (Read 1726 times)

0 Members and 1 Guest are viewing this topic.

Herodes

  • Guest
Save In external .lua table
« on: 05 June, 2004, 11:14:11 »
I was wondering how to save a table like the one that follows ...  :
Code: [Select]
---  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 and bonki's RecordBot
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
« Last Edit: 05 June, 2004, 11:21:15 by Herodes »

PtokaX forum

Save In external .lua table
« on: 05 June, 2004, 11:14:11 »

Offline NightLitch

  • Emperor
  • **
  • Posts: 1 455
  • Karma: +16/-0
(No subject)
« Reply #1 on: 05 June, 2004, 11:49:31 »
use this serialize, I got this from begining from Wombat.

Code: [Select]
-----------------------------------------------------------
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()

Code: [Select]
function Main()
    LoadFile(filename)
end

/NightLitch
//NL

Offline ((UKSN))shad_dow

  • Triple Ace
  • **
  • Posts: 213
  • Karma: +2/-0
(No subject)
« Reply #2 on: 28 June, 2004, 16:57:36 »
lo NightLitch

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

shad ?(
creator of Therapy-X? bot

Offline plop

  • Forum God
  • ****
  • Posts: 2 464
  • Karma: +37/-0
(No subject)
« Reply #3 on: 28 June, 2004, 23:35:58 »
Quote
Originally 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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

PtokaX forum

(No subject)
« Reply #3 on: 28 June, 2004, 23:35:58 »