if (ErrorLevel = 0) and 1

Get help with using AutoHotkey and its commands and hotkeys
AHKlover
Posts: 3
Joined: 20 Sep 2020, 18:17

if (ErrorLevel = 0) and 1

20 Sep 2020, 18:30

hello community,
I am new to the forum, I apologize and correct me if I am posting in the wrong area.
I have a question regarding if (ErrorLevel = 1)
and if (ErrorLevel = 0).
For example, I have a PixelSearch multiple script. I would like to know how I use ErrorLevel if the pixel is found to follow the script and if it is not found skip to the next PixelSearch.
I tried to use it but even when PixelSearch is not found it still runs.
User avatar
mikeyww
Posts: 1121
Joined: 09 Sep 2014, 18:38

Re: if (ErrorLevel = 0) and 1

20 Sep 2020, 21:49

Debugging an invisible script is a challenge!

Code: Select all

PixelGetColor, bgr, 100, 100
PixelSearch, x, y, 99, 99, 101, 101, %bgr%
Switch ErrorLevel
{
 Case 0: MsgBox, 64, Success, Pixel %bgr% was found at (%x%,%y%)!
 Case 1: MsgBox, 48, Failure, Pixel was not found.
 Case 2: MsgBox, 48, Failure, A problem prevented the command from conducting the search
}
Alternatively, you can use something like the following.

Code: Select all

If ErrorLevel
 Do something
Else Do something different
Add braces if you have multiple lines in either part.

Code: Select all

If ErrorLevel {
 Do something because the search failed
 Do more
} Else {
 Do something different because the search succeeded
 Do something more different
}
AHKlover
Posts: 3
Joined: 20 Sep 2020, 18:17

Re: if (ErrorLevel = 0) and 1

21 Sep 2020, 07:27

mikeyww wrote:
20 Sep 2020, 21:49
Debugging an invisible script is a challenge!

Code: Select all

PixelGetColor, bgr, 100, 100
PixelSearch, x, y, 99, 99, 101, 101, %bgr%
Switch ErrorLevel
{
 Case 0: MsgBox, 64, Success, Pixel %bgr% was found at (%x%,%y%)!
 Case 1: MsgBox, 48, Failure, Pixel was not found.
 Case 2: MsgBox, 48, Failure, A problem prevented the command from conducting the search
}
Alternatively, you can use something like the following.

Code: Select all

If ErrorLevel
 Do something
Else Do something different
Add braces if you have multiple lines in either part.

Code: Select all

If ErrorLevel {
 Do something because the search failed
 Do more
} Else {
 Do something different because the search succeeded
 Do something more different
}
hello friend, first thanks for responding.
my problem is precisely in the "else".
I just wanted him to skip to the next one without doing anything or without breaking the "loop"
I made this script quickly just as a demonstration to understand what I would like.
I'm new to this program so my script is a mess...

Code: Select all

1::
loop
{
	if (ErrorLevel = 0)
{
loop 1
{
PixelSearch, x, y, 0, 0, 1440, 900, 0x219AFF, 3, Fast
	
MouseMove, %x%, %y%, 2
}
}
else
{
	skip to the next one (I tried to use "skip" but it is not a valid code)
}
if (ErrorLevel = 0)
{
loop 1
{
PixelSearch, x, y, 0, 0, 1440, 900, 0x21DBF7, 3, Fast

MouseMove, %x%, %y%, 2
}
}
else
{
	skip to the next one (I tried to use "skip" but it is not a valid code)
}
}
return

I haven't tested this script yet, this is just the idea of ​​my original so that I can apply the if (ErrorLevel = 0) correctly
Last edited by gregster on 21 Sep 2020, 07:29, edited 1 time in total.
Reason: [Code] tags added. Please use them. Thank you!
User avatar
mikeyww
Posts: 1121
Joined: 09 Sep 2014, 18:38

Re: if (ErrorLevel = 0) and 1

21 Sep 2020, 07:55

This worked for me. You can adjust the coordinates. If you want to search every pixel on the screen, it's going to take a while.

Code: Select all

find := ["0xFADADA", "0x219AFF"]
Return

F4::
n := 0
While (n < find.MaxIndex()) {
 n++
 PixelSearch, x, y, 9, 9, 12, 12, % find[n]
 If !ErrorLevel {
  SoundBeep, 1500, 30
  MouseMove, x, y
  n := find.MaxIndex()
 }
}
SoundBeep, 1000, 20
Return
An alternative for the same thing is below.

Code: Select all

F4::
For index, color in ["0xFADADA", "0x219AFF"] {
 PixelSearch, x, y, 9, 9, 12, 12, %color%
 If !ErrorLevel {
  SoundBeep, 1500, 30
  MouseMove, x, y
  Break
 }
}
SoundBeep, 1000, 20
Return
AHKlover
Posts: 3
Joined: 20 Sep 2020, 18:17

Re: if (ErrorLevel = 0) and 1

21 Sep 2020, 08:03

mikeyww wrote:
21 Sep 2020, 07:55
This worked for me. You can adjust the coordinates. If you want to search every pixel on the screen, it's going to take a while.

An alternative for the same thing is below.

Code: Select all

F4::
For index, color in ["0xFADADA", "0x219AFF"] {
 PixelSearch, x, y, 9, 9, 12, 12, %color%
 If !ErrorLevel {
  SoundBeep, 1500, 30
  MouseMove, x, y
  Break
 }
}
SoundBeep, 1000, 20
Return
so I just use it !ErrorLevel without level = 0, 1, 2
and without Else?
to jump to the next PixelSearch on the screen?
but if I use "Break" it will not stop my "Loop"?
User avatar
mikeyww
Posts: 1121
Joined: 09 Sep 2014, 18:38

Re: if (ErrorLevel = 0) and 1

21 Sep 2020, 08:24

This will look for every color in the list, if that is what you want. Break jumps out of the loop.

Code: Select all

F4::
For index, color in ["0xFADADA", "0x219AFF"] {
 PixelSearch, x, y, 9, 9, 12, 12, %color%
 If !x
  Continue
 SoundBeep, 1200, 30
 MouseMove, x, y
}
SoundBeep, 1000, 20
Return

Return to “Ask For Help”

Who is online

Users browsing this forum: mikeyww and 33 guests