Simple alias that cycles through commands one at a time

Posted by Sinule on Sat 06 Feb 2021 08:22 AM — 6 posts, 20,751 views.

#0
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
USA Global Moderator #1
[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
Amended on Sun 07 Feb 2021 06:28 AM by Fiendish
Australia Forum Administrator #2
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
USA Global Moderator #3
Hah! Oops! I edited my post.
#4
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!
Amended on Sun 07 Feb 2021 12:19 PM by Sinule
Australia Forum Administrator #5
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.