Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Updating An Old Script

Updating An Old Script

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


Posted by Webkid   USA  (22 posts)  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
Top

Posted by Nick Gammon   Australia  (23,173 posts)  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
Top

Posted by Nick Gammon   Australia  (23,173 posts)  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
Top

Posted by Nick Gammon   Australia  (23,173 posts)  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
Top

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

-Webkid
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.


20,095 views.

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

Go to topic:           Search the forum


[Go to top] top

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