Jump to content

Newb need help with script


Recommended Posts

I am having trouble with one part of the code, It is made to detect a pixel on the screen that is given a set color. I want it to give 2 scripts (one if it finds the pixel, and another if it doesnt.)

I have "If @error then" and the script, IF it did not find the pixel.

I have "If Not @error Then" and the script, IF it found the pixel.

It just seems to continue the whole script in order, ignoring the "If @error then" & "If Not @error Then"

Global $Runner

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

Func Terminate()
Exit 0
EndFunc

Func Click()


PixelSearch(1233, 826, 1386, 852, 0xFFCC00)

If @error then
    MouseClick("left", 716, 698, 10)
Sleep (500)
Send("{a down}")
    Sleep (800)
    Send("{a up}")
    Sleep (500)
    MouseClick("left", 716, 698, 10)
    Sleep (500)
    Send("{d down}")
    Sleep (800)
    Send("{d up}")
    Sleep (500)
    MouseClick("left", 716, 698, 10)
    Sleep (500)
    EndIf
If Not @error Then
    Sleep (500)
    MouseClick("left", 863, 697, 3)
    Sleep (500)
    MouseClick("left", 701, 750, 3)
    Sleep (500)
    MouseClick("left", 863, 697, 3)
    Sleep (500)
    MouseClick("left", 701, 750, 3)
    Sleep (500)
    MouseClick("left", 1095, 873, 3)
    Send("{@ 1}")
    Sleep (100)
    Send("{c 1}")
    Sleep (100)
    Send("{o 1}")
    Sleep (100)
    Send("{n 1}")
    Sleep (100)
    Send("{f 1}")
    Sleep (100)
    Send("{l 1}")
    Sleep (100)
    Send("{i 1}")
    Sleep (100)
    Send("{c 1}")
    Sleep (100)
    Send("{t 1}")
    Sleep (100)
    Send("{s 1}")
    Sleep (100)
    Send("{o 1}")
    Sleep (100)
    Send("{u 1}")
    Sleep (100)
    Send("{l 1}")
    Sleep (100)
    Send("{Space 1}")
    Sleep (100)
    Send("{d 1}")
    Sleep (100)
    Send("{Enter 1}")
    Sleep (500)
    Send("{@ 1}")
    Sleep (100)
    Send("{c 1}")
    Sleep (100)
    Send("{o 1}")
    Sleep (100)
    Send("{n 1}")
    Sleep (100)
    Send("{f 1}")
    Sleep (100)
    Send("{l 1}")
    Sleep (100)
    Send("{i 1}")
    Sleep (100)
    Send("{c 1}")
    Sleep (100)
    Send("{t 1}")
    Sleep (100)
    Send("{s 1}")
    Sleep (100)
    Send("{o 1}")
    Sleep (100)
    Send("{u 1}")
    Sleep (100)
    Send("{l 1}")
    Sleep (100)
    Send("{Space 1}")
    Sleep (100)
    Send("{d 1}")
    Sleep (100)
    Send("{Enter 1}")
    Sleep (500)
Send("{@ 1}")
    Sleep (100)
    Send("{c 1}")
    Sleep (100)
    Send("{o 1}")
    Sleep (100)
    Send("{n 1}")
    Sleep (100)
    Send("{f 1}")
    Sleep (100)
    Send("{l 1}")
    Sleep (100)
    Send("{i 1}")
    Sleep (100)
    Send("{c 1}")
    Sleep (100)
    Send("{t 1}")
    Sleep (100)
    Send("{s 1}")
    Sleep (100)
    Send("{o 1}")
    Sleep (100)
    Send("{u 1}")
    Sleep (100)
    Send("{l 1}")
    Sleep (100)
    Send("{Space 1}")
    Sleep (100)
    Send("{d 1}")
    Sleep (100)
    Send("{Enter 1}")
    Sleep (500)
    EndIf
EndFunc


While 1
    Sleep(100)
    WEnd
Link to comment
Share on other sites

Change your script to this for debugging purposes:

Global $Runner

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

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Func Click()


    PixelSearch(1233, 826, 1386, 852, 0xFFCC00)

    If @error Then
        MsgBox(0,0,"@error was set!")
    EndIf
    If Not @error Then
        MsgBox(0,0,"@error was not set!")
    EndIf
EndFunc   ;==>Click


While 1
    Sleep(100)
WEnd

Much easier to read, and you'll see which part of the code is executed and which isn't.

What sdfaheemuddin is saying is indeed a good idea. Even though your code should work as well, it is vulnerable to a situation where the @error is set or unset by any of the code in your first if-block, because the second if will read that new @error value that may not be the same since the first if. He is suggesting you avoid checking the @error again halfway your script, and go "IF <check @error: error was set> <do stuff> ELSE (meaning error was not set) <do other stuff>". This is both more efficient, because you won't have to perform a check twice, and safer.

Global $Runner

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

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Func Click()


    PixelSearch(1233, 826, 1386, 852, 0xFFCC00)

    If @error Then
        MsgBox(0, 0, "@error was set!")
    Else
        MsgBox(0, 0, "@error was not set!")
    EndIf
EndFunc   ;==>Click


While 1
    Sleep(100)
WEnd
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Structure of your script is fine except you should return after calling functions in your If statements.

Either way, one or both of the conditions will be met.

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

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Func Click()
    _Func()
    If @error Then
        MsgBox(0,0, "Error")
        Return
    EndIf
    If Not @error Then
        MsgBox(0,0, "Not Error")
        Return
    EndIf
EndFunc   ;==>Click

Func _Func()
    Local Static $error = 0
    $error = Not $error
    Return SetError($error)
EndFunc

While 1
    Sleep(100)
WEnd

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

Thanks, but I also need the function to loop, and "While 1" doesnt seem to work in this situation.

I have tried adding

Do
    Send("{F1}")
    Sleep (3000)
    $i = $i + 1
    Until $i = 1000

As a unreliable fix, but it also didnt work.

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

Global $Runner

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

$i = 0

While 1
    Sleep(100)
WEnd


Do
    Send("{F1}")
    Sleep (3000)
    $i = $i + 1
    Until $i = 1000

Func Click()
    _Func()
    PixelSearch(1233, 826, 1386, 852, 0xFFCC00)
        If @error Then
        MouseClick("left", 716, 698, 10)
        Sleep (500)
        Send("{a down}")
        Sleep (800)
        Send("{a up}")
        Sleep (500)
        MouseClick("left", 716, 698, 10)
        Sleep (500)
        Send("{d down}")
        Sleep (800)
        Send("{d up}")
        Sleep (500)
        MouseClick("left", 716, 698, 10)
        Sleep (500)
        Return
    EndIf
    If Not @error Then
        Sleep (500)
        MouseClick("left", 863, 697, 3)
        Sleep (500)
        MouseClick("left", 701, 750, 3)
        Sleep (500)
        MouseClick("left", 863, 697, 3)
        Sleep (500)
        MouseClick("left", 701, 750, 3)
        Sleep (500)
        MouseClick("left", 1095, 873, 3)
        Send("{@ 1}")
        Sleep (100)
        Send("{c 1}")
        Sleep (100)
        Send("{o 1}")
        Sleep (100)
        Send("{n 1}")
        Sleep (100)
        Send("{f 1}")
        Sleep (100)
        Send("{l 1}")
        Sleep (100)
        Send("{i 1}")
        Sleep (100)
        Send("{c 1}")
        Sleep (100)
        Send("{t 1}")
        Sleep (100)
        Send("{s 1}")
        Sleep (100)
        Send("{o 1}")
        Sleep (100)
        Send("{u 1}")
        Sleep (100)
        Send("{l 1}")
        Sleep (100)
        Send("{Space 1}")
        Sleep (100)
        Send("{d 1}")
        Sleep (100)
        Send("{Enter 1}")
        Sleep (500)
        Send("{@ 1}")
        Sleep (100)
        Send("{c 1}")
        Sleep (100)
        Send("{o 1}")
        Sleep (100)
        Send("{n 1}")
        Sleep (100)
        Send("{f 1}")
        Sleep (100)
        Send("{l 1}")
        Sleep (100)
        Send("{i 1}")
        Sleep (100)
        Send("{c 1}")
        Sleep (100)
        Send("{t 1}")
        Sleep (100)
        Send("{s 1}")
        Sleep (100)
        Send("{o 1}")
        Sleep (100)
        Send("{u 1}")
        Sleep (100)
        Send("{l 1}")
        Sleep (100)
        Send("{Space 1}")
        Sleep (100)
        Send("{d 1}")
        Sleep (100)
        Send("{Enter 1}")
        Sleep (500)
        Send("{@ 1}")
        Sleep (100)
        Send("{c 1}")
        Sleep (100)
        Send("{o 1}")
        Sleep (100)
        Send("{n 1}")
        Sleep (100)
        Send("{f 1}")
        Sleep (100)
        Send("{l 1}")
        Sleep (100)
        Send("{i 1}")
        Sleep (100)
        Send("{c 1}")
        Sleep (100)
        Send("{t 1}")
        Sleep (100)
        Send("{s 1}")
        Sleep (100)
        Send("{o 1}")
        Sleep (100)
        Send("{u 1}")
        Sleep (100)
        Send("{l 1}")
        Sleep (100)
        Send("{Space 1}")
        Sleep (100)
        Send("{d 1}")
        Sleep (100)
        Send("{Enter 1}")
        Sleep (500)
        Return
    EndIf
EndFunc   ;==>Click

Func _Func()
    Local Static $error = 0
    $error = Not $error
    Return SetError($error)
EndFunc
Link to comment
Share on other sites

Here's your function loop...

While 1

Function ()

Wend

This will loop Function() forever.

Ya a function won't execute unless it's called. Func to EndFunc just has what the function supposed to do. You call a function by simply typing it's name and ().

Oic what you want. Just have your function call itself at the end...

Or hotkeyset("{F1}","Loop")

Func Loop ()

While 1

Click ()

Wend

EndFunc

Edited by markyrocks
Link to comment
Share on other sites

  • Moderators

travinia,

Your basic request seems strangely similar to that of jamesch222 here - that plus other indications make me think that you are one and the same person. Multiple accounts are not permitted here so would you (or jamesch222) care to explain what is going on? :huh:

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

  • Recently Browsing   0 members

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