How to alternate between 2 options?

Ask gaming related questions (AHK v1.1 and older)
Fayz
Posts: 1
Joined: 29 Sep 2020, 12:12

How to alternate between 2 options?

29 Sep 2020, 13:20

Hello!

I started using AHK yesterday, so keep in mind that I have very basic knowledge. I wrote a script for a game, works great! But.. it's long and messy so I'm improving it now by adding things like brackets, loops and referencing some values etc. I need some help figuring out how to best alternate between 2 lines of code (commands?). The 2 lines in question are the ones i've put a comment on "-- Login --" and "-- Enter --". I'd ideally like a very simple solution. I've never used a counter before but I was thinking maybe starting a counter and each time it loops it will either "Login" if the number is uneven or "Enter" if it's even.

Essentially this but in actual working code:
Count (+1)
if (count = uneven)
"Login"
if (count = even)
"Enter"


I'm inserting it in to this code:

Code: Select all

	{
		Loop 10
		{
			mouseclick, left, %i%, %j%, 1, 0 ; -- Login --
			; or
			mouseclick, left, %k%, %l%, 1, 0 ; -- Enter --
			Sleep 500
			Loop 3
			{
				Send %A_Tab% ; -- Switch target --
				Sleep 500
				Send x ; -- Cast spell --
				Loop 4
				{
					Sleep 2125
					if ($stop)
					{
						return
					}
				}
			}
		}
Last edited by BoBo on 30 Sep 2020, 05:32, edited 1 time in total.
Reason: Moved to Gaming section.
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: How to alternate between 2 options?

29 Sep 2020, 13:27

Use Mod(count, 2) to determine whether it is even or odd. Demo:

Code: Select all

loop, 10 {
	count := A_Index
	MsgBox, % count " is " (Mod(count, 2) ? "odd" : "even")
}
just me
Posts: 9528
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to alternate between 2 options?

30 Sep 2020, 05:16

Less maths:

Code: Select all

loop, 10 {
	count := A_Index
	MsgBox, % count " is " (Count & 1 ? "odd" : "even")
}
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to alternate between 2 options?

30 Sep 2020, 12:03

We could even go further: use merely boolean logic; actually, there's no skip during the loop: the operation can be consider as independent of a_index in this specific case; the value logically alternate between false and true:

Code: Select all

loop, 10 {
	; MsgBox, % a_index " is " ((toggle:=!toggle) ? "odd" : "even")
	MsgBox, % a_index " is " ((toggle:=not toggle) ? "odd" : "even")
}
A_AhkUser
my scripts

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 26 guests