vtDemo
vtDemo

Reputation: 9

AHK Continue after PixelSearch

I would like to simply make the script continue ONLY after a sucessful PixelSearch. ie.

This is what I have now. What do I replace the break with?


;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel = 0
    break //???
;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel = 0
    break //???
;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel = 0
    break
}

Upvotes: 0

Views: 1439

Answers (3)

Mister Robato
Mister Robato

Reputation: 176

Here, a loop for every PixelSearch loops until ErrorLevel is 0, which means pixel is found.

Loop
{
    CoordMode, Pixel, Screen
    PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast RGB
}
Until ErrorLevel = 0
MsgBox, 0, , success 1

Loop
{
    CoordMode, Pixel, Screen
    PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast RGB
}
Until ErrorLevel = 0
MsgBox, 0, , success 2

Loop
{
    CoordMode, Pixel, Screen
    PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast RGB
}
Until ErrorLevel = 0
MsgBox, 0, , success 3

Upvotes: 0

Spyre
Spyre

Reputation: 2344

Note: this was an answer to a version of the question prior to a clarification. I have posted a new answer that should better solve this problem.

Based on what I understand, here is a GoSub solution that runs a subroutine if PixelSearch fails to find the pixel/ ErrorLevel = 0:

PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel = 0
    gosub, subroutine 

PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel = 0
    gosub, subroutine

subroutine:
;insert whatever code you want to run in between the line above and the return
MsgBox, This is a subroutine. No matching pixels were found
return

Upvotes: 0

Spyre
Spyre

Reputation: 2344

Short Answer: You would want to replace break with return.

Long Answer: Break and Continue are used when you are trying to exit any kind of loopable statement. In order to stop the execution of most other parts of scripts, such as the auto-execute section, a hotkey, a subroutine, or a function, you would use a return statement. Additionally, I don't think some of the other parts of your script are working as you intend them to. For example, with your current usage of ErrorLevel, your script would effectively be only continuing if none of the mentioned pixels are found on screen. This is because your conditional statement that checks whether the program should stop executing is checking for if ErrorLevel = 0. From the previously linked docs, ErrorLevel would be 0 only if the pixel was successfully found, meaning that a nonzero value will be returned is the pixel is not found. In order to fix this in your code, we can simply change if ErrorLevel = 0 to if ErrorLevel whenever it appears in the code.

Modified Code:

;Move other stuff up here
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel
    return
;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel
    return
;stuff
PixelSearch, Px, Py, 995, 256, 999, 262, 0x84BCD1, 40, Fast
if ErrorLevel
    return
MsgBox, complete
;put code that you want to run after the condition is met here
return

Upvotes: 1

Related Questions