Need Help with subs, loops and timers please

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Noo0B
Posts: 151
Joined: 26 Jan 2018, 19:54

Need Help with subs, loops and timers please

04 Apr 2021, 20:52

Hi,
I`m trying to write a code that does an image search loop until the image is not found.
Then it has to wait 4 seconds and send an imput.
Finally, It needs to go back to the image search loop, and there is where I fail.
The code i wrote is more or less like this:

Code: Select all

Type1:
Toggle:= !Toggle
		If Toggle {
			While Toggle {
				ImageSearch, , , 0, 0, 1000, 90, *10 youimage.png
					If ErrorLevel = 0
						Var:= True
					if ErrorLevel = 1
						Var:= False
					If (Var = False) {
						SetTimer,Type1ImgSearch , -4000
					}
		Return
		}
		If !Toggle {
		Return
		}
Return

Type1ImgSearch:
			SendInput, q
Return
}
What am I doing wrong? The code simply doesnt go back to the loop.
Any help is much appreciated.
Thanks and kind regards.
User avatar
boiler
Posts: 16917
Joined: 21 Dec 2014, 02:44

Re: Need Help with subs, loops and timers please

05 Apr 2021, 00:19

This does what you described. Toggle on and off using the Tab key (change the hotkey to whatever, of course).

Code: Select all

loop {
	while Toggle {
		ImageSearch, , , 0, 0, 1000, 90, *10 youimage.png
		if ErrorLevel {
			Sleep, 4000
			SendInput, q
		}
	}
	Sleep, 100 ; to prevent overusage of CPU. can reduce if desired
}
return

Tab::Toggle:= !Toggle

Some explanations:

It needs an outer loop so it will keep retrying the inner loop after it is exited when toggled off.

The part in your script where you have a bunch of if statements to change the value of another variable based on ErrorLevel's value is unnecessary when you can just check the value of ErrorLevel directly. The line if ErrorLevel evaluates as true when ErrorLevel is 1, which is when the image is not found.

Don't use a SetTimer because then the loop continues immediately and will keep resetting the timer over and over each time through the loop (before hardly any of the 4 seconds have elapsed). Just use a simple Sleep, 4000 so the loop won't continue until the 4 second wait and sending q is complete.
Noo0B
Posts: 151
Joined: 26 Jan 2018, 19:54

Re: Need Help with subs, loops and timers please

05 Apr 2021, 08:34

Hi Boiler,
thanks alot for the reply.
I tested it and it did wait the 4 seconds.
Two other things happened, though:
4 seconds after it sent the first input, code sent another one while the image was there.
I redid the image capture but it didn`t help, it pops up onscreen and lasts 5 seconds.
Maybe one of those ErrorLevel =1 for the ImageSearch line?
I also found out it sends an extra input after toggled off.
My wild guess is the code had two loops running consecutively, and that's somehow messing it up.
I tried this, but the opposite happens, it never goes back inside the loop and toggles off after one iteration:

Code: Select all

Type1:
Toggle:= !Toggle
		If Toggle {
			While Toggle {
				ImageSearch, , , 0, 0, 1000, 90, *10 youimage.png
					If ErrorLevel {
						SetTimer,Type1ImgSearch , -4000
						return
					}
		Return
		}
		If !Toggle {
		Return
		}
Return

Type1ImgSearch:
SendInput, q
Sleep, 500
Gosub, Type1
Return
User avatar
boiler
Posts: 16917
Joined: 21 Dec 2014, 02:44

Re: Need Help with subs, loops and timers please

05 Apr 2021, 08:46

It would seem to me that part of what you’re seeing is because of how you said you wanted it to work. If it waits four seconds after not finding the image before sending q, the image could have appeared in that amount of time but it’s still going to send it. Are you now saying you want it to send it only after it hasn’t appeared during that four-second wait?

The version of the script I posted would not send q again if the image was truly found after the 4 second wait has expired and then sent the letter. The image could have easily appeared immediately after the script didn’t find it while it was waiting the four seconds, then sent the letter at the end of those four seconds, as was how you said it should work.

Noo0B wrote:
05 Apr 2021, 08:34
Maybe one of those ErrorLevel =1 for the ImageSearch line?
I’m not sure what you’re asking. If ErrorLevel is the same thing as if ErrorLevel = 1, if that’s what you’re asking.
edgecraft
Posts: 3
Joined: 05 Apr 2021, 19:37
Contact:

Re: Need Help with subs, loops and timers please

06 Apr 2021, 00:16

Hi! This might help. I'm still new with scripting so this might not give you what you want.

Code: Select all

Type1:
Var:= ""
Toggle:= !Toggle
			Loop {
				If !Toggle
					break
				ImageSearch, , , 0, 0, 1000, 90, *10 yourimage.png
					If ErrorLevel = 0
						{
						Var = 0
						continue
						}
					else if ErrorLevel = 1
						{
						sleep, 100
						Var += 1
						if Var < 40
							continue
						Send {Your Input}
						Var = 0
						continue
						}
				}
				}
Return
Noo0B
Posts: 151
Joined: 26 Jan 2018, 19:54

Re: Need Help with subs, loops and timers please

26 Apr 2021, 09:42

boiler wrote:
05 Apr 2021, 08:46
It would seem to me that part of what you’re seeing is because of how you said you wanted it to work. If it waits four seconds after not finding the image before sending q, the image could have appeared in that amount of time but it’s still going to send it. Are you now saying you want it to send it only after it hasn’t appeared during that four-second wait?

The version of the script I posted would not send q again if the image was truly found after the 4 second wait has expired and then sent the letter. The image could have easily appeared immediately after the script didn’t find it while it was waiting the four seconds, then sent the letter at the end of those four seconds, as was how you said it should work.

Noo0B wrote:
05 Apr 2021, 08:34
Maybe one of those ErrorLevel =1 for the ImageSearch line?
I’m not sure what you’re asking. If ErrorLevel is the same thing as if ErrorLevel = 1, if that’s what you’re asking.
I see what you are saying, But the image is for the input itself, and it can't come up before another input is accepted, that being after 4 secs the image expires.
The script should only do 3 things:
1- send the input; that toggles the scannable image wich expires at a certain time or before, at random.
2- scan loop catches that the image is gone.
3- waits 4 seconds before sending the input again.

I know it seems simple but i can't make it happen right.
User avatar
boiler
Posts: 16917
Joined: 21 Dec 2014, 02:44

Re: Need Help with subs, loops and timers please

26 Apr 2021, 09:57

Noo0B wrote: The script should only do 3 things:
1- send the input; that toggles the scannable image wich expires at a certain time or before, at random.
2- scan loop catches that the image is gone.
3- waits 4 seconds before sending the input again.

I know it seems simple but i can't make it happen right.
Where is step one in any version of what you had posted? Or is it part of code that you haven't shown? I don't see where you send it prior to checking for the image to be gone.
Noo0B
Posts: 151
Joined: 26 Jan 2018, 19:54

Re: Need Help with subs, loops and timers please

07 May 2021, 17:14

boiler wrote:
26 Apr 2021, 09:57
Where is step one in any version of what you had posted? Or is it part of code that you haven't shown? I don't see where you send it prior to checking for the image to be gone.
The code sends the initial image hotkey Q.
User avatar
boiler
Posts: 16917
Joined: 21 Dec 2014, 02:44

Re: Need Help with subs, loops and timers please

07 May 2021, 20:21

Noo0B wrote:
07 May 2021, 17:14
The code sends the initial image hotkey Q.
I asked where it does that, and I still don't see that happening before any of the looping starts. Not sure why you're not showing it.

Also, I mentioned why you should not to use SetTimer and you went right back to it in your next post after that.

I can only help if I can methodically step through what it's supposed to do and you implement my suggestions. It's not looking like you're really wanting to do that, so I'll let someone else try to help.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot] and 225 guests