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 ➜ General ➜ random in lua

random in lua

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


Posted by Magellan   (15 posts)  Bio
Date Mon 05 Mar 2018 06:49 PM (UTC)

Amended on Mon 05 Mar 2018 06:50 PM (UTC) by Magellan

Message
need help trying to select a single random direction from the variable i pull from the line in game. i think i'm headed in the right direction with my trigger here but insight suggestions would be helpful:

<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   keep_evaluating="y"
   match="Visible Exits\: (.*?)$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>Note ("this worked, variable is %1")
RDir = GetVariable ("RDir")
SetVariable ("RDir", "%1")
t = utils.split (RDir, " ")
Note (table.concat (t, "|"))

--this all works, i'm currently trying to build it so it picks a random exit from the list.</send>
  </trigger>
</triggers>
Top

Posted by Fiendish   USA  (2,535 posts)  Bio   Global Moderator
Date Reply #1 on Mon 05 Mar 2018 08:28 PM (UTC)

Amended on Mon 05 Mar 2018 08:37 PM (UTC) by Fiendish

Message
https://www.gammon.com.au/scripts/doc.php?lua=math.random

but don't do
RDir = GetVariable ("RDir")
SetVariable ("RDir", "%1")


That's almost certainly wrong. It makes RDir equal to the _previous_ directions line instead of the one that you just saw. And will also fail the first time, because if RDir isn't "Set" yet, then that GetVariable will return nil.

Just use

RDir = "%1"


Or if you really really for some other reason want to save the exits line for some other trigger you can use

SetVariable ("RDir", "%1")
RDir = GetVariable ("RDir")

But GetVariable immediately after SetVariable does basically the same thing as just doing RDir = "%1" if you don't need to store the value away in MUSHclient storage.

Anyway, with your table t, you want to pick a random index between 1 and #t.


dir = t[math.random(#t)]



The distinction between local Lua variables like RDir="foo" and MUSHclient storage variables like SetVariable("RDir", "foo") is one of the more confusing challenges that a new scripter deals with in MUSHclient, so don't fret if it takes a while to get used to them.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #2 on Mon 05 Mar 2018 09:05 PM (UTC)
Message
Fiendish said:

The distinction between local Lua variables like RDir="foo" and MUSHclient storage variables like SetVariable("RDir", "foo") is one of the more confusing challenges that a new scripter deals with in MUSHclient, so don't fret if it takes a while to get used to them.


There is, however, a tutorial on that:

http://www.gammon.com.au/forum/?id=10863

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Magellan   (15 posts)  Bio
Date Reply #3 on Mon 05 Mar 2018 09:59 PM (UTC)
Message
that actually helps a lot with some of my other scripts. i have used zmud and cmud for the better part of 20 years so a lot of this is all new to me. I do appreciate the help with the variables.

so i have fixed the script as you suggested, so my next question is to the script you suggested at the end:


dir = t[math.random(#t)]


can you help me understand how to use that and Send it to the mud as an output?

I keep reading "dir" as it's own variable. if this is the case can i change it to something else? I wouldn't mind saving the isolated direction as it's own variable too.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 05 Mar 2018 10:11 PM (UTC)

Amended on Mon 05 Mar 2018 10:12 PM (UTC) by Nick Gammon

Message
Assuming t is a table of directions, eg.


t = { "n", "s", "e", "w" }


Then #t is the number of items in t.


math.random (#t)


That returns a number from 1 to #t (ie. in this case: 1, 2, 3, 4 randomly).

http://www.gammon.com.au/scripts/doc.php?lua=math.random


dir = t[math.random(#t)]


That picks one of the directions from the table.

Now you can send that (Send is the correct capitalization):


Send (dir)


Template:function=Send Send

The documentation for the Send script function is available online. It is also in the MUSHclient help file.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #5 on Mon 05 Mar 2018 10:13 PM (UTC)
Message
Magellan said:

I keep reading "dir" as it's own variable. if this is the case can i change it to something else? I wouldn't mind saving the isolated direction as it's own variable too.


Of course, it's just a variable.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Magellan   (15 posts)  Bio
Date Reply #6 on Sat 10 Mar 2018 09:57 PM (UTC)

Amended on Sat 10 Mar 2018 10:17 PM (UTC) by Magellan

Message
an additional question. When i capture the line and then split it, it adds a space at the end which I've been trying to have the script ignore. I've tried Trim which appears to work but i don't believe that it is.

is there anything i should use or possibly instruct me to how to use Split if that's the solution. Thanks.

i figured it out i think. i just included a space at the end of the match string.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #7 on Sat 10 Mar 2018 10:06 PM (UTC)
Message
It appears to work, but it appears not to work?

Please post your code, and example output from the code which demonstrates it not working.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #8 on Sat 10 Mar 2018 10:09 PM (UTC)
Message
Possibly get rid of extra spaces from inside the variable, eg.


RDir = string.gsub (RDir, "%s+", " ")  -- convert multiple spaces to one space
RDir = Trim (RDir)  -- get rid of leading/trailing spaces

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fiendish   USA  (2,535 posts)  Bio   Global Moderator
Date Reply #9 on Sun 11 Mar 2018 03:02 PM (UTC)
Message
Quote:
When i capture the line and then split it, it adds a space at the end


That description is a little ambiguous. Are you saying that you have something like "a b c " and when you split on " " you get {"a", "b", "c", ""}?

https://github.com/fiendish/aardwolfclientpackage
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.


23,868 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.