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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Simple alias that cycles through commands one at a time

Simple alias that cycles through commands one at a time

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


Posted by Sinule   (6 posts)  [Biography] bio
Date Sat 06 Feb 2021 08:22 AM (UTC)
Message
Hi, I'm after an alias that outputs a command to the mud and then the next time it's run, outputs the next command in the list to the mud, until reaching the end of the list where it will go back to the beginning and start again.
So as an example:
I have a list of three commands: command1, command2, command3
[I press the * button]
The alias outputs "command1" to the mud
[I press the * button]
The alias outputs "command2" to the mud
[I press the * button]
The alias outputs "command3" to the mud
[I press the * button]
The alias outputs "command1" to the mud
etc...

It's been a long time since I attempted any sort of coding (and my mind has been a bit ravaged by time since then), but I'm guessing the alias would need:
A list or array of strings
A counter which resets to 0 when reaching the end of the list

I've tried searching for some sort of similar alias which I could butcher into something suitable for my own purposes but without any luck (which must be down to my poor searching abilities as I would assume there must plenty of examples of something like this floating about, I'm just not sure what search terms to use...)

Any pointers in the right direction would be gratefully appreciated!

Many thanks,

Sinule
[Go to top] top

Posted by Fiendish   USA  (2,513 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Sat 06 Feb 2021 10:37 PM (UTC)

Amended on Sun 07 Feb 2021 06:28 AM (UTC) by Fiendish

Message
[EDIT]Oops! Thanks Nick!

Quote:
I'm guessing the alias would need:
A list or array of strings
A counter which resets to 0 when reaching the end of the list

Your intuition is correct! (except lists in Lua start at index 1, not 0)

In Lua:

your_list_of_strings = {"Hello", "My", "Name", "Is", "Sinule"}


function send_next_command()
   your_counter = (your_counter or 0) + 1
   if your_counter > #your_list_of_strings then
      your_counter = 1
   end
   Send(your_list_of_strings[your_counter])
end


Depending on where you put this, you may need to use GetVariable and SetVariable for using and incrementing your_counter.

That might look like


function send_next_command()
   your_counter = (GetVariable("your_counter") or 0) + 1
   if your_counter > #your_list_of_strings then
      your_counter = 1
   end
   Send(your_list_of_strings[your_counter])
   SetVariable("your_counter", your_counter)
end

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,966 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sat 06 Feb 2021 11:50 PM (UTC)
Message
Fiendish, you are out by one in your code. It should read:


your_list_of_strings = {"Hello", "My", "Name", "Is", "Sinule"}


function send_next_command()
   your_counter = (your_counter or 0) + 1
   -- wrap if we exceed the number in the array
   if your_counter > #your_list_of_strings then
      your_counter = 1
   end
   Send(your_list_of_strings[your_counter])
end

- Nick Gammon

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

Posted by Fiendish   USA  (2,513 posts)  [Biography] bio   Global Moderator
Date Reply #3 on Sun 07 Feb 2021 06:29 AM (UTC)
Message
Hah! Oops! I edited my post.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Sinule   (6 posts)  [Biography] bio
Date Reply #4 on Sun 07 Feb 2021 08:41 AM (UTC)

Amended on Sun 07 Feb 2021 12:19 PM (UTC) by Sinule

Message
Fiendish said:
Depending on where you put this, you may need to use GetVariable and SetVariable for using and incrementing your_counter.


Thanks so much for the help, I'm not sure I would haven't gotten anywhere without it! Could you describe what you mean by "Depending on where you put this"? I've created a new script and put the following in it:


command_list = {"rip", "fei", "stb", "sla", "sho", "tri", "fli", "sli", "pie"}
-- ----------------------------------------------------------
-- Initialise command_list with strings
-- ----------------------------------------------------------

function send_next_command()
   command_counter = (command_counter or 0) + 1
-- ----------------------------------------------------------------
-- A counter which is incrimented every time the function is called
-- ----------------------------------------------------------------

   if command_counter > #command_list then
-- ----------------------------------------------------------------------------------------------------
-- Check to see if the value of command_counter is greater than the number of items in the command_list
-- ----------------------------------------------------------------------------------------------------

      command_counter = 1
-- --------------------------------------------------------------------------------------------------------------
-- Change the value of command_counter to 1 if it's value is greater than the number of items in the command_list
-- --------------------------------------------------------------------------------------------------------------

   end

   Send(command_list[command_counter])
-- ------------------------------------------------------------------------------
-- Send the string in the command_list which matches the value of command_counter
-- ------------------------------------------------------------------------------

end

The comments are my understanding of what's going on (writing them helps me get my head around it!)

The script is called commandcycle.lua
I haven't been able to work out how to call that script by pressing a button on the keypad yet, I'm guessing I need to create an alias and then put that in the "keypad" window, I can see that you can send things *to* a script, but struggling to find out how I can *call* a script using an alias.

I got it working! Woohoo! I created an alias "comc" with nothing in the send box, and then "send_next_command" (no parenthesise) in the script box. Then just changed the alias in the keypad screen to "comc" and it seems to work perfectly.

Thanks a lot for your help again!
[Go to top] top

Posted by Nick Gammon   Australia  (22,966 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Sun 07 Feb 2021 09:02 PM (UTC)
Message
Glad you worked it out. I was about to suggest that.

An alternative would be to make a macro (see world configuration -> Input -> Macros) and choose one, say, F3. Then in the Send box put your alias name. That way you can just jab at F3 without having to also type Enter.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[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.


10,771 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 HostDash]