Random commands

Posted by Gpan on Fri 10 May 2019 03:09 AM — 5 posts, 22,427 views.

#0
Dear all,
i wanna random to excute commands, Zmud, we can use the #case %random {fill skin} {drink skin} {drop skin}, How can i do it by Lua? which function can help me?
Amended on Fri 10 May 2019 03:11 AM by Gpan
USA Global Moderator #1

commands = {"fill skin", "drink skin", "drop skin"}

commands[math.random(#commands)]


https://www.gammon.com.au/scripts/doc.php?lua=math.random
http://lua-users.org/wiki/MathLibraryTutorial
Amended on Fri 10 May 2019 04:53 AM by Fiendish
#2
thank you for your help, now i know how to execute the random commands,
if i wanna execute the specific commands.
EG. in Zmud, we can use #case @action {fill skin} {drink skin} {drop skin}, the commands depends on the parameter @action, how can i do it by Lua? still use Math.XXX?
please do not use Function "if elseif" that makes the scripts too large. Do we have any other simple short ideas?
USA Global Moderator #3
The answer is actually the same, just without generating a random number.


commands = {"fill skin", "drink skin", "drop skin"}
action = 1
commands[action]

This will always result in "fill skin"

The logic is that I have built a list of commands to choose from on the first line, and then I select which one of those commands to use. In my first answer it was selected randomly.

A combination of both answers for clarity is:

commands = {"fill skin", "drink skin", "drop skin"}
action = math.random(#commands)
commands[action]
Amended on Fri 10 May 2019 11:39 AM by Fiendish
Australia Forum Administrator #4
Gpan said:

thank you for your help, now i know how to execute the random commands


You can use the Send or Execute functions to send the random command to the MUD. "Send" literally sends the command, "Execute" treats it as if you had typed it (so if "fill" was an alias, for example, then that alias is processed).

eg.



commands = {"fill skin", "drink skin", "drop skin"}

Send ( commands[math.random(#commands)] )


Put together as an alias:


<aliases>
  <alias
   match="test"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>

commands = {"fill skin", "drink skin", "drop skin"}

Send (commands[math.random(#commands)])

</send>
  </alias>
</aliases>


Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.