Jump to content

if/pixelsearch with while/wend


Recommended Posts

Hello I am a new comer.

First of all I praise this great software. AutoIt just rock!

I have major problem understanding IF..... with mutiple line and loop (while...wend)

Originaly my goal was search the pixel from screen and click or press a keyboard, I made it work which it was from one of those tutorial however I want to make it improve somehow and even work better

B4 talking about problems i have, here is the code

Dim $hunger
Dim $hp
Dim $target = 0xwhatever
HotKeySet("{END}", "_Pause")
HotKeySet("{DEL}", "_Start")

Opt("MouseCoordMode", 0)
Opt("PixelCoordMode", 0)

While 1
    Sleep(5000)
  ;
WEnd

Func _Start()
    While 1
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)

        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
                
    $target = PixelSearch ( 205 , 306 , 410 , 459 , $target , 0 , 1 ) 
                If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)                    
                    If @error = 1 Then PixelSearch ( 615 , 306 , 459 , 820 , $target , 0 , 1 ) 
                        If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10) 
                            If @error = 1 Then PixelSearch ( 410 , 170 , 615 , 306 , $target , 0 , 1 )
                                If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                    If @error = 1 Then PixelSearch ( 410 , 459 , 615 , 612 , $target , 0 , 1 )
                                        If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                            If @error = 1 Then PixelSearch ( 205 , 170 , 410 , 306 , $target , 0 , 1 )
                                                If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                                    If @error = 1 Then PixelSearch ( 615 , 170 , 820 , 306 , $target , 0 , 1 )
                                                        If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                                            If @error = 1 Then PixelSearch ( 205 , 459 , 410 , 612 , $target , 0 , 1 )
                                                                If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                                                    If @error = 1 Then PixelSearch ( 615 , 459 , 820 , 612 , $target , 0 , 1 )
                                                                        If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                                                            If @error = 1 Then PixelSearch ( 410 , 60 , 615 , 170 , $target , 0 , 1 )
                                                                                If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                                                                    If @error = 1 Then PixelSearch ( 410 , 612 , 615 , 724 , $target , 0 , 1 )
                                                                                        If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                                                                            If @error = 1 Then PixelSearch ( 100 , 306 , 205 , 459 , $target , 0 , 1 )
                                                                                                If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                                                                                    If @error = 1 Then PixelSearch ( 820 , 306 , 900 , 459 , $target , 0 , 1 )
                                                                                                        If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                                                                                            Sleep(250)
;
    Wend
EndFunc

Func _Pause()
    Exit
EndFunc

Where i am having problem is $target.

basically i devide the res to total 25 and i wanted to pixelsearch only 12 out of 25

One start from box1 and "IF" no error then click on $target or "IF" error then move to next line and so on and on.... but

when i execute this code it doesn't run properly

Also i devide the while/wend as while 1 with first $hunger, $hp and while 2 only for $target and still doesn't work

If i only put

$target = PixelSearch ( 200 , 200 , 900 , 700 , $target , 0 , 1 ) 
                If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
                                    If @error = 1 then Sleep(500)
                                 Sleep(1000)

this kinda works but when it comes with "If @error = 1" it still clicks on somewhere on the screen which I don't want it to

is the

What am I doing wrong/missing?

thx in advance. =)

Edited by AnotherNewb
Link to comment
Share on other sites

Sorry Zedna

i tried and it came up with other problems.... trying fixing it but i kinda got lost on that one

here is the new code i have done and "ONE PROBLEM"

Func _Start()
    While 1
        $target1 = PixelSearch ( 205 , 306 , 410 , 459 , $target1 , 0 , 1 ) 
                If @error = 0 Then MouseClick("left", $target1[0], $target1[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target2 = PixelSearch ( 615 , 306 , 459 , 820 , $target2 , 0 , 1 ) 
                If @error = 0 Then MouseClick("left", $target2[0], $target2[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target3 = PixelSearch ( 410 , 170 , 615 , 306 , $target3 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target3[0], $target3[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target4 = PixelSearch ( 410 , 459 , 615 , 612 , $target4 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target4[0], $target4[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target5 = PixelSearch ( 205 , 170 , 410 , 306 , $target5 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target5[0], $target5[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target6 = PixelSearch ( 615 , 170 , 820 , 306 , $target6 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target6[0], $target6[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target7 = PixelSearch ( 205 , 459 , 410 , 612 , $target7 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target7[0], $target7[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target8 = PixelSearch ( 615 , 459 , 820 , 612 , $target8 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target8[0], $target8[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target9 = PixelSearch ( 410 , 60 , 615 , 170 , $target9 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target9[0], $target9[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target10 = PixelSearch ( 410 , 612 , 615 , 724 , $target10 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target10[0], $target10[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target11 = PixelSearch ( 100 , 306 , 205 , 459 , $target11 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target11[0], $target11[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
        $target12 = PixelSearch ( 820 , 306 , 900 , 459 , $target12 , 0 , 1 )
                If @error = 0 Then MouseClick("left", $target12[0], $target12[1], 1, 10)
                    sleep(3000)
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
                If @error = 0 Then Send("{F2 3}")
                    Sleep(500)
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
                If @error = 0 Then Send("{F1 3}")
                Sleep(500)
;
    WEnd
EndFunc

as you can see, I have made all box search semi_var.

It is yet one complete loop cycle but sort of separated from each box seach.

However T,.T

I have one major problem left to fix and i was searching helpfiles/websites but gave me no fix or clue

after each box seach and If statement returns with error = 1 then it still clicks on somewhere in "THAT BOX"

is anyway i can stop that click "If Pixelsearch returned Error" and goto next box?

Link to comment
Share on other sites

I dislike this code very much.

You are assigning a hex color to $target, then later in the script overwriting $target with PixelSearch results, making it into an array, thereby breaking every other PixelSearch call.

Look at this:

Dim $hunger
Dim $hp
Dim $target = 0xFFFFF
HotKeySet("{END}", "_Pause")
HotKeySet("{DEL}", "_Start")

Opt("MouseCoordMode", 0)
Opt("PixelCoordMode", 0)

While 1
    Sleep(5000)
WEnd

Func _Start()
    While 1
        
        $hunger = PixelSearch(176, 90, 176, 90, 0xCEF3A5, 0, 1);Feed if the bar level lower then Set
        If @error = 0 Then Send("{F2 3}")
        Sleep(500)

        $hp = PixelSearch(148, 134, 148, 134, 0x000000, 0, 1);Use potion if the bar lower then Set
        If @error = 0 Then Send("{F1 3}")
        Sleep(500)
        
        
        ;Declare matrix L,T,W,H
        Dim $coordinates[12][4] = [[205, 306, 410, 459], _
                        [615, 306, 459, 820], _
                        [410, 170, 615, 306], _
                        [410, 459, 615, 612], _
                        [205, 170, 410, 306], _
                        [615, 170, 820, 306], _
                        [205, 459, 410, 612], _
                        [615, 459, 820, 612], _
                        [410, 60, 615, 170], _
                        [410, 612, 615, 724], _
                        [100, 306, 205, 459], _
                        [820, 306, 900, 459]]
                        
        MsgBox(0,"",Ubound($coordinates))
        
        ;Loop through matrix
        For $V = 0 to Ubound($coordinates) - 1
            If $V = 0 Then
                ;Set $targetxy on first PixelSearch only ???
                $targetxy = PixelSearch($coordinates[$V][0], $coordinates[$V][1], $coordinates[$V][2], $coordinates[$V][3], $target, 0, 1)
            Else
                If @ERROR Then PixelSearch($coordinates[$V][0], $coordinates[$V][1], $coordinates[$V][2], $coordinates[$V][3], $target, 0, 1)
            EndIf
            If NOT @ERROR Then MouseClick("left", $targetxy[0], $targetxy[1], 1, 10)
        Next
        
        Sleep(250)
        ;
    WEnd
EndFunc   ;==>_Start

Func _Pause()
    Exit
EndFunc   ;==>_Pause
Edited by weaponx
Link to comment
Share on other sites

Dude you are making debugging impossible with your repetitive code. Here I shortened the second version you posted:

Func _Start()
While 1
    ;Declare matrix L,T,W,H
    Dim $coordinates[12][4] = [[205, 306, 410, 459], _
                    [615, 306, 459, 820], _
                    [410, 170, 615, 306], _
                    [410, 459, 615, 612], _
                    [205, 170, 410, 306], _
                    [615, 170, 820, 306], _
                    [205, 459, 410, 612], _
                    [615, 459, 820, 612], _
                    [410, 60, 615, 170], _
                    [410, 612, 615, 724], _
                    [100, 306, 205, 459], _
                    [820, 306, 900, 459]]
                    

    ;Loop through matrix
    For $V = 0 to Ubound($coordinates) - 1
        $target = PixelSearch ( $coordinates[$V][0], $coordinates[$V][1], $coordinates[$V][2], $coordinates[$V][3] , $target1 , 0 , 1 )
        If @error = 0 Then MouseClick("left", $target[0], $target[1], 1, 10)
        Sleep(3000)
        
        ;Static coordinates
        $hunger = PixelSearch ( 176 , 90 , 176 , 90 , 0xCEF3A5 , 0 , 1 );Feed if the bar level lower then Set
        If @error = 0 Then Send("{F2 3}")
        Sleep(500)
        
        ;Static coordinates
        $hp = PixelSearch ( 148 , 134 , 148 , 134 , 0x000000 , 0 , 1 );Use potion if the bar lower then Set
        If @error = 0 Then Send("{F1 3}")
        Sleep(500)
    Next
WEnd
EndFunc
Edited by weaponx
Link to comment
Share on other sites

Thank you for your help weaponx!

But............. You just sent my brain to JUPITER T,.T

This line "the For.... To Ubound" is something i didn;t know of, Thank you but I kinda lost on that $V

Isn't $V has to be declaired begining of the code? cuz it just came back up with an error on that line for "Badly formatted"

AutoIt now complains about line 44 and I'm just eyeballing autoit helpfile to explain what is For...To....next,,,,,,,,,,ubound........ etc etc etc

Link to comment
Share on other sites

Ubound returns the number of elements in the array, essentially the same as the number you use to declare it, in this case there are 12 items in the array.

Using For $V = 0 to Ubound($coordinates) - 1

Makes the variable $V equal 0,1,2,3,4,5,6,7,8, etc, incrementing it each time.

I didn't test the code completely so I know there are some errors which I am about to edit.

Link to comment
Share on other sites

Feel free to message me directly.

I updated my posts since I had the array name entered wrong.

Also when you are declaring the $target color you should name it something clear like $targetColor = 0xFFFFFF

And then when you are using $target = PixelSearch call it $targetXY so you can distinguish between them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...