Updating An Old Script

Posted by Webkid on Sun 09 Mar 2008 05:44 AM — 5 posts, 21,121 views.

USA #0
Ok here is the deal. MY HDD took a crash on me and since I had to order a new one and I have been wanting to make the switch to Lua from VBscript for my scripting in MUSHclient, I decided to just start a whole new world file/fresh MUSHclient install.

I am trying to put all my scripts into a file now, instead of "Send to Script" all the time like I was previously doing. I am a complete newbie when it comes to Lua. Because of the help I got way back when on this very script, I learned how to do some really neat stuff with VBscript.

The fourm post link to the script in question is here: http://tinyurl.com/2kr8hd

So here I sit, with an almost blank script file with the following lines, wondering what to do next.


function ScourCounter ()
	
end -- of ScourCounter


So if I could be assisted once again, this time in converting this old script into Lua, it would be much appreciated.

Thank You.
Australia Forum Administrator #1
First let's look at doing it the "send to script" way for comparison.

The trigger to count things ...

VBscript version:


<triggers>
  <trigger
   enabled="y"
   match="^You find (?P&lt;coins&gt;\d{1,3}) coins from the ground\!$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
SetVariable "coins", CInt (GetVariable ("coins")) + %&lt;coins&gt;
ColourNote "white", "blue", "You now have " &amp; GetVariable ("coins") &amp; " coins."
</send>
  </trigger>
</triggers>


Lua version:


<triggers>
  <trigger
   enabled="y"
   match="^You find (?P&lt;coins&gt;\d{1,3}) coins from the ground\!$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
SetVariable ("coins", (GetVariable ("coins") or 0) + %&lt;coins&gt;)
ColourNote ("white", "blue", "You now have " .. GetVariable ("coins") .. " coins.")
</send>
  </trigger>
</triggers>


They are virtually the same, except for some brackets, the "or 0" to handle if the variable is not set yet, and using ".." to concatenate strings.

The alias to reset the counter ...


VBscript version:


<aliases>
  <alias
   match="resetcoins"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
SetVariable "coins", "0"
ColourNote "white", "blue", "Number of coins reset to zero."
</send>
  </alias>
</aliases>


Lua version:


<aliases>
  <alias
   match="resetcoins"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
SetVariable ("coins", "0")
ColourNote ("white", "blue", "Number of coins reset to zero.")
</send>
  </alias>
</aliases>


However if you want to use a script file instead, we need to move the scripting part.

The trigger is now:


<triggers>
  <trigger
   enabled="y"
   match="^You find (?P&lt;coins&gt;\d{1,3}) coins from the ground\!$"
   regexp="y"
   script="ScourCounter"
   sequence="100"
  >
  </trigger>
</triggers>


The alias is now:


<aliases>
  <alias
   script="ResetCounter"
   match="resetcoins"
   enabled="y"
   sequence="100"
  >
  </alias>
</aliases>


And the script file is this:


function ScourCounter (name, line, wildcards)
  SetVariable ("coins", (GetVariable ("coins") or 0) + wildcards.coins)
  ColourNote ("white", "blue", "You now have " .. GetVariable ("coins") .. " coins.")
end -- of ScourCounter

function ResetCounter (name, line, wildcards)
  SetVariable ("coins", "0")
  ColourNote ("white", "blue", "Number of coins reset to zero.")
end -- of ResetCounter 

Amended on Sun 09 Mar 2008 06:17 AM by Nick Gammon
Australia Forum Administrator #2
There is a Lua module (which comes with MUSHclient) that lets you simplify getting and setting MUSHclient variables. This is a simplified version of the script that uses that instead:


require "var"
function ScourCounter (name, line, wildcards)
  var.coins = (var.coins or 0) + wildcards.coins
  ColourNote ("white", "blue", "You now have " .. var.coins .. " coins.")
end -- of ScourCounter

function ResetCounter (name, line, wildcards)
  var.coins = 0
  ColourNote ("white", "blue", "Number of coins reset to zero.")
end -- of ResetCounter 



Note how the GetVariable and SetVariable function calls are gone, instead you simply use "var.coins" to get at the coins variable.
Australia Forum Administrator #3
http://tinyurl.com/2kr8hd is a reference to http://www.gammon.com.au/forum/?id=4272
USA #4
Thank you very much Nick. Works great!