Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Nested Tables Referencing Externally
Nested Tables Referencing Externally
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Myrilith
(5 posts) Bio
|
Date
| Thu 22 May 2008 08:31 PM (UTC) Amended on Thu 22 May 2008 08:45 PM (UTC) by Myrilith
|
Message
| 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?? | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #1 on Thu 22 May 2008 09:24 PM (UTC) |
Message
| 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:
... 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.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #2 on Thu 22 May 2008 09:58 PM (UTC) |
Message
| |
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
12,190 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top