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
➜ Wildcards as Function Parameters
|
Wildcards as Function Parameters
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Terry
USA (87 posts) Bio
|
| Date
| Sun 10 Feb 2008 08:22 PM (UTC) Amended on Sun 10 Feb 2008 08:44 PM (UTC) by Nick Gammon
|
| Message
| I was just wondering how you could make it so a wildcard could be the parname in a function. I tried this, but it didn't work:
<alias
match="^l(|ook) (n|ne|e|se|s|sw|w|nw|u|d|north|northeast|east|southeast|south|southwest|west|northwest|up|down)$"
enabled="y"
regexp="y"
send_to="12"
sequence="100"
>
<send>
function dir2 ("%1")
if "%2" == "n" then
dir = "north"
elseif "%2" == "ne" then
dir = "northeast"
elseif "%2" == "e" then
dir = "east"
elseif "%2" == "se" then
dir = "southeast"
...
end -- function
Send (look, " ", dir)</send>
</alias>
What I want to have happen is that you can type, for example, 'l n', and "look north" will be sent instead.
If someone could help me, that'd be great.
The reason why I want it as a function, is because I have three of these things that use the same directional thing.
After a while, it just gets really repetitive. :( | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Sun 10 Feb 2008 08:57 PM (UTC) Amended on Sun 10 Feb 2008 08:58 PM (UTC) by Nick Gammon
|
| Message
| What you are doing here is confusing formal and actual parameters. I am assuming you meant %2 in the function declaration, as that is the direction. Let's see how that would look, expanded out, if you typed "look north":
function dir2 ("north")
if "north" == "n" then
dir = "north"
elseif "north" == "ne" then
dir = "northeast"
elseif "north" == "e" then
dir = "east"
elseif "north" == "se" then
dir = "southeast"
...
end -- function
Having a string literal ("north") in the function declaration is wrong. What you really mean is this:
function dir2 (s)
if s == "n" then
dir = "north"
elseif s == "ne" then
dir = "northeast"
elseif s == "e" then
dir = "east"
elseif s == "se" then
dir = "southeast"
...
return dir -- return the result
end -- function
However all that does is declare the function, it doesn't call it. Then you would need to have the last couple of lines like this:
Send (look, " ", dir2 ("%2"))
This example calls the function dir2, passing the current direction (eg. "north") as the argument. Inside the function you examine the argument and decide what to substitute.
I have to say though that I don't think this has made things much simpler - unless you are planning to translate directions in lots of places. In that case the dir2 function should be in the script file (not this particular alias) so it can be shared amongst all aliases.
There is also a problem here. You haven't quoted "look" so it is treated as a variable. Thus this sends: nil north (because look is nil). You meant:
I also want to point out there is a much simpler way of achieving this in Lua. Try this:
dir_table = {
n = "north",
s = "south",
north = "north",
south = "south",
e = "east",
se = "southeast",
--- and so on
} -- end of table
Send ("look ", dir_table ["%2"])
This removes the need for a function at all. I have a table of directions (dir_table) and inside is an entry for each direction you might type (as the key) and the value for each one is the direction you actually want sent. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Terry
USA (87 posts) Bio
|
| Date
| Reply #2 on Sun 10 Feb 2008 09:05 PM (UTC) Amended on Sun 10 Feb 2008 09:09 PM (UTC) by Terry
|
| Message
|
Quote: I have a table of directions (dir_table) and inside is an entry for each direction you might type (as the key) and the value for each one is the direction you actually want sent.
Is it already built into MUSHclient? Or would I have to make one too? | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #3 on Sun 10 Feb 2008 09:17 PM (UTC) |
| Message
| | I meant, in my example. You would need to make one, like the one shown above with the extra directions fleshed out. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | 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.
16,581 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top