Quote:
Originally Posted by Numb-Ex
Hallo zusammen, ich bin derzeit dabei mir ein kleines Script zu schreiben.
Derzeit habe ich folgenden Code:
Code:
func start()
While 1
MouseClick("left", 1282, 570, 1)
Sleep("2500")
MouseClick("left", 713, 718, 1)
Sleep("1000")
$array_pos = PixelSearch(840,769,1050,819,0xB7E99B,5)
MouseClick("left", $array_pos[0],$array_pos[1],1,0)
Sleep("50000")
WEnd
EndFunc
Soweit so gut, er fährt die einzelnen Punkte an. Erkennt auch ob die Passende Farbe in dem Bereich vorhanden ist.
Jetzt möchte ich aber sagen, wenn diese Farbe dort nicht vorhanden ist, breche diese Funktion ab, und gehe in eine andere.
Wie kann ich dies mittels If abfrage realisieren?
|
You can follow the easy way:
Code:
$color1 = 0xB7E99B
$color2 = 0xFF5512
func start()
While 1
MouseClick("left", 1282, 570, 1)
Sleep(2500)
MouseClick("left", 713, 718, 1)
Sleep(1000)
$array_pos = PixelSearch(840,769,1050,819,0xB7E99B,5)
If Not @error Then
MouseClick("left", $array_pos[0],$array_pos[1],1,0)
Sleep(50000)
Else
;Not found, do sth here, maybe another pixel search or sth
;Ex:
;$array_pos = PixelSearch(0,0,500,500,$color2,5)
;If Not @error Then
;MsgBox(0,"Found","yay")
;EndIf
EndIf
WEnd
EndFunc
Advantages:
Easy to implement
Works most of the time
Cons:
Messy if/else concatenation when need to check a lot of events
Low maintainability
Or the event oriented one:
Code:
;This function register event handlers for pixelsearch, and the callback function
Global $handlers[0][3]
;Function,Callback,Disabled
Func registerSearchHandler($searchParams,$callbackFunction)
ReDim $handlers[UBound($handlers)+1][3]
$index = UBound($handlers)-1
Local $newArray[UBound($searchParams)+1]
$newArray[0] = "CallArgArray"
For $i = 1 To UBound($searchParams)
$newArray[$i] = $searchParams[$i-1]
Next
$handlers[$index][0] = $newArray
$handlers[$index][1] = $callbackFunction
$handlers[$index][2] = False
EndFunc
;Event handler functions, called when condition is met
Func handler1()
MsgBox(0,"handler1","pink found")
EndFunc
Func handler2()
MsgBox(0,"handler2","yellow found, disabling pink search")
$handlers[0][2] = True
EndFunc
Func handlern()
MsgBox(0,"handlerN","light blue found, enabling pink search")
$handlers[0][2] = False
EndFunc
;Register pixelsearch params, and event handler function
Local $arr = [0,160,555,300,0xFF00FF,5]
registerSearchHandler($arr,handler1)
Local $arr = [0,160,555,300,0xFFFF00,5]
registerSearchHandler($arr,handler2)
Local $arr = [0,160,555,300,0x00FFFF,5]
registerSearchHandler($arr,handlern)
While 1
; MouseClick("left", 1282, 570, 1)
; Sleep(2500)
; MouseClick("left", 713, 718, 1)
; Sleep(1000)
For $i = 0 To UBound($handlers) -1
;if handler disabled, not execute
if($handlers[$i][2]) Then ContinueLoop
$ret = Call("PixelSearch",$handlers[$i][0])
If Not @error Then
Call($handlers[$i][1])
EndIf
Next
;Sleep(5000)
WEnd
Advantages:
Easy scalability
No messy code
Can change callbacks at any time
Cons:
Messy State machine, (if after some event, another should be disabled, then callback function should handle this manually)...
Hard to implement and understand