Scripting problem

Posted by Starky on Sat 22 Dec 2007 08:26 AM — 3 posts, 13,573 views.

#0
Alright I'm writing a script for Aardwolf just a very simple vorpal counter it works fine when set up in the client but when trying to put it into a plugin format for clannies Im running into a strange problem with * and %1 that has effectively stumped the late night crew on tech channel.

Everything in the script works fine except for capturing the name of the vorpalled mob and reporting it back in the string. The %1 returns a blank space instead of the mob name. Replacing the %1 with a setvariable and getvariable does nothing still returns a blank space. Defining a variable and having it getvariable and calling it with @variable returns the default string set to the variable in the script. Anyways enough with the rambling I'll put a copy of the plugin script here so you can take a looksy.


<plugin
	name="VorpalCounter"
	author="Starky"
	id="a234cd0873b23a1f234e826c"
	language="lua"
	purpose="Simple vorpal counter which tallies per mort and total"
	date_written="2007-12-22 02:24:45"
	save_state="y"
	>
</plugin>

<variables>
	<variable name="VorpChan">gt</variable>
	<variable name="VorpCount">0</variable>
	<variable name="VorpTotal">0</variable>
</variables>

<aliases>
	<alias
	match="vorpchan *"
	enabled="y"
	send_to="12"
	sequence="100"
	>
	<send>SetVariable ("VorpChan", "%1" )
	</send>
	</alias>
	<alias
	match="vorpreset"
	enabled="y"
	send_to="12"
	sequence="100"
	>
	<send>SetVariable ("VorpCount", "0")
	</send>
	</alias>
</aliases>

<triggers>
	<trigger
	enabled="y"
	group="Vorpal"
	match="Your weapon glows brightly and decapitates *"
	regexp="y"
	expand_variables="y"
	send_to="12"
	sequence="100"
	>
	<send>SetVariable ("VorpVictim", "%1")
SetVariable ("VorpCount",tonumber(GetVariable("VorpCount") +1))
SetVariable ("VorpTotal",tonumber(GetVariable("VorpTotal") +1))
SendNoEcho(GetVariable ("VorpChan") .. " I just vorpalled " .. 
  GetVariable("VorpVictim") .. "! Which is " .. 
  GetVariable("VorpCount") .. " total vorpals this remort, and " .. 
  GetVariable("VorpTotal") .. " since added on Dec 12th 2007!")
	</send>
	</trigger>
</triggers>	

the setvariable of VorpVictim was a test after the original * and %1 didn't work the original code had no VorpVictim variable and had just a %1 in the quotes of said text on the channel. This had no different effect from the code above both post a blank character


said code I am testing using the echo command on aardwolf

Quote:

echo Your weapon glows brightly and decapitates test
Your weapon glows brightly and decapitates test

2499/2499 100%hp 1984/1984 100%mn 2205mv 4291tnl 127626gol 0tell 0quest (Reply: Marcellous)
(Group) Mad Hermit Starky: 'I just vorpalled ! Which is 4 total vorpals this remort, and 4 since added on Dec 12th 2007!'


PS: as a side note thanks in advance but Im going to bed here soon and leave on vacation later today so I most likely wont be able to post with feedback on how this worked or not for about a week.
Amended on Sat 22 Dec 2007 07:38 PM by Nick Gammon
Australia Forum Administrator #1
OK, let's run through the various problems here. :)

Below is my amended version, which works correctly.

  • Your two aliases, whilst working correctly, can be simplified to use "send to variable" which replaces the small script with the inbuilt ability to set variables directly without scripting.
  • It is a trap to use these lines in the plugin:

    
    <variables>
    	<variable name="VorpChan">gt</variable>
    	<variable name="VorpCount">0</variable>
    	<variable name="VorpTotal">0</variable>
    </variables>
    


    Because of the plugin initialization sequence, the variables are loaded from the state file and then overwritten by the above lines, which means the vorpal count will always be zero each time the plugin is loaded. I have omitted them and replaced them with the following lines in the Lua script section, which only initializes them if not nil:

    
    -- initialize variables first time
    var.VorpChan = var.VorpChan or "gt"
    var.VorpCount = var.VorpCount or "0"
    var.VorpTotal = var.VorpTotal or "0"
    

  • You can save a lot of GetVariable and SetVariable wordiness by using the "var" module, which uses a Lua metatable to simulate converting MUSHclient variables to/from Lua variables. Thus:

    
    SetVariable ("VorpCount",tonumber(GetVariable("VorpCount") +1))
    


    becomes:

    
    var.VorpCount = var.VorpCount + 1
    


    This is much easier to read.
  • You had a misplaced bracket on that line:

    
    SetVariable ("VorpCount",tonumber(GetVariable("VorpCount") +1))
    


    I think you meant to get the variable, convert it to a number, add 1, and put it back, but you were getting the variable, adding 1, and then converting it to a number. It should have read:

    
    SetVariable ("VorpCount",tonumber(GetVariable("VorpCount")) +1)
    


    However Lua is smart enough to convert strings to numbers anyway, if you are adding them, so the tonumber is redundant anyway, and it could be written:

    
    SetVariable ("VorpCount", GetVariable("VorpCount") + 1)
    

  • Your main problem, that the vorpal name was blank, was because you had made this a regular expression:

    
    match="Your weapon glows brightly and decapitates *"
    


    You probably meant that to not be a regular expression, so I removed the 'regexp="y"' part. If you really want to make it a regular expression it should have read:

    
    match="^Your weapon glows brightly and decapitates (.*)$"
    


    Note the brackets around the wildcard part, without them all wildcards are empty.
  • You had 'expand_variables="y"' which was not necessary, so I removed that. Expanding variables is where you do something like 'I have killed @Vorpvictim' - the @xxx part is expanded. However you were right to use scripting instead, as variables are expanded before the script executes.


I hope this helps your future scripting. :)



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
	name="VorpalCounter"
	author="Starky"
	id="a234cd0873b23a1f234e826c"
	language="lua"
	purpose="Simple vorpal counter which tallies per mort and total"
	date_written="2007-12-22 02:24:45"
	save_state="y"
	>
</plugin>

<aliases>

  <alias
   match="vorpchan *"
   enabled="y"
   variable="VorpChan"
   send_to="9"
   sequence="100"
  >
  <send>%1</send>
  </alias>
  
  <alias
   match="vorpreset"
   enabled="y"
   variable="VorpCount"
   send_to="9"
   sequence="100"
  >
  <send>0</send>
  </alias>
  
</aliases>



<triggers>
	<trigger
	enabled="y"
	group="Vorpal"
	match="Your weapon glows brightly and decapitates *"
	send_to="12"
	sequence="100"
	>
	<send>
var.VorpVictim = "%1"
var.VorpCount = var.VorpCount + 1
var.VorpTotal = var.VorpTotal + 1

SendNoEcho (var.VorpChan .. " I just vorpalled " .. 
            var.VorpVictim .. "! Which is " .. 
            var.VorpCount .. " total vorpals this remort, and " .. 
            var.VorpTotal .. " since added on Dec 12th 2007!")
	</send>
	</trigger>
</triggers>	


<script>
require "var"

-- initialize variables first time
var.VorpChan = var.VorpChan or "gt"
var.VorpCount = var.VorpCount or "0"
var.VorpTotal = var.VorpTotal or "0"

</script>
</muclient>
Australia Forum Administrator #2
You could make it a bit fancier by having it detect the date it was installed rather than hard-coding it in, like this:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
	name="VorpalCounter"
	author="Starky"
	id="a234cd0873b23a1f234e826c"
	language="lua"
	purpose="Simple vorpal counter which tallies per mort and total"
	date_written="2007-12-22 02:24:45"
	save_state="y"
	>
</plugin>

<aliases>

  <alias
   match="vorpchan *"
   enabled="y"
   variable="VorpChan"
   send_to="9"
   sequence="100"
  >
  <send>%1</send>
  </alias>
  
  <alias
   match="vorpreset"
   enabled="y"
   variable="VorpCount"
   send_to="9"
   sequence="100"
  >
  <send>0</send>
  </alias>
  
</aliases>



<triggers>
	<trigger
	enabled="y"
	group="Vorpal"
	match="Your weapon glows brightly and decapitates *"
	send_to="12"
	sequence="100"
	>
	<send>
var.VorpVictim = "%1"
var.VorpCount = var.VorpCount + 1
var.VorpTotal = var.VorpTotal + 1

SendNoEcho (var.VorpChan .. " I just vorpalled " .. 
            var.VorpVictim .. "! Which is " .. 
            var.VorpCount .. " total vorpals this remort, and " .. 
            var.VorpTotal .. " since added on " ..
            var.InstallDate .. "!")
	</send>
	</trigger>
</triggers>	


<script>
require "var"

-- initialize variables first time
var.VorpChan = var.VorpChan or "gt"
var.VorpCount = var.VorpCount or "0"
var.VorpTotal = var.VorpTotal or "0"
var.InstallDate = var.InstallDate or os.date ("%B %m, %Y")

</script>
</muclient>