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
➜ Scripting a multi-variable trigger
Scripting a multi-variable trigger
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Faedara
(106 posts) Bio
|
Date
| Wed 14 Jul 2010 04:52 AM (UTC) Amended on Wed 14 Jul 2010 04:54 AM (UTC) by Faedara
|
Message
| I'm making anti-theft triggers, and looking at Lua I thought, why make ten or twenty triggers, one for each piece of clothing, when I can make a single trigger for all my clothing.
However, I'm stuck on how to do this, and I feel I'm not even close to a solution.
So far I have:
^You remove (list of clothing each separated by a | )/.$
Which for obvious reasons I don't think will work. But what I need is for the script to identify which object in the list was removed and replace the wildcard in the following response with a variable appropriate to the clothing removed because the clothing descriptions and keywords are different.
wear *
I'm not entirely new to this, but I only recently opened an XML, took a good look, and realized this is simple if I can just understand things like this. |
The eternally clueless <3
Currently looking for a mentor/tutor to help me learn everything about Lua from the ground up. I can't understand 28*41 until I learn what causes 2+2. | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #1 on Wed 14 Jul 2010 06:12 AM (UTC) |
Message
| You can insert a MUSHclient variable into a pattern field using the syntax @!variable. The ! is a special flag on the normal @variable that tells it to preserve the |'s in the variable instead of escaping them, which lets you do:
^You remove (@!protected_clothes)\.$
(You used /. above instead, which probably isn't what you want. Make sure to use the right kind of slash!)
Then you just have to manage the MUSHclient variable "protected clothes". I think the easiest way to use this is to use a MUSHclient array (ArrayCreate() and similar functions) with the clothes as the key (not the value). Then you can use ArrayExportKeys() to get a | delimited string you can use in the variable in the regexp.
If any of that didn't make sense, I'd be glad to give examples. Just a little bit too harried at this particular moment. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| Faedara
(106 posts) Bio
|
Date
| Reply #2 on Wed 14 Jul 2010 06:19 AM (UTC) |
Message
| I'd love an example please. My "simple" just flew right out the window. And since you seem to be busy I'll research what you just said while you're away. |
The eternally clueless <3
Currently looking for a mentor/tutor to help me learn everything about Lua from the ground up. I can't understand 28*41 until I learn what causes 2+2. | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #3 on Wed 14 Jul 2010 06:27 AM (UTC) Amended on Wed 14 Jul 2010 06:28 AM (UTC) by Nick Gammon
|
Message
| What he means is, that if the variable "protected_clothes" contains something like:
Then the code he gave will get MUSHclient to substitute that into a regular expression, effectively testing for hat OR pants etc.
However another approach is to simply match on:
(This is NOT a regular expression).
Now in the script wildcard 1 is whatever got removed, and you can do a table lookup on it.
eg.
clothing = {
hat = "You put the hat back on",
tie = "You wear your tie again",
shoes = "You put your shoes on",
["your cane"] = "You wield your cane",
-- more here
} -- end of table
action = clothing ["%1"]
if action then
Send (action)
end -- if
So we lookup in the clothing table for a match on wildcard 1, and if found (ie. action is not nil) then we get back some value which we do something with.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Faedara
(106 posts) Bio
|
Date
| Reply #4 on Wed 14 Jul 2010 06:40 AM (UTC) |
Message
| You don't sleep much, do you Nick?
And the problem with that is, as I mentioned earlier, the description when taking it off is *not* the same as the item identifier.
So if I take off "a canvas backpack" the only word I need and can use to put it back on is either pack or backpack, "wear a canvas backpack" would have no effect. |
The eternally clueless <3
Currently looking for a mentor/tutor to help me learn everything about Lua from the ground up. I can't understand 28*41 until I learn what causes 2+2. | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #5 on Wed 14 Jul 2010 06:41 AM (UTC) Amended on Wed 14 Jul 2010 06:43 AM (UTC) by Twisol
|
Message
|
Faedara said:
I'd love an example please. My "simple" just flew right out the window. And since you seem to be busy I'll research what you just said while you're away.
I might draft a simple reusable function to keep the complexity hidden, but here's the basic idea. (Nick's got a good one, too, and if it fits your situation, use it! Sometimes you just need to match only on specific things though, and this makes it a little easier.)
Put this somewhere outside your trigger, ideally your script file, so it's run immediately when the world is loaded.
local function keys_to_list(tbl)
local tmp = {}
for k,_ in pairs(tbl) do
table.insert(string.gsub(k, "|", "\\|")
end
return table.concat(tmp, "|")
end
protected_clothes = {}
function add_clothes(name, keyword)
protected_clothes[name] = keyword
SetVariable("protected_clothes", keys_to_list(protected_clothes))
end
function remove_clothes(name)
protected_clothes[name] = nil
SetVariable("protected_clothes", keys_to_list(protected_clothes))
end
Then you can call add_clothes and remove_clothes, which automatically update the protected_clothes variable for you. I used a Lua table simply because I'm in that sort of mindset right now, but a MUSHclient Array would provide ArrayExportKeys(), which does the job of my keys_to_list() function itself.
It also adds the keyword as the value, so you can do something like Send("wear " .. protected_clothes["%1"]) to get at it. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #6 on Wed 14 Jul 2010 07:02 AM (UTC) |
Message
|
Faedara said:
You don't sleep much, do you Nick?
It's only 5 pm here. :)
Faedara said:
And the problem with that is, as I mentioned earlier, the description when taking it off is *not* the same as the item identifier.
Well my example was trying to show you might match on "hat" but send "put on my cap" - the match word, and what you do with it, are only linked in the sense that they are together in the table.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Faedara
(106 posts) Bio
|
Date
| Reply #7 on Wed 14 Jul 2010 07:19 AM (UTC) |
Message
| I see... it'll take me some time then, but I should be able to use that to figure out what I'm doing.
And I'm glad I'm a night person then, it's 3:18am here. |
The eternally clueless <3
Currently looking for a mentor/tutor to help me learn everything about Lua from the ground up. I can't understand 28*41 until I learn what causes 2+2. | Top |
|
Posted by
| Faedara
(106 posts) Bio
|
Date
| Reply #8 on Fri 06 Aug 2010 11:20 PM (UTC) |
Message
| New piece to this question, I'm using that variable table you gave me Nick, but I don't know how to change where it sends the command. Sending the command directly to the client skips command stacking, so I want to send it to the command line instead. ((No longer for clothing, this is now for a combo type alias)) |
The eternally clueless <3
Currently looking for a mentor/tutor to help me learn everything about Lua from the ground up. I can't understand 28*41 until I learn what causes 2+2. | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #9 on Fri 06 Aug 2010 11:30 PM (UTC) |
Message
| Try using Execute() instead of Send(). |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| Faedara
(106 posts) Bio
|
Date
| Reply #10 on Fri 06 Aug 2010 11:36 PM (UTC) |
Message
| That's exactly what I needed, thanks |
The eternally clueless <3
Currently looking for a mentor/tutor to help me learn everything about Lua from the ground up. I can't understand 28*41 until I learn what causes 2+2. | Top |
|
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.
28,115 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top