Help a noob with tables

Aquifn
Posts: 19
Joined: Fri Mar 15, 2013 8:37 pm

Help a noob with tables

Post by Aquifn »

Greetings!

I'm currently attempting to use aliases to set variables that will be saved to a table for further reference. I've kind of hit a wall as the help file doesn't really make it all that clear. So I thought someone would be willing to help me out. I'm basically learning everything from the ground up, so hopefully you won't mind treating me like a three year old. All the steps are important.

How do I use alias 1 to save variable 1, alias 2 to save variable 2, both into the same table. And the more importantly, how do I call this information in the future?

So far I've been able to make this (apparently) work the first time I set the variables. The new file appears in my profile folder and everything. But quitting mudlet and coming back...the variables go back to nil. So there could be any number of issues from me not understanding how to call the information correctly to...well anything. Would someone be willing and kind enough to step by step this for me?

Or alternatively point me at some very helpful files/examples that display this. The help files didn't help much though.

Thanks!

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Help a noob with tables

Post by Delrayne »

If you don't explicitly make the table in a script it will never show up after you close mudlet. You could set the table to nothing i.e. 'tableName = tableName or {}' and just use your aliases on start up to populate the table how you wish.

Although you could also take a look at table.save() and table.load() for references on how to do this without putting the table in a script, and if I'm not mistaken there is a ?remember?(not sure of the exact syntax) function that saves all variables etc.. in a .lua file. You'll have to wait for a more experienced member to assist on finding that function however.

Aquifn
Posts: 19
Joined: Fri Mar 15, 2013 8:37 pm

Re: Help a noob with tables

Post by Aquifn »

I have been trying to use table.save and table.load. Just incorrectly I believe.

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Help a noob with tables

Post by Delrayne »

I may have been slightly wrong then...you might need to initialize the table before you can use table.save() and table.load(). Try putting 'tableName = tableName or {}' in a script without the quotes and then using table.save() and table.load().

And just so we are clear, this is how you should use table.save()
Code: [show] | [select all] lua
table.save(getMudletHomeDir().."/tableName.lua", tableName)
Granted the '/tableName.lua' section of that can be named anything you wish. Another thing to note is that Lua IS case sensitive, meaning tablename is completely different from tableName as far as Lua is concerned

Aquifn
Posts: 19
Joined: Fri Mar 15, 2013 8:37 pm

Re: Help a noob with tables

Post by Aquifn »

I feel like you're expecting me to know some steps that I don't appear to know. I understand the correct table.save and table.load syntaxes. I just don't know the proper way to fit it all together. I learn best by example, if that helps at all :) Thanks for helping though

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Help a noob with tables

Post by Delrayne »

Step 1: click the scripts button
Step 2: click the add new button
Step 3: in the script put 'tableName = tableName or {}' without the quotes
Step 4: run you table.save/load() aliases to see if they work
Step 5: if they didn't work, open up the script editor again and click on the errors button. Copy/paste the error when you run your aliases to this thread. If the aliases worked ignore step 5.

That's your step by step friend. You can also try the irc chat room for additional help. Just be patient for a reply.

Aquifn
Posts: 19
Joined: Fri Mar 15, 2013 8:37 pm

Re: Help a noob with tables

Post by Aquifn »

Okay, so I did like you suggested. I'm coming across the same problem where it doesn't appear to actually save. The aliases all fire fine for the save part, a file is created in the profile section, but it doesn't appear to actually contain data. When I relanuch mudlet, the values are gone and the load doesn't appear to find them. The following is the code for one such exercise.

Alias which creates the table (I wish):
Code: [show] | [select all] lua
if matches[2] == "1" or matches[2] == "one" or matches[2] == "fire" then
	band = {"fire"}
		echo '\n'
		svo.echof("\nBand level set to Fire (level 1), numbers changed accordingly.")
	saveBand()
end
The function saveBand() :
Code: [show] | [select all] lua
function saveBand()
	table.save(getMudletHomeDir().."\\saveData", band)
end
I have a simple alias set up to try and load it for testing purposes. The only thing in this alias is: loadBand()
Code: [show] | [select all] lua
function loadBand()
	table.load(getMudletHomeDir().."\\saveData", band)
end

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Help a noob with tables

Post by Vadi »

Try:
Code: [show] | [select all] lua
function loadBand()
        band = band or {}
        table.load(getMudletHomeDir().."\\saveData", band)
end

Aquifn
Posts: 19
Joined: Fri Mar 15, 2013 8:37 pm

Re: Help a noob with tables

Post by Aquifn »

I think that worked. Huzzah! Now I'm trying to get it to run. The script I want it for, works perfectly if I have the variable stored in script form, but when doing it this way I get the following error:

Lua syntax error:[string "-------------------------------------------..."]:100: attempt to concatenate global 'band' (a table value)

The amusing, to me, part is that line 100 is blank. Just an empty return. But the following is the next two lines.
Code: [show] | [select all] lua
function breakpath()
if band == "none" then
now, I've tried loadBand() at the start of the scripting, and directly after function breakpath() with no change in the error. Any ideas?

User avatar
chris
Posts: 493
Joined: Fri Jun 17, 2011 5:39 am

Re: Help a noob with tables

Post by chris »

If you are trying to see if band exists, do
Code: [show] | [select all] lua
if not band then
    ....
end

Post Reply