[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Updating An Old Script
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Updating An Old Script

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Webkid   USA  (22 posts)  [Biography] bio
Date Sun 09 Mar 2008 05:44 AM (UTC)
Message
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.

-Webkid
[Go to top] top

Posted by Nick Gammon   Australia  (21,677 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sun 09 Mar 2008 06:10 AM (UTC)

Amended on Sun 09 Mar 2008 06:17 AM (UTC) by Nick Gammon

Message
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 


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,677 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sun 09 Mar 2008 06:13 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,677 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sun 09 Mar 2008 06:15 AM (UTC)
Message
http://tinyurl.com/2kr8hd is a reference to http://www.gammon.com.au/forum/?id=4272

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Webkid   USA  (22 posts)  [Biography] bio
Date Reply #4 on Sun 09 Mar 2008 11:24 PM (UTC)
Message
Thank you very much Nick. Works great!

-Webkid
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


3,119 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]