AutoHotKey - repeating if statement keeps treating its condition as true in a loop

1

I'm trying to create an autoclicker using AutoHotKey for a game which has a system for fishing. This system makes an icon of a black exclamation mark surrounded by a large square border of white appear above your head, at which point you have to spam left click to catch the fish. Since the time at which the icon appears is randomly different each time, I have to be able to determine the time at which to spam click dynamically.

I've attempted to do that using PixelGetColor(as shown below) to find a particular pixel which ends up white whenever the exclamation mark appears, yet it never detects the exclamation mark, and more concerningly, whenever it detects the particular pixel on my screen to be white, the if statement's condition following PixelGetColor always returns true.

Toggle = False

loop 
{
  if (Toggle = "true")
  { 
    Sleep, 100
    PixelGetColor, color, 966, 463
    if (color = 0xFFFFFFFF) {
          MsgBox, It works!
    }
  }
}

F1::
if (Toggle = "true") {
  Toggle = False
}
else {
  Toggle = True
}

F1 is bound to a boolean which determines if the code inside of the loop is ran or not. The last two inputs for PixelGetColor, 966 and 463, are the x and y positions of the specific pixel respectively.

I'm expecting to get different values for the color once I start moving my camera in game, but after it detects white it proceeds to spam the message about the script 'working' when it clearly doesn't.

Simply put, what the hell is going on? I've been trying to figure that out for a while now. If anyone could give an explanation of what I've done wrong, as well as a snippet of code for what should be done inside of the loop to make the program work as intended, I would be eternally(well, almost) grateful.

loops
if-statement
autohotkey
asked on Stack Overflow Oct 25, 2019 by cow bears101

1 Answer

1

When I run your code as is, I don't get the message box after pressing F1. (I ensured the pixel at the specified coordinates was white.) Changing the color match to "0xFFFFFF" (two bytes per RGB) made it work for me and it worked as you intended in your description.

However, I am surprised it worked because true/false is being stored as a string in variable Toggle and I fully expected "True" wouldn't match "true" because of the case difference. (After a little more digging, I found that = in expressions is case-insensitive; == is case-sensitive - I guess I learned something today.)

Other than the color, I saw nothing in the code that would prevent it from working. Below is a modified version of yours that treats true/false as boolean values and is simplified slightly. Hopefully it will work for you too.

Loop 
{
    If Toggle
    { 
        Sleep , 100
        PixelGetColor , color , 966 , 463
        If ( color = 0xFFFFFF )
            MsgBox ,,, It works! , 0.5
}   }

f1::Toggle := !Toggle


Edit: Added area and variance to search per comment below.
Replace the two PixelGetColor and the If statement lines with the two lines below.

        PixelSearch ,,, 900 , 400 , 1000 , 500 , 0xFFFFFF , 20 , FastRGB
        If !ErrorLevel

This will search a 100x100px area for pure white with 20 shades of variance. You may need to adjust the rectangle size and placement as well as the variance to get it to work for you. More info here.

answered on Stack Overflow Oct 25, 2019 by EJE • edited Oct 25, 2019 by EJE

User contributions licensed under CC BY-SA 3.0