random in lua

Posted by Magellan on Mon 05 Mar 2018 06:49 PM — 10 posts, 33,334 views.

#0
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>
Amended on Mon 05 Mar 2018 06:50 PM by Magellan
USA Global Moderator #1
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.
Amended on Mon 05 Mar 2018 08:37 PM by Fiendish
Australia Forum Administrator #2
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
#3
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.
Australia Forum Administrator #4
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.

Amended on Mon 05 Mar 2018 10:12 PM by Nick Gammon
Australia Forum Administrator #5
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.
#6
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.
Amended on Sat 10 Mar 2018 10:17 PM by Magellan
Australia Forum Administrator #7
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.
Australia Forum Administrator #8
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
USA Global Moderator #9
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", ""}?