Help with Else If

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
David1484
Posts: 11
Joined: 08 Apr 2020, 03:11

Help with Else If

08 Apr 2020, 03:28

Hi, I am a bit of a newbie at AHK. I tried making this script which moves your mouse on 3 different colours depending on if they're on the screen. The colours are Yellow, Pink then Orange. If Yellow isn't on screen, it searches for the colour pink and if pink isn't on screen is searches for Orange. When I tried running it, it didn't work. It showed an error message saying, "Error: ELSE with no matching IF". If anyone could help me it would be appreciated. Thanks.

Code: Select all

Loop
{
    CoordMode, Pixel, Screen
    PixelSearch, OranX, OranY, 0, 0, 1365, 767, 0xFFCC66, 0, Fast RGB
    CoordMode, Pixel, Screen
    PixelSearch, DotX, DotY, 0, 0, 1365, 767, 0xFF66FF, 0, Fast RGB
    CoordMode, Pixel, Screen
    PixelSearch, FoundX, FoundY, 0, 0, 1366, 768, 0xFCE839, 0, Fast RGB
    If ErrorLevel = 0
    {
        Click, %FoundX%, %FoundY%, 0
        Sleep, 10
    }
    Else
    {
        Click, %DotX%, %DotY%, 0
        Sleep, 10
    }
    Else
    {
        Click, %OranX%, %OranY%, 0
        Sleep, 10
    }
}
[Mod edit: [code][/code] tags added]
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Help with Else If

08 Apr 2020, 03:32

Welcome to the forums!

You can't have multiple Elses for one If. (exception: else ifs).
Combine both else branches into one? It's the same condition anyway...

For Click, you most likely also need CoordMode, Mouse, Screen. Otherwise, you mix up different coordinate scopes...
CoordMode, Pixel, Screen you don't have to always repeat, if you don't change it in the meantime.
David1484
Posts: 11
Joined: 08 Apr 2020, 03:11

Re: Help with Else If

08 Apr 2020, 03:55

If you could provide me with an example using CoordMode, Mouse, Screen it would be appreciated.
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Help with Else If

08 Apr 2020, 04:07

Just put it at the top of the script.
User avatar
Scr1pter
Posts: 1277
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Help with Else If

08 Apr 2020, 04:38

Hi David,

you can also create an array with all the target pixels inside of it.
With a for-loop, you search for all pixels.

Try this:

Code: Select all

;Pixelsearch-Array
#SingleInstance force ; For just 1 instance, overwrite existing one

F1::
CoordMode, Mouse, Screen
pixelArray := [0xFFCC66, 0xFF66FF, 0xFCE839, 0xED1C24] ; Target colors
for i in pixelArray ; Cycle through array
{
  pixel := pixelArray[i] ; Access element of array
  PixelSearch, px, py, 0, 0, 1365, 767, %pixel%, 3, Fast ; Search for currently selected pixel
  if ErrorLevel = 0 ; If pixel found:
  {
    Click, %px%, %py% ; Click on pixel
    break ; Leave loop
  }
}
return
Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
David1484
Posts: 11
Joined: 08 Apr 2020, 03:11

Re: Help with Else If

08 Apr 2020, 05:02

I tried your script although it did not work.
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Help with Else If

08 Apr 2020, 05:03

Scr1pter wrote:
08 Apr 2020, 04:38
Try this:
You used CoordMode, Mouse, Screen, but removed the CoordMode, Pixel, Screen (you don't have to repeat it, but use it at least once to make it consistent with the mouse Coordmode)... that doesn't look like it would work reliably.

It would also use for i, color in pixelArray (or similar) for simplification - but that's secondary.
User avatar
Scr1pter
Posts: 1277
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Help with Else If

08 Apr 2020, 05:18

@David1484:
Any error message or did just nothing happen?
You'll probably only have to fix the CoordMode, as gregster said.

@gregster:
Thanks for the array hint, I'll remember this.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
David1484
Posts: 11
Joined: 08 Apr 2020, 03:11

Re: Help with Else If

08 Apr 2020, 05:21

Nothing happened.
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Help with Else If

08 Apr 2020, 05:25

You should show your updated code...
David1484
Posts: 11
Joined: 08 Apr 2020, 03:11

Re: Help with Else If

08 Apr 2020, 05:35

Sorry I was replying to Scr1pter question. Here is what I'm working with so far. This time it works and opens, although the OranX/Y Variables do not work. Whenever theres an orange colour with no yellow or pink colours on my screen, my mouse pointer doesn't go on the orange. It works fine for the FoundX/Y Variables (Yellow Colour) and DotX/Y Variables (Pink Colour).

Code: Select all

Loop
{
    CoordMode, Mouse, Screen
    PixelSearch, OranX, OranY, 0, 0, 1365, 767, 0xFFCC66, 0, Fast RGB
    CoordMode, Mouse, Screen
    PixelSearch, DotX, DotY, 0, 0, 1365, 767, 0xFF66FF, 0, Fast RGB
    CoordMode, Mouse, Screen
    PixelSearch, FoundX, FoundY, 0, 0, 1366, 768, 0xFCE839, 0, Fast RGB
    If ErrorLevel = 0
    {
        Click, %FoundX%, %FoundY%, 0
        Sleep, 10
    }
    Else If ErrorLevel
    {
        Click, %DotX%, %DotY%, 0
        Sleep, 10
    }
    Else If ErrorLevel
    {
        Click, %OranX%, %OranY%, 0
        Sleep, 10
    }
}
[Mod edit: [code][/code] tags added]
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: Help with Else If

08 Apr 2020, 06:14

First you should put CoordMode, Mouse, Screen and CoordMode, Pixel, Screen at the top, no matter which version you use:

Code: Select all

CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
; whatever
Btw, currently you are just searching for the last color (well, no, it depends), as Errorlevel always gets overwritten by the last search, no matter what was found before... you have to think about your search logic and the order of things you want to do.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DaveyW, RussF, ymnaren and 154 guests