Quote:
I'm getting errors...
It always helps to paste the error, that narrows it down a lot. I presume this is it?
Plugin: Count_Mobs_Killed (called from world: smaug 2)
Function/Sub: exits_line called by trigger
Reason: processing trigger ""
[string "Plugin"]:60: attempt to call global 'killed_mobs' (a table value)
stack traceback:
[string "Plugin"]:60: in function <[string "Plugin"]:45>
Error context in script:
56 : WINDOW_BACKGROUND_COLOUR) -- create window
57 :
58 :
59 : WindowText (win, "f", exits, HORIZONTAL_OFFSET, VERTICAL_OFFSET, 0, 0, WINDOW_TEXT_COLOUR)
60*: WindowText (win, "f", killed_mobs" mobs killed.", HORIZONTAL_OFFSET, font_height + VERTICAL_OFFSET, 0, 0, WINDOW_TEXT_COLOUR)
61 :
62 : -- show window
63 : WindowShow (win, true) -- show it
64 :
It was basically crashing on this bit: killed_mobs" mobs killed."
At the very least, you need to concatentate them together, like this:
killed_mobs" .. mobs killed."
However even that doesn't work because killed_mobs is a table of mobs, not a single number.
I had to add some extra code to count the number, from going through the table.
Then there was the problem of getting the count to update after each kill, even if you didn't see another "Exits" line, so a bit more fiddling around got that working. Below is my amended plugin:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Tuesday, January 08, 2008, 3:26 -->
<!-- MuClient version 4.20 -->
<!-- Plugin "Count_Mobs_Killed" generated by Plugin Wizard -->
<muclient>
<plugin
name="Count_Mobs_Killed"
author="Nick Gammon"
id="f11e3bb0e48d526152798439"
language="Lua"
purpose="Counts how many mobs I have killed"
save_state="y"
date_written="2008-01-08 15:17:18"
requires="4.00"
version="1.0"
>
<description trim="y">
<![CDATA[
Counts how many mobs have been killed.
Type "show_killed" to see a count.
]]>
</description>
</plugin>
<!-- Triggers -->
<triggers>
<trigger
custom_colour="2"
enabled="y"
match="* dies."
send_to="12"
sequence="100"
>
<send>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 ()
-- update miniwindow
exits_line ("died_trigger")
</send>
</trigger>
<trigger
enabled="y"
match=" Obvious exits : *."
script="exits_line"
name="exits_trigger"
sequence="100"
>
</trigger>
</triggers>
<!-- Aliases -->
<aliases>
<alias
match="show_killed"
enabled="y"
send_to="12"
sequence="100"
>
<send>
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))</send>
</alias>
</aliases>
<aliases>
<alias
match="reset_mob_counts"
enabled="y"
send_to="12"
sequence="100"
>
<send>
killed_mobs = {} -- clear mobs table
</send>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
-- 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
-- configuration
-- font
FONT_NAME = "Lucida Console"
FONT_SIZE = 9
-- where to put the window
WINDOW_POSITION = 4 -- see below (4 is top left)
--[[
Useful positions:
4 = top left
5 = center left-right at top
6 = top right
7 = on right, center top-bottom
8 = on right, at bottom
9 = center left-right at bottom
--]]
-- colours
WINDOW_BACKGROUND_COLOUR = ColourNameToRGB ("olivedrab")
WINDOW_TEXT_COLOUR = ColourNameToRGB ("#002800")
-- offset of text from edge
HORIZONTAL_OFFSET = 5
VERTICAL_OFFSET = 2
-- here on getting an exits line
function exits_line (name, line, wildcards, styles)
-- only have exits line if called from exits_trigger
if name == "exits_trigger" then
exits = Trim (line)
end -- if
-- if no exits line, can't make the window
if not exits then
return
end -- if
width = WindowTextWidth (win, "f", exits)
-- make the window again the correct size
WindowCreate (win, 0, 0,
width + (HORIZONTAL_OFFSET * 2),
font_height *2 + (VERTICAL_OFFSET * 2),
WINDOW_POSITION, 0,
WINDOW_BACKGROUND_COLOUR) -- create window
-- count total mobs killed
local count = 0
if killed_mobs then
for k, v in pairs (killed_mobs) do
count = count + v.count
end -- for loop
end -- if table exists
WindowText (win, "f", exits, HORIZONTAL_OFFSET, VERTICAL_OFFSET, 0, 0, WINDOW_TEXT_COLOUR)
WindowText (win, "f", count .. " mobs killed.", HORIZONTAL_OFFSET, font_height + VERTICAL_OFFSET, 0, 0, WINDOW_TEXT_COLOUR)
-- show window
WindowShow (win, true) -- show it
end -- end exits_line
-- hide window on removal
function OnPluginClose ()
WindowShow (win, false) -- hide it
end -- OnPluginClose
-- hide window on disable
function OnPluginDisable ()
WindowShow (win, false) -- hide it
end -- OnPluginDisable
-- show window on enable
function OnPluginEnable ()
if exits then
WindowShow (win, true) -- show it
end -- if
end -- OnPluginEnable
-- startup stuff
win = GetPluginID () -- get a unique name
-- make the window with zero size to load the font into
WindowCreate (win, 0, 0, 0, 0, WINDOW_POSITION, 0,
WINDOW_BACKGROUND_COLOUR) -- create window
-- grab a font
WindowFont (win, "f", FONT_NAME, FONT_SIZE) -- define font
-- work out how high it is
font_height = WindowFontInfo (win, "f", 1) -- height of the font
]]>
</script>
<!-- Plugin help -->
<aliases>
<alias
script="OnHelp"
match="Count_Mobs_Killed:help"
enabled="y"
>
</alias>
</aliases>
<script>
<![CDATA[
function OnHelp ()
world.Note (world.GetPluginInfo (world.GetPluginID (), 3))
end
]]>
</script>
</muclient>
|