Jump to content

How to speed up pixel search and If/Else question


Recommended Posts

Hi I am currently trying to speed up the following code

 
; Press Esc to terminate script, Pause/Break to "pause"


Global $Paused, $counter = 0
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("+!d", "ShowMessage") ;Shift-Alt-d


;;;; Body of program would go here;;;;
While 1
    $coord = PixelSearch( 1490, 720, 1776, 720, 0x202020, 0 )
    If @error = 0 Then ;; if color found the do action
    MouseMove($coord[0],$coord[1])
        mouseclick("left")
   Sleep(15)
    Else ;; if not wait 5 secs and repeat 
        Sleep(1000)
    EndIf


Wend
;;;;;;;;


Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0, $counter, 1)
    WEnd
    ToolTip("")
EndFunc


Func Terminate()
    Exit 0
EndFunc


Func ShowMessage()
    MsgBox(4096,"","This is a message.")
EndFunc

the above "works" but I think that searching 4 specific pixels for 0x202020 rather than a range of pixels would be much faster? So I used Autoit window info to gather the following information

 
pixel1 1400, 530 0x202020
pixel2 1560, 530 0x202020
pixel3 1700, 530 0x202020
pixel4 1850, 530 0x202020
 
now with my basic knowledge I understand that using if/else I would need to set the pixelsearch to for the first location which seeing the correct pixel colour would click the mouse
While 1
    $coord = PixelSearch( 1400, 530, 1400, 530, 0x202020, 0 )
    If @error = 0 Then ;; if color found the do action
    MouseMove($coord[0],$coord[1])
        mouseclick("left")
   Sleep(15)
    Else ;; if not wait 5 secs and repeat 
        Sleep(1000)
    EndIf


Wend

and if 0x202020 was not present at 1400, 530 it would then check 1560, 530 and if not present there it would check 1700, 530 and so on. looped

so I am guessing that that information would go in the else section but I am having trouble converting my thinking into correct code :)

Any help would be much appreciated.

Link to comment
Share on other sites

Or would it perhaps go after the first 

$coord = PixelSearch( 1490, 720, 1776, 720, 0x202020, 0 )

Something like this

$coord = PixelSearch( 1400, 720, 1400, 720, 0x202020, 0 ), 
$coord2 = PixelSearch( 1560, 720, 1560, 720, 0x202020, 0 ), 
$coord3 = PixelSearch( 1700, 720, 1700, 720, 0x202020, 0 ), 
$coord4 = PixelSearch( 1850, 720, 1850, 720, 0x202020, 0 )

Or am I way off track? 

Thanks.

Link to comment
Share on other sites

Ok so I assume you are saying that PixelGetColour is a faster method than pixelsearch

This is what I just came up with im sure I have the grammar of AHK wrong could I get some feedback on it thank you

HotKeySet("{ESC}", "Terminate")

$box1 = PixelGetColor(1400, 530)
$box2 = PixelGetColor(1560, 530)
$box3 = PixelGetColor(1700, 530)
$box4 = PixelGetColor(1850, 530)

if $box1 = 0x202020 Then
   MouseMove($box1[0],$box1[1]) 
   mouseclick("left")
   Sleep(15)
Else
 if $box2 = 0x202020 Then
       MouseMove($box2[0],$box2[1]) 
   mouseclick("left")
   Sleep(15)
Else
    if $box3 = 0x202020 Then
       MouseMove($box3[0],$box3[1]) 
   mouseclick("left")
   Sleep(15)
Else
    if $box4 = 0x202020 Then
       MouseMove($box4[0],$box4[1]) 
   mouseclick("left")
   Sleep(15)
Edited by 7upinhere
Link to comment
Share on other sites

  • Moderators

7upinhere,

 

I have the grammar of AHK wrong

The language you are using is AutoIt. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Developers

im sure I have the grammar of AHK wrong could I get some feedback on it thank you

AHK ?

What about you try a little more research yourself by opening the helpfile and checking out the proper syntax?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Sorry mybad I was in a rush to leave and got the two confused.

Ok I have given it another go and tried to tidy it up still with not much luck, I checked over the help file about the correct grammar and I think I did it correct.

HotKeySet("{ESC}", "Terminate")
HotKeySet("{c}", "Copy")

Local $box1 = PixelGetColor(1400, 530)
Local $box2 = PixelGetColor(1560, 530)
Local $box3 = PixelGetColor(1700, 530)
Local $box4 = PixelGetColor(1850, 530)
local $colour = 0x202020

if $box1 = $colour Then
   MouseMove($box1[0],$box1[1])
   mouseclick("left")
   Sleep(15)
ElseIf $box2 = $colour Then
       MouseMove($box2[0],$box2[1])
   mouseclick("left")
   Sleep(15)
 ElseIf $box3 = $colour Then
       MouseMove($box3[0],$box3[1])
   mouseclick("left")
   Sleep(15)
 Elseif $box4 = $colour Then
       MouseMove($box4[0],$box4[1])
   mouseclick("left")
   Sleep(15)
EndIf
Edited by 7upinhere
Link to comment
Share on other sites

 I notice you use LOTS of mouseclicks. Is there some reason you can't use ControlClick? Much more stable and if the window loses focus the control will still be clicked.

 

I guess there is no reason not to use ControlClick I just saw examples of MouseClick and thought that would be best to use. The application that the script will be interfacing will be running on a separate laptop so there is no need to worry about it not being on top.

Anyway I would like to figure out why its not working before I rewrite it using contolclicks but thank you for the advice.

Link to comment
Share on other sites

PixelGetColor returns the color at co-ordinates x,y

In your updated example @ #7 ; $Box1 is not an array, it is a color

Try something like below  

Edit - Made recursive Ifs , I think this will speed up your program, if color is found at the first opixel this will skip the rest of the searches

HotKeySet("{ESC}", "Terminate")
HotKeySet("{c}", "Copy")
Local $Box1[2] = [1400,530]
Local $Box2[2] = [1560,530]
Local $Box3[2] = [1700,530]
Local $Box4[2] = [1850,530]
Local $Color1 = PixelGetColor($box1[0], $box1[1])


local $colour = 0x202020

If $Color1 = $colour Then
     MouseMove($box1[0],$box1[1])
     mouseclick("left")
     Sleep(15)
   Else 
     If $colour = PixelGetColor($box2[0], $box2[1]) Then
        MouseMove($box2[0],$box2[1])
        mouseclick("left")
        Sleep(15)
     Else
       If $colour = PixelGetColor($box3[0], $box3[1]) Then
         MouseMove($box3[0],$box3[1])
         mouseclick("left")
         Sleep(15)
       Else
         If $colour = PixelGetColor($box4[0], $box4[1]) Then
           MouseMove($box4[0],$box4[1])
           mouseclick("left")
           Sleep(15)
         Else
           MsgBox(0,'Error','No Matches Found.')
         EndIf
     EndIf
   EndIf
EndIf
Edited by Shane0000
Link to comment
Share on other sites

Second question: Is there a reason why you have to use PixelGetColor? When I write scripts I try to avoid that method at all cost. It is the most unstable method to detect change.

 

PixelGetColor

 

 

 

PixelGetColor returns the color at co-ordinates x,y

In your updated example @ #7 ; $Box1 is not an array, it is a color

Try something like below  

Edit - Made recursive Ifs , I think this will speed up your program, if color is found at the first opixel this will skip the rest of the searches

HotKeySet("{ESC}", "Terminate")
HotKeySet("{c}", "Copy")
Local $Box1[2] = [1400,530]
Local $Box2[2] = [1560,530]
Local $Box3[2] = [1700,530]
Local $Box4[2] = [1850,530]
Local $Color1 = PixelGetColor($box1[0], $box1[1])


local $colour = 0x202020

If $Color1 = $colour Then
     MouseMove($box1[0],$box1[1])
     mouseclick("left")
     Sleep(15)
   Else 
     If $colour = PixelGetColor($box2[0], $box2[1]) Then
        MouseMove($box2[0],$box2[1])
        mouseclick("left")
        Sleep(15)
     Else
       If $colour = PixelGetColor($box3[0], $box3[1]) Then
         MouseMove($box3[0],$box3[1])
         mouseclick("left")
         Sleep(15)
       Else
         If $colour = PixelGetColor($box4[0], $box4[1]) Then
           MouseMove($box4[0],$box4[1])
           mouseclick("left")
           Sleep(15)
         Else
           MsgBox(0,'Error','No Matches Found.')
         EndIf
     EndIf
   EndIf
EndIf

Yep this works great thanks I added a while/wend to make it loop and made a few minor adjustments.

Link to comment
Share on other sites

you never did answer the question I asked. Why do you HAVE to use PixelGetColor?

 

I did not HAVE to use PixelGetColour it was suggested by John who by judging from his/her post count has a good enough knowledge of autoit. It would be a bit dumb me coming here for advice and then not trying advice that was offered, no?

Link to comment
Share on other sites

PixelGetColor is more appropriate than PixelSearch in this instance here.

MBALZESHARI is hinting there may be better ways than Pixel functions all together depending on what you are working with. For example if you were working with a standard native windows GUI there would almost certainly be a more stable way.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

PixelGetColor is more appropriate than PixelSearch in this instance here.

MBALZESHARI is hinting there may be better ways than Pixel functions all together depending on what you are working with. For example if you were working with a standard native windows GUI there would almost certainly be a more stable way.

That is what I'm getting at. Thanks John!

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...