Nested Tables Referencing Externally

Posted by Myrilith on Thu 22 May 2008 08:31 PM — 3 posts, 16,556 views.

#0
I've hunted around to see if I could find this anywhere else, but without any luck. Hopefully y'all can help me out -- I can't figure out how to reference a table external to a function from within that function. Here's what I mean:

I have a Defenses table set up, with sub-tables of defense names, and each of these subtables contains information like the text to set it and the balance that's required. For example:



Defenses = {
	secondsight = {
		settext = {"psi super secondsight",},
		balreq = {"sup_av",},
	},
	nightsight = {
		settext = {"nightsight",},
		balreq = {"base_av",},
	},
}


Then I have another table, changed dynamically according to which defenses I want to set. So, for example, if I wanted to set my two test defenses, it would look like this:


deflist = {"nightsight","secondsight"}


And finally, I have a function that is designed to check to see if each defense is already set, and to set it if I have the appropriate balance. If not, then I make a one-shot trigger (incidentally, would it be better to have a static trigger that is enabled/disabled appropriately? I wasn't sure) to set the defense when I regain the balance. The problem is... I don't know how to reference the information contained in my external Defenses table. Here's how it looks:


function defset (name,output,wildcards)
	for index,defname in pairs (deflist) do
		if GetVariable("def_" .. defname) == 1 then 
			Note ("Defense Set: " .. defname)
			else if GetVariable(Defenses.defname.balreq) ~= nil then
				Send (Defenses.defname.settext)
			else
				Note (defname)
				-- Need Trigger Function!
				return
			end
		end
	end
end


I've tried every combination I can think of in place of the "Defenses.defname.balreq" code, but I can only get it to reference the deflist table, not the Defenses table. How do I reference that external table from within the function??
Amended on Thu 22 May 2008 08:45 PM by Myrilith
Australia Forum Administrator #1
You seem to be confused here:


GetVariable(Defenses.defname.balreq)


The table Defenses is a Lua variable, not a MUSHclient variable, thus you don't use GetVariable to access it. See http://mushclient.com/scripting and scroll down to the part titled "Using variables".

This script snippet shows how you could access your Defenses table, based on a loop of your deflist table, like you did:


Defenses = {
	secondsight = {
		settext = {"psi super secondsight",},
		balreq = {"sup_av",},
	},
	nightsight = {
		settext = {"nightsight",},
		balreq = {"base_av",},
	},
}

deflist = {"nightsight","secondsight"}

for index,defname in pairs (deflist) do

  print ("defname = " , defname, 
         "balreq = ", Defenses [defname] . balreq [1])

end -- for


Output

defname =  nightsight balreq =  base_av
defname =  secondsight balreq =  sup_av


Note that I did not use GetVariable at all, and you need to be careful when indexing nested tables. Something like this:


Defenses.defname.balreq


... access the table "Defenses", looks for a key called "defname", and inside that looks for a key called "balreq".

In your case defname is a variable, not literally "defname" so you put the variable into square brackets.

Also I had to throw in [1] at the end because you used tables for things like settext, where I am not sure you need to. Basically you only need tables if you want more than one thing. Why not:


Defenses = {
	secondsight = {
		settext = "psi super secondsight",
		balreq = "sup_av",
	},
	nightsight = {
		settext = "nightsight",
		balreq = "base_av",
	},
}


The "bottom level" of your table is just strings isn't it? You don't need yet more tables.

Australia Forum Administrator #2
Make sure you read my posting about Lua tables: http://www.gammon.com.au/forum/?id=6036