Help with detecting failure

Ask gaming related questions (AHK v1.1 and older)
Kageame
Posts: 2
Joined: 11 Dec 2021, 04:24

Help with detecting failure

Post by Kageame » 27 Jan 2022, 23:12

I wany my code to detect if it cannot find a certain image after X amount of attempts. Heres my current code that works but will not detect if it cant find the image and continue scanning for it infinitely

Code: Select all

#SingleInstance Force
#MaxThreadsPerHotkey 2
^!r::
  $stop := 0
  Loop, 
{
		Loop 
			{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, retry.png
					Sleep, 5
				} until !ErrorLevel
			MouseClick, left, %x%, %y%
		sleep, 25
	
	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, breed.png
					Sleep, 5
				} until !ErrorLevel
			MouseClick, left, %x%, %y%
		sleep, 25

	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, heart.png
					Sleep, 5
				} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%

	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, nursery.png
					Sleep, 5
			} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%
		sleep, 500
			MouseClick, left, 458, 329
		sleep, 25
		sleep, 50
	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, *50, egg ui.png
					Sleep, 5
			} until !ErrorLevel
		sleep, 500
			MouseClick, left, %x%, %y%
	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, sell.png
					Sleep, 5
			} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%
	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, yes.png
					Sleep, 5
			} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%
		sleep, 1200
			MouseClick, left, 1481, 696
		sleep, 500
	
if ($stop)
    {
      return
    }
  }

^!p:: $stop := 1

!a::
tooltip, %failure%
sleep, 500
tooltip
return
And heres the WIP code that I cant seem to figure out

Code: Select all

#SingleInstance Force
#MaxThreadsPerHotkey 2

	if (failure < 100) {
	^!r::
  		$stop := 0
 		 Loop, 
			{
		Loop 
			{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, retry.png
					Sleep, 25
			if (ErrorLevel = 1)
				failure++
			if (ErrorLevel = 2)
				failure++
				} until (ErrorLevel = 0)
			MouseClick, left, %x%, %y%
		sleep, 25
	
	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, breed.png
					Sleep, 25
				} until !ErrorLevel
			MouseClick, left, %x%, %y%
		sleep, 25

	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, heart.png
					Sleep, 25
				} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%

	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, nursery.png
					Sleep, 25
			} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%
		sleep, 1000
			MouseClick, left, 458, 329
		sleep, 25
		sleep, 500
	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, egg ui.png
					Sleep, 25
			} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%
	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, sell.png
					Sleep, 25
			} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%
	Loop
		{
				ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, yes.png
					Sleep, 25
			} until !ErrorLevel
		sleep, 25
			MouseClick, left, %x%, %y%
		sleep, 1200
			MouseClick, left, 1481, 696
		sleep, 500
if ($stop)
    {
      return
    }
  }
^!p:: $stop := 1
	}

	if (failure < 99) {
		Loop {
			ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, wonky.png
					Sleep, 25
				if (ErrorLevel = 1)
						failure--
				if (ErrorLevel = 2)
						failure--
				if (ErrorLevel = 0)
							MouseClick, left, %x%, %y%
					sleep, 25
			ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, connection.png
					Sleep, 25
				if (ErrorLevel = 1)
						failure--
				if (ErrorLevel = 2)
						failure--
				if (ErrorLevel = 0)
							MouseClick, left, %x%, %y%
					sleep, 25
		}
	}
	

!a::
tooltip, %failure%
sleep, 500
tooltip
return

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Help with detecting failure

Post by boiler » 27 Jan 2022, 23:45

Putting an if statement and code block markers around the hotkey subroutine doesn’t accomplish anything. What would cause it to ever execute that if statement after you press the hotkey even once, let alone each time through the loop? The code inside the hotkey subroutine doesn’t know there is an if statement outside of it and magically act upon it. You need to be execute that if statement in the flow of the code by putting the it inside the loop and breaking the loop if failures reach 100.

Post Reply

Return to “Gaming Help (v1)”