This tutorial will show, with screen captures, the general process involved in making a script to count something useful (like the number of times you have killed various mobs), and how to turn that into a plugin.
We will start by getting an example message up from the MUD, and then Shift+Double-click to select the entire line:
It is best to copy an actual message so we get the spelling right, and the spacing. The next thing to do is make a trigger that matches this message, so we press Alt+Enter to enter world configuration, and select the Trigger section, and click Add to make a new trigger:
Now we paste in the death message as the trigger match field:
However the mob name will vary, so we need to replace its name with an asterisk, to make it a wildcard. Then to make sure we see it matching I changed the colour to Custom2, and the "send to" field to "Script". Finally we click on the little button that lets us edit the script in the "Send" box:
Inside the send text, a small Lua script is used to count the number of mobs we have seen killed. The first line makes the table "killed_mobs" if it doesn't already exist. Then we add this mob to the table, if it isn't there already. After that we add 1 to the count, and make a note of the time it was killed.:
If you want to copy and paste the code, here it is in text form:
killed_mobs = killed_mobs or {} -- make mobs table
mob_name = "%1" -- this mob's name (first wildcard)
-- add this mob if first time
killed_mobs [mob_name] = killed_mobs [mob_name] or { count = 0 }
-- add 1 to count of mobs
killed_mobs [mob_name].count = killed_mobs [mob_name].count + 1
-- remember when we last killed it
killed_mobs [mob_name].last_time = os.time ()
Next, to test it we kill a few mobs, and see if the script is working. First we should see the "death" line in yellow, as that shows the trigger has matched. To see if the Lua table is being built correctly I make sure that the scripting prefix is a slash (and to help with error messages I checked "note errors"):
Now we can use a slash as a script prefix to load the "tprint" module:
With tprint (table print) loaded we can use it to print the killed_mobs table:
So far, so good. The table is showing the mobs I killed, how many times, and when. Let's make an alias to display the information neatly. First go into world configuration and add an alias:
I will make up a name for it (show_killed) but you can call it anything you like. It will call a script so I change the "send to" field to "script" (like for the trigger) and click the button to edit the script field:
Now we make a small script that goes through each entry in the table, and lists the mob name, count and date formatted nicely:
If you want to copy and paste the code, here it is in text form:
if not killed_mobs or next (killed_mobs) == nil then
ColourNote ("white", "blue", "No mobs killed yet")
return
end -- if nothing
-- go through each one
count = 0
for k, v in pairs (killed_mobs) do
Note (string.format ("%%-30s x %%i (last at %%s)",
k,
v.count,
os.date ("%%H:%%M %%d %%b %%Y", v.last_time)))
count = count + v.count
end -- for loop
-- show total
Note (string.format ("%%5i mobs killed.", count))
I have doubled the number of % symbols used in string.format and os.date because in the "send" box MUSHclient tries to treat % followed by something as a wildcard, so you need to use %% for a single % to be sent to the script engine. Once we have got our alias entered we can test it:
Time to turn it into a plugin. First I use the File menu to start the Plugin Wizard. First we enter the plugin name, purpose, author and required version of MUSHclient:
Next we put in a simple description, in case the player clicks on the "Show Info" button in the plugins list:
Next, we move to the Triggers tab. In my case there is only my trigger there, but if you had others you would click "Select None" and then click to select any required triggers:
Now, we move to the Aliases tab. In my case there is only my alias there, but if you had others you would click "Select None" and then click to select any required aliases:
I skipped the Timers tab, as this script does not use timers, and move onto Variables. An important thing to do here is check "Retain State" as this lets the plugin save and restore variables, which is how we remember the mob count from one session to the next:
Finally we move to the Script tab. I don't use any "standard constants" so "Include Standard Constants" can be unchecked. Then click Edit to add a bit more scripting:
What we need to do now is add two functions: OnPluginInstall and OnPluginSaveState. The first is called when the plugin is loaded, the second when the plugin is saving its state file. We "require" the Serialize module, which is a helper module for saving the plugin's variables as a string. Then we make an empty killed_mobs table, in case this is the first time the plugin is run. Finally we use loadstring to convert any variables serialized earlier into the Lua table. At plugin save time we use serialize.save_simple to convert the Lua table back into a string:
If you want to copy and paste the code, here it is in text form:
-- on plugin install, convert variable into Lua table
function OnPluginInstall ()
require "serialize" -- needed to serialize table to string
killed_mobs = {} -- ensure table exists, if not loaded from variable
assert (loadstring (GetVariable ("killed_mobs") or "")) ()
end -- function OnPluginInstall
-- on saving state, convert Lua table back into string variable
function OnPluginSaveState ()
SetVariable ("killed_mobs", "killed_mobs = " ..
serialize.save_simple (killed_mobs))
end -- function OnPluginSaveState
If you check out the state file (double-click the RH mouse button on the plugin name in the plugins list) after using the plugin you will see something like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Tuesday, January 08, 2008, 3:42 -->
<!-- MuClient version 4.20 -->
<!-- Written by Nick Gammon -->
<!-- Home Page: http://www.mushclient.com/ -->
<!-- Plugin state saved. Plugin: "Count_Mobs_Killed". World: "Realms of Depair". -->
<muclient>
<!-- variables -->
<variables
muclient_version="4.20"
world_file_version="15"
date_saved="2008-01-08 15:42:07"
>
<variable name="killed_mobs">killed_mobs = {
["A large grey rat"] = {
count = 1,
last_time = 1199767236,
},
["A Fire Ant"] = {
count = 1,
last_time = 1199767275,
},
}</variable>
</variables>
</muclient>
Now we can hit the Create button to make our new plugin:
We can accept the suggested name for the plugin:
Now to test it! Go to the File Menu, select Plugins, and you will see something like this:
Choose our newly created plugin file:
It now appears in the list of plugins:
We can now type "show_killed" - this time it should report "No mobs killed yet" because each plugin has its own script space, separate from the main world script space. However we can soon kill a few more mobs and the show_killed alias should now work. Also we can close and re-open the world and it will remember the mobs we killed last time, thanks to the script to save and restore the killed_mobs table.
|