table.save error.

Post Reply
Lofn
Posts: 1
Joined: Wed Sep 01, 2010 12:51 pm

table.save error.

Post by Lofn »

Hi guys! I'm trying to get table.save to function properly after having issues with saveVars() and tables nested in tables, and I'm running into an error when I try to call it. For testing, I wrote up this and stuck it in a script file by itself:

Code: Select all

test_table = {"foo","bar","november 5th"}

function Load()
echo("Loading table.\n")
table.load("testtable.txt",test_table)
echo("Table loaded.\n")
end

function Save()
	echo("Saving table.\n")
	table.save("testtable.txt",test_table)
	echo("Table saved.\n")
end


When I try and use Save(), I get the following error:

Code: Select all

 [ERROR:] object:<run_code> function:<Alias27>
         <[string "--[[..."]:66: bad argument #1 to 'open' (string expected, got table)>
(for clarity, run_code is an alias that runs anything after a /)
I'm not sure what I'm doing wrong, or if it's right in front of me, or what.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: table.save error.

Post by Heiko »

table.save() and table.load() are the standard functions for user data persistency in Mudlet. They are pretty straight forward to use: Here's a little demo:
Code: [show] | [select all] lua
itemTable = {"rapier","pants","fish", nestedTableExample={"nested1", "nested2", "nested3"}}
echo("original table:\n")
display(itemTable)
table.save("test_itemTable", itemTable);

itemTable = {"foo", "bar"}
echo("table changed to:\n")
display(itemTable)
table.load("test_itemTable", itemTable)
echo("tabled restored from file to:\n")
display(itemTable)
This will get you this output:
Code: [show] | [select all] lua
original table:

table {
  1: 'rapier'
  2: 'pants'
  3: 'fish'
  'nestedTableExample': table {
    1: 'nested1'
    2: 'nested2'
    3: 'nested3'
  }
}
table changed to:

table {
  1: 'foo'
  2: 'bar'
}
tabled restored from file to:

table {
  1: 'rapier'
  2: 'pants'
  3: 'fish'
  'nestedTableExample': table {
    1: 'nested1'
    2: 'nested2'
    3: 'nested3'
  }
}
I can't comment on your alias problem, because you haven't provided any code of the alias script ;)

Lucky24
Posts: 52
Joined: Sun Sep 12, 2010 1:50 am

Re: table.save error.

Post by Lucky24 »

Just adding keywords for people searching for this:

saveVars()
remember()
remember("")
loadVars()

P.S. I've noticed other stuff in 1.1 that doesn't correspond to the manual exactly, like db:fetch_database(""), which throws a "attempt to call method 'fetch_database' (a nil value)" error in 1.1, like the above-mentioned non-working saveVars() in 1.1.

Post Reply