How to make 2 loop run accordingly?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Shiro0384
Posts: 11
Joined: 12 Jun 2022, 08:35

How to make 2 loop run accordingly?

Post by Shiro0384 » 26 Jun 2022, 08:15

This is loop 1

Code: Select all

           loop {
                    random, ran, 66, 89
                    sendinput, {r}
                    sleep %ran%
                   }
                   sleep 500 
This is loop 2

Code: Select all

           
            loop {
                    random, ran, 66, 89
                    sendinput, {q}
                    sleep %ran%
                   }
                   sleep 500 
I want too loop 1 for 5minute, the when the time after 5min then switch to loop 2 and loop for 10min. After 10min of loop 2 , I want to switch back to loop 1 and loop for 5min and the process is repeat and repeat. May I know how to do it? I need some example for it, appreciated for your help ya~

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to make 2 loop run accordingly?

Post by mikeyww » 26 Jun 2022, 08:51

Code: Select all

Loop
 For each, keySet in ["r|5", "q|10"] {
  part := StrSplit(keySet, "|"), end := A_TickCount + 60000 * part.2
  While (A_TickCount < end) {
   Send % part.1
   Random, ran, 56, 79
   Sleep, ran
  }
  Sleep, 500
 }

Rohwedder
Posts: 7552
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to make 2 loop run accordingly?

Post by Rohwedder » 26 Jun 2022, 10:05

Hallo,
or:

Code: Select all

SetTimer, Key,% 5*60*1000 ;5 minutes
Gosub, Key
Loop
{
	random, ran, 66, 89
	sendinput, {%Key%}
	sleep %ran%
}
Key:
Key := ["r","q","q"][++No:=Mod(0 No, 3)]
sleep, 500 * ( No < 3)
Return

Shiro0384
Posts: 11
Joined: 12 Jun 2022, 08:35

Re: How to make 2 loop run accordingly?

Post by Shiro0384 » 26 Jun 2022, 10:45

mikeyww wrote:
26 Jun 2022, 08:51

Code: Select all

Loop
 For each, keySet in ["r|5", "q|10"] {
  part := StrSplit(keySet, "|"), end := A_TickCount + 60000 * part.2
  While (A_TickCount < end) {
   Send % part.1
   Random, ran, 56, 79
   Sleep, ran
  }
  Sleep, 500
 }
Ya, but loop 1 and loop 2 still have long way down in the loop. Just I don't know how to settimer for loop 1 which is 5min, then after 5min and it pause the loop 1 proceed to loop 2 which is 10min. Then repeat to loop 1 after loop 2 reached 10min. Just repeat and repeat the process.

Shiro0384
Posts: 11
Joined: 12 Jun 2022, 08:35

Re: How to make 2 loop run accordingly?

Post by Shiro0384 » 26 Jun 2022, 10:49

Rohwedder wrote:
26 Jun 2022, 10:05
Hallo,
or:

Code: Select all

SetTimer, Key,% 5*60*1000 ;5 minutes
Gosub, Key
Loop
{
	random, ran, 66, 89
	sendinput, {%Key%}
	sleep %ran%
}
Key:
Key := ["r","q","q"][++No:=Mod(0 No, 3)]
sleep, 500 * ( No < 3)
Return
Got other method more simpler? I'm pretty new for programming, not sure how to use Settimer for my script, i do try alot way but it doesn't work for me :cry:

Rohwedder
Posts: 7552
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to make 2 loop run accordingly?

Post by Rohwedder » 26 Jun 2022, 11:09

I am amazed! What is difficult in this?:
SetTimer, Key,% 5*60*1000 ;5 minutes
The multiplications?
I would have expected more questions about that:
Key := ["r","q","q"][++No:=Mod(0 No, 3)]
Just to explain. The loop is working continuously without thinking. Every 5 minutes the boss (timer) comes and gives it a new tool (key).

Shiro0384
Posts: 11
Joined: 12 Jun 2022, 08:35

Re: How to make 2 loop run accordingly?

Post by Shiro0384 » 27 Jun 2022, 05:19

Code: Select all

     loop{           
            loop {
                    random, ran, 66, 89
                    sendinput, {r}
                    sleep %ran%
                   }
                   sleep 500 
                   Settimer,, 5*60*1000 ;(I want to add settimer between loop 1 and loop 2, so it will direct go loop 2 after 5min then after loop 2 reach 10min go back to loop 1)
            loop {
                    random, ran, 66, 89
                    sendinput, {q}
                    sleep %ran%
                   }
                   sleep 500 
                   Settimer,, 10*60*1000
           }

Rohwedder
Posts: 7552
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to make 2 loop run accordingly?

Post by Rohwedder » 27 Jun 2022, 07:38

A Timer is not a time-dependent Goto!
The Timer "Counter" in the following script creates a new thread every 5 minutes, as a Hotkey fired at that time would do, and counts the variable Counter one step further:

Code: Select all

SetTimer, Counter,% 5*60*1000 ;every 5 minutes
loop
{
	Counter := 0
	While, Counter = 0
	{
		random, ran, 66, 89
		sendinput, {r}
		sleep %ran%
	}
	sleep 500
	While, Counter < 3
	{
		random, ran, 66, 89
		sendinput, {q}
		sleep %ran%
	}
	sleep 500
}
Return
Counter:
Counter++
Return

Shiro0384
Posts: 11
Joined: 12 Jun 2022, 08:35

Re: How to make 2 loop run accordingly?

Post by Shiro0384 » 29 Jun 2022, 01:35

Rohwedder wrote:
27 Jun 2022, 07:38
A Timer is not a time-dependent Goto!
The Timer "Counter" in the following script creates a new thread every 5 minutes, as a Hotkey fired at that time would do, and counts the variable Counter one step further:

Code: Select all

SetTimer, Counter,% 5*60*1000 ;every 5 minutes
loop
{
	Counter := 0
	While, Counter = 0
	{
		random, ran, 66, 89
		sendinput, {r}
		sleep %ran%
	}
	sleep 500
	While, Counter < 3
	{
		random, ran, 66, 89
		sendinput, {q}
		sleep %ran%
	}
	sleep 500
}
Return
Counter:
Counter++
Return
May I asked why is "While, Counter <3" ? And The last line "Counter++" is mean what ya?

Rohwedder
Posts: 7552
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to make 2 loop run accordingly?

Post by Rohwedder » 29 Jun 2022, 02:19

There are two cases to distinguish.
Case 1: Counter is 0
Case 2: Counter is 1 or 2
Since the query While, Counter = 0 happened first, case 2 can be handled with While, Counter < 3, i.e. as long as Counter is less than 3.

Hotkey Q switches Timer CTimer on, Hotkey W switches off:

Code: Select all

q::SetTimer, CTimer, 500 ;every 500 ms
w::
SetTimer, CTimer, Off
SoundBeep, 1000, 20
Return
CTimer:
Counter++ ;post-increment
;https://www.autohotkey.com/docs/Variables.htm#Operators
SoundBeep, 4000, 20
ToolTip,% "Counter: " Counter
Return

Shiro0384
Posts: 11
Joined: 12 Jun 2022, 08:35

Re: How to make 2 loop run accordingly?

Post by Shiro0384 » 29 Jun 2022, 02:39

Appreciated for the explanations, It's worked for me, Thanks~

Post Reply

Return to “Ask for Help (v1)”