MUSHclient complex alias script
Updated 15th June 1997.
Introduction
This example script assumes you have read the companion article: Scripting a simple aliasThe main drawback with the simple example is that you have to amend the script every time you want to add a new location to teleport to.
This example shows how to store teleport locations as a MUSHclient "variable", so that you can add new locations "on-the-fly". As the variables are stored in the world file they are saved when you save the world, and available next time you use that world.
Typing in the script subroutine
Use Notepad (or some other text editor) to edit the script file (the file whose name appears on the "scripts" configuration page) and add the following subroutine (you can copy from this web page and paste into the file). Both subroutines below are supplied in the file "exampscript.vbs" which accompanies MUSHclient.sub OnTeleport (thename, theoutput, thewildcards)
dim sDestination
dim sRoomList
dim sHelp
dim iSubscript
dim iRoom
sDestination = Trim (thewildcards(1))
' if nothing entered echo possible destinations
if sDestination = "" then
world.note "-------- TELEPORT destinations ----------"
' find list of all variables
sRoomList = world.GetVariableList
if not IsEmpty (sRoomList) then
' loop through each variable, and add to help if it starts with "teleport_"
for iSubscript = lbound (sRoomList) to ubound (sRoomList)
if Left (sRoomList (iSubscript), 9) = "teleport_" then
if sHelp <> "" then
sHelp = sHelp & ", "
end if
sHelp = sHelp & Mid (sRoomList (iSubscript), 10)
end if ' variable starts with "teleport_"
next ' loop through sRoomList
end if
' if no destinations found, tell them
if sHelp = "" then
sHelp = "<no rooms in teleport list>"
end if ' no destinations found in list
world.note sHelp
exit sub
end if ' no destination supplied
' get contents of the destination variable
iRoom = world.GetVariable ("teleport_" & lCase (sDestination))
' if not found, or invalid name, that isn't in the list
if IsEmpty (iRoom) or IsNull (iRoom) then
world.note "******** Destination " & sDestination & " unknown *********"
exit sub
end if
world.note "------> Teleporting to " & sDestination
world.send "@teleport #" & cstr (iRoom)
end sub
Interestingly, although this version is much more flexible, it is only about 10 lines longer than the simple version. What is happening is:
- The third parameter passed into the script subroutine (called "thewildcards" here, but you could call it anything) is the array of wildcards to the alias, in other words "whatever the user typed after the word TELEPORT".
- This first wildcard ("thewildcards (1)") is "trimmed" by using the "Trim" function. This removes leading and trailing spaces, and moved into the variable "sDestination" in the process.
- If the destination is blank (ie. the user typed nothing in) then the program
echoes the possible teleport locations, and exits the subroutine. It does this
by:
- Getting the contents of all variables by calling "world.getvariablelist"
- Looping through each variable, and seeing if it starts with "teleport_"
- If so, it is added to the variable "sHelp", with a comma before each room name except the first
- If none were found, an explanatory message is moved in
- The contents of "sHelp" are displayed
- The subroutine exits
- Otherwise, using "world.getvariable" the contents of the appropriate variable is fetched
- If there is no match, a warning message is displayed (using world.note) and the subroutine exits.
- If there is a match, the subroutine displays an informative message, and then sends the appropriate teleport command to the world.
Linking the alias to the script
The only thing that remains is to add an alias called "teleport" (you could make it "tel" if you want to save some typing), and tell it to call the script subroutine "OnTeleport" (ie. the one you just created) if someone types "teleport".The resulting alias will look like this:
- The "alias" field is what is matched on to trigger the alias. You can make it "tel" if you like, or "TEL", remembering that aliases are case-sensitive.
- It is important to type the alias exactly as:
so MUSHclient knows to send the rest of what you type after the word "teleport" to the script subroutine.^teleport(.*)$ - Leave the "send" area empty - you want the script to send things to the world, not the alias directly.
- Aliases that call scripts must have a label, it doesn't matter what it is, as long as it is unique.
- In the "script" box type the name of the subroutine that was keyed into the VBscript file.
- You can see from the example above that MUSHclient maintains a count of how many times a script was called. You can see that I tested the above script 8 times in the course of writing this article.
Making another alias to add rooms to the list
The next trick is to add rooms to our list of rooms that we can teleport to. To do this, I have defined another VBscript subroutine "OnAddTeleport" which is called from an alias "add_teleport". This subroutine expects you to type in a room name followed by its number, like this:add_teleport RocketShip 3321The subroutine uses two wildcards to allow you to type in two words separated by a space. It does a couple of checks (eg. that the second word is a number) and then calls "world.setvariable" to add it to the variables list. The subroutine looks like this:
sub OnAddTeleport (thename, theoutput, thewildcards)
dim sDestination
dim iRoom
dim iStatus
dim sCurrentLocation
' wildcard one is the destination
sDestination = Trim (thewildcards (1))
' if nothing entered tell them command syntax
if sDestination = "" then
world.note "Syntax: add_teleport name dbref"
world.note " eg. add_teleport LandingBay 4421"
exit sub
end if
' wildcard 2 is the room to go to
iRoom= Trim (thewildcards (2))
if not IsNumeric (iRoom) then
world.note "Room to teleport to must be a number, you entered: " & iRoom
exit sub
end if
' add room and destination location to variable list
iStatus = world.SetVariable ("teleport_" & sDestination, iRoom)
if iStatus <> 0 then
world.note "Room name must be alphabetic, you entered: " & sDestination
exit sub
end if
world.note "Teleport location " & sDestination & "(#" _
& iRoom & ") added to teleport list"
end sub
Add the second alias
You will now need to add an alias which matches on "add_teleport" with a script subroutine of "OnAddTeleport", using a very similar method to the one for adding the "teleport" alias.
In this case, however, you need to match on:
add_teleport * *
The first wildcard is the room name, the second wildcard is the room number.
Test it out!
Having typed in the script, and set up both aliases, you are ready to try them! First type in:
add_teleport bazaar 34
And then type in:
teleport bazaar
Here is what it looked like for me...
If you get syntax errors, just edit the script file and correct them. If you are using Window NT, MUSHclient will automatically detect that you have changed the script file and offer to reprocess it. If you are using Windows 95, just use the "reload script file" function (Shift+Ctrl+R).
Once you have added a couple of teleport destinations you can look at them in the variables configuration screen. You can also use that screen to change or delete destinations. Here is an example: