Jump to content

How do you implement a timer condition?


Recommended Posts

$test = True
while($test)
    $BananaPixel = PixelSearch(903, 24, 1250, 157, 0xD9D70C, 1)
    if NOT(@error) Then
        LC($BananaPixel[0], $BananaPixel[1] + 250)
        $test = False
    EndIf
WEnd

#if BananaPixel isnt found in 15 seconds, do: 'LC(993,34)' then search for $BananaPixel again

Func LC($x, $y)
   MouseClick("left", $x, $y, 1, 3)
EndFunc

My code above tries to search for a pixel for an infinite amount of time until it is found. Once it is found, it clicks the x coordinate of the pixel and the y coordinate + 250 of the pixel, then the while loop stops. I don't know how to use the timer functions to make a condition for my code that "if I cannot find the BananaPixel in 15 seconds, do a left click at x,y coordinates then search for BananaPixel again. Any help would be greatly appreciated! Thanks.

Link to comment
Share on other sites

Good morning @briang123, and welcome to the AutoIt forum :)
Let's comment a bit what your script does :)

$test = True
while($test) ; While 1
    $BananaPixel = PixelSearch(903, 24, 1250, 157, 0xD9D70C, 1) ; Search for the Pixel
    if NOT(@error) Then                                         ; If the Pixel has been found, then...
        LC($BananaPixel[0], $BananaPixel[1] + 250)
        $test = False
    EndIf                                                       ; Nothing ELSE...
WEnd

; if BananaPixel isnt found in 15 seconds, do: 'LC(993,34)' then search for $BananaPixel again

Func LC($x, $y)
   MouseClick("left", $x, $y, 1, 3)
EndFunc

Do you know about "If...Else" statement? :)
You can take a look here.

About the timer, indeed, you should take a look at TimerInit() and TimerDiff() functions, which you can find here:

Local $test = True, _
      $hTimer                                                       ; Declare the Timer


$hTimer = TimerInit()                                               ; Start the Timer

while($test)                                                        ; While 1
    $BananaPixel = PixelSearch(903, 24, 1250, 157, 0xD9D70C, 1)     ; Search for the Pixel
    If Not (@error) Then                                            ; If the Pixel has been found, then...
        LC($BananaPixel[0], $BananaPixel[1] + 250)                  ; Left Click
        $test = False                                               ; ExitLoop
    Else
        If TimerDiff($hTimer) >= 15000 Then                         ; If the Timer limit of 15 seconds has been reached, then...
            ConsoleWrite(Round(TimerDiff($hTimer)/1000, 0) & " seconds limit reached!" & @CRLF & _
                         "The timer will be restarted!" & @CRLF)
            ; LC(1, 2)                                              ; LC(x, y)
            $hTimer = TimerInit()                                   ; Restart the timer
        Else
            ContinueLoop                                            ; ELSE, Continue the While loop
        EndIf
    EndIf
WEnd

; if BananaPixel isnt found in 15 seconds, do: 'LC(993,34)' then search for $BananaPixel again

Func LC($x, $y)
   MouseClick("left", $x, $y, 1, 3)
EndFunc

If you have question about this, feel free to ask :)

Best Regards.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Moderators

briang123,

By the name of your variable ($BananaPixel) you might well be looking to automate a game and you have possibly not read the Forum rules since your arrival on the forum. Please do read them - particularly the bit about not discussing game automation - before you post again.

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

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

×
×
  • Create New...