Jump to content

Subscript Used with a non-array variable


Recommended Posts

I'm pretty new to this, so my apologies if this is a silly question. Look at the second line up from the bottom. It starts off fine, but on the third click in it gives me the "Subscript Used with a non-array variable" error for $coord[0] and $coord[1]. I don't understand what I'm doing wrong. I've got it defined as a global variable at the top. It's probably something really simple that I'm overlooking.

Global $coord

$x=10

Here is the problem section of the code. Remember. It's the third line up, the $coord[0] $coord[1] array...

If $exit = True Then ExitLoop
        $coord = PixelSearch($pos[0] - 3, $pos[1] - 3, $pos[0] + 3, $pos[1] + 3, $pixelcheck, 10)
        SplashOff()
        $message = "Now Clicking..."
        SplashTextOn("", $message, 400, 130, 400, 20, 1)
        If @error Then
            If $_number_of_errors = $failuresbeforereset Then
                $new_message = "AUTOAVOID - Detected " & $failuresbeforereset & ", non matching squares in a row" & @CRLF & "Starting a new row..."
                ControlSetText("", $message, "static1", $new_message)
                $message = $new_message
                If $failuresbeforereset > 2 Then $failuresbeforereset = $failuresbeforereset - 1
                $_number_of_errors = 0
                $x = 1
            Else
                $message = "AUTOAVOID - Square contents did not match original square" & @CRLF & "Skipping..."
                ControlSetText("", $message, "static1", $new_message)
                $_number_of_errors = $_number_of_errors + 1
            EndIf
        Else
            If $coord = 0xFFFFFF Then
                SplashTextOn("Edge of screen", "WARNING!" & @CRLF & "AUTOAVOIDANCE has detected you are at the edge of the screen. Starting a new row...",-1,-1,400,50,1)
                Sleep(1000)
                SplashOff()
                $x = 1
            Else
                MouseClick("left", $coord[0], $coord[1], 1, $mousespeed)
                MsgBox(0,"Working","line 109 is working...",.4)
            EndIf       
EndIf

Here is the whole thing in case you need it

#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.0.0
    Author: Daniel Mohr

    Script Function: Clicks grid of squares with optional auto avoidance if target square doesn't reasonably match the first.

#ce ----------------------------------------------------------------------------

HotKeySet("{esc}", "MyExit")
HotKeySet("{f5}", "startpos")
HotKeySet("{home}", "startpos")
HotKeySet("{g}", "startpos")

$yes = True
$loop = True
$exit = False ;this is an alternitive way to exit the loop when the esc button is pressed
$startset = False ;this is so I know later that they set a start position
$mousespeed = 1.3 ;make a slider for this later...
$xrst = 9 ;this is just to make the compiler happy, it gets defined later when I press HOME
$pos = MouseGetPos()
$_number_of_errors = 0
$failuresbeforereset = 3
Global $pixelcheck
Global $coord

$ans = MsgBox(4, "AUTOAVOIDANCE?", "Enable AUTOAVOIDANCE?")
If $ans = 6 Then $Autoavoidance = True
If $ans = 7 Then $Autoavoidance = False
$x = InputBox("columns?", "how many squares in top row?", 11)
$y = InputBox("rows?", "how many squares in side row?", 20)
;on GUI make a checkbox to make autovoidance optional

Func startpos()
    $pos = MouseGetPos()
    $startset = True ;so I know later that they set a start pos
    $xrst = $x
    $xx = $pos[0]
    $yy = $pos[1]
    $pixelcheck = PixelGetColor($pos[0], $pos[1])
    ;$coord = PixelSearch($pos[0] - 3, $pos[1] - 3, $pos[0] + 3, $pos[1] + 3, $pixelcheck, 10)  ; I pasted this here from xclicks()
    SplashTextOn("", "Start position set," & $pos[0] & ", " & $pos[1], 400, 20, 400, 100)
    Sleep(700)
    SplashOff()
EndFunc   ;==>startpos
$pos = MouseGetPos()

MsgBox(0, "define startin position", "press g to define starting swarue then hit OK to begin")
$xx = $pos[0]
$yy = $pos[1]

yclicks()

Func MyExit()
    SplashTextOn("", "Closing", "300", "50", "640", "400", 33, "Courier New", "20", "700")
    $exit = True
    Exit
EndFunc   ;==>MyExit



Func yclicks()
    Do
        If $exit = True Then
            ExitLoop
        EndIf
        SplashTextOn("", "Press ESC to exit", "300", "30", "640", "0", 33, "Courier New", "20", "700")
        If $Autoavoidance = True Then
            xclicks()
        Else
            xclicks_no_autoavoidance()
        EndIf
        $x = $xrst ;resets x back to normal
        $xx = $xx + 25 ;  shifts starting point down a row
        $yy = $yy + 12 ;  shifts starting point down a row
        $pos[0] = $xx ;   shifts starting point down a row
        $pos[1] = $yy ;   shifts starting point down a row
        $y = $y - 1
    Until $y = 0
EndFunc   ;==>yclicks

Func xclicks()
    Do
        If $exit = True Then ExitLoop
        $coord = PixelSearch($pos[0] - 3, $pos[1] - 3, $pos[0] + 3, $pos[1] + 3, $pixelcheck, 10)
        SplashOff()
        $message = "Now Clicking..."
        SplashTextOn("", $message, 400, 130, 400, 20, 1)
        If @error Then
            If $_number_of_errors = $failuresbeforereset Then
                $new_message = "AUTOAVOID - Detected " & $failuresbeforereset & ", non matching squares in a row" & @CRLF & "Starting a new row..."
                ControlSetText("", $message, "static1", $new_message)
                $message = $new_message
                If $failuresbeforereset > 2 Then $failuresbeforereset = $failuresbeforereset - 1
                $_number_of_errors = 0
                $x = 1
            Else
                $message = "AUTOAVOID - Square contents did not match original square" & @CRLF & "Skipping..."
                ControlSetText("", $message, "static1", $new_message)
                $_number_of_errors = $_number_of_errors + 1
            EndIf
        Else
            If $coord = 0xFFFFFF Then
                SplashTextOn("Edge of screen", "WARNING!" & @CRLF & "AUTOAVOIDANCE has detected you are at the edge of the screen. Starting a new row...",-1,-1,400,50,1)
                Sleep(1000)
                SplashOff()
                $x = 1
            Else
                MouseClick("left", $coord[0], $coord[1], 1, $mousespeed)
                MsgBox(0,"Working","line 109 is working...",.4)
            EndIf
        EndIf
        $pos[0] = $pos[0] + 25
        $pos[1] = $pos[1] - 12
        $x = $x - 1
    Until $x = 0
EndFunc   ;==>xclicks


Func xclicks_no_autoavoidance()
    Do
        If $exit = True Then ExitLoop
        MouseClick("left", $pos[0], $pos[1], 1, $mousespeed)
        $pos[0] = $pos[0] + 25
        $pos[1] = $pos[1] - 12
        $x = $x - 1
    Until $x = 0
EndFunc   ;==>xclicks_no_autoavoidance
Link to comment
Share on other sites

  • Moderators

danielmohr91,

$coord = PixelSearch(......

If PixelSearch fails, $coord is not an array - so $coord[0] & $coord[1] do not exist. Add some errorchecking to make sure $coord is an array before trying to use the elements $coord[0], $coord[1] in your MouseClick line.

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

danielmohr91,

$coord = PixelSearch(......

If PixelSearch fails, $coord is not an array - so $coord[0] & $coord[1] do not exist. Add some errorchecking to make sure $coord is an array before trying to use the elements $coord[0], $coord[1] in your MouseClick line.

M23

Brilliant!! Thanks mate! That makes sense now. I actually had @error, and it was working. But then I added the splash text etc... in between. I guess @error has to be on the line directly below $coord = PixelSearch(...

line. I switched the $coord = PixelSearch... to right above the If @error line and it's working beatiful now. I also read up on IsArray I the help file. Thank you both SO much!

Link to comment
Share on other sites

  • Moderators

danielmohr91,

I guess @error has to be on the line directly below $coord = PixelSearch(...

Spot on. Any command resets @error, so you have to check immediately after a command if you want to catch its error return. The only alternative is to use a variable to hold the value for later checking.

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

danielmohr91,

Spot on. Any command resets @error, so you have to check immediately after a command if you want to catch its error return. The only alternative is to use a variable to hold the value for later checking.

M23

Ok, thanks! Posted Image I'm beginning to understand @error. And honestly, I'm very grateful for your help! I was clueless and had no idea what was wrong. It's people like you and IchBistTod that make this forum a great place.

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