ImageSearch and errorlevel Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
donaldthejohn
Posts: 25
Joined: 27 Mar 2018, 20:23

ImageSearch and errorlevel

12 May 2018, 18:39

When I run this, autohotkey says "ELSE with no matching IF" and points to line 13. Tried chaning bits of code here and there, probably something simple im missing, but can't find it.
Code:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

F4::
Loop
{
ImageSearch, ImageX, ImageY, 0, 0, 1920, 1080, D:\Main\etc\etc\ahk\Sword_Active.PNG
if (errorlevel = 1)
sleep 100
send, 1
else if (errorlevel = 2)
msgbox, cant search
else if (errorlevel = 0)
continue
sleep 100
click
}
F5::ExitApp
I am a noob at coding, PLEASE be patient with me :D
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: ImageSearch and errorlevel

12 May 2018, 18:50

this is what your code looks like to the interpreter:

Code: Select all

F4::
	Loop
	{
		ImageSearch, ImageX, ImageY, 0, 0, 1920, 1080, D:\Main\etc\etc\ahk\Sword_Active.PNG
		
		if (errorlevel = 1)
		{
			sleep 100
		}

		send, 1
		
		else if (errorlevel = 2) ; this has no matching if
		{
			msgbox, cant search
		}

		else if (errorlevel = 0) ; this doesnt either
		{
			continue
		}

		sleep 100
		click
	}
; also, hotkey Return statement missing, unless of course you intended it that way

F5::ExitApp
tl;dr avoing getting into the habit of writing braceless code
User avatar
donaldthejohn
Posts: 25
Joined: 27 Mar 2018, 20:23

Re: ImageSearch and errorlevel

12 May 2018, 19:26

Thanks for the help!
I got the error with some of the code manipulations that a statement has "no matching if" and i'm not certain on what that means. Also, what would be the purpose of a return.
I'm a noob at coding!
I am a noob at coding, PLEASE be patient with me :D
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: ImageSearch and errorlevel

13 May 2018, 03:47

donaldthejohn wrote:what would be the purpose of a return.
u press F4, code starts executing top to bottom. if u dont have a Return at the end of the F4 hotkey, the next thing to be executed is the F5 hotkey, which will shutdown your script. in your particular case, its not of great relevance whether u have a return or not, since youll never break out of your neverending loop(thats another can of worms in not opening)

'no matching if' means u cant have standalone else or else if statements in your script, ie there cant be any commands/statements in between as is the case here:

Code: Select all

if (errorlevel = 1)
		{
			sleep 100
		}

		send, 1 ; no bueno
		; comments or whitespace is allowed
		else if (errorlevel = 2)
		{
			msgbox, cant search
		}
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: ImageSearch and errorlevel

13 May 2018, 04:01

Hey bud,

Use braces or the interpreter will assume only the first line is part of the executing sequence under the statement.
Like so:

Code: Select all

if (errorlevel = 1) {
    sleep 100
    send, 1
}
Since you didn't put braces, only sleep 100 was under the statement. So send,1 was outside and would have always been executed regardless of whether the if was passed. If you want it to work like that, then you must mention send, 1 after the else's. Since you put it before the else's there was no connection between the if else's and first if therefore you got that message.

Cheers! :D
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
donaldthejohn
Posts: 25
Joined: 27 Mar 2018, 20:23

Re: ImageSearch and errorlevel

13 May 2018, 08:38

Okay i've read this and am able to make sense of it, or believe i am, i had to change the code to a PixelGetColor since an image would be changing. I get the "else with no matching if" at line 19, or the else after "Msgbox, color not found". I'm not seeing a command or case inbetween similar to the "send, 1".

Code: Select all

F4::
	Loop {
		
		PixelGetColor, SwordActive, 883, 1017
		if (ErrorLevel) {
			Msgbox, there is a problem
		}

		else {
			PixelSearch, ColorX, ColorY, 0, 0, 1920, 1080, %SwordActive%
			if (ErrorLevel) {
				Msgbox, color not found
			{
			else {
				Continue
			}
		}

		Sleep 100
		Click
	}

F5::Pause
I am a noob at coding, PLEASE be patient with me :D
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: ImageSearch and errorlevel  Topic is solved

13 May 2018, 09:53

donaldthejohn wrote:Okay i've read this and am able to make sense of it, or believe i am, i had to change the code to a PixelGetColor since an image would be changing. I get the "else with no matching if" at line 19, or the else after "Msgbox, color not found". I'm not seeing a command or case inbetween similar to the "send, 1".

Code: Select all

F4::
	Loop {
		
		PixelGetColor, SwordActive, 883, 1017
		if (ErrorLevel) {
			Msgbox, there is a problem
		}

		else {
			PixelSearch, ColorX, ColorY, 0, 0, 1920, 1080, %SwordActive%
			if (ErrorLevel) {
				Msgbox, color not found
			{
			else {
				Continue
			}
		}

		Sleep 100
		Click
	}

F5::Pause
Hey bud,

Everything was correct! You had just replaced the closing brace by an opening brace by mistake, after the if and before the faulty else. Hope that clears out why that dialogue was popping up.

Try this:

Code: Select all

F4::
	Loop {
		
		PixelGetColor, SwordActive, 883, 1017
		if (ErrorLevel) {
			Msgbox, there is a problem
		}

		else {
			PixelSearch, ColorX, ColorY, 0, 0, 1920, 1080, %SwordActive%
			if (ErrorLevel) {
				Msgbox, color not found
			}
			else {
				Continue
			}
		}

		Sleep 100
		Click
	}

F5::Pause
Cheers! :D
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
donaldthejohn
Posts: 25
Joined: 27 Mar 2018, 20:23

Re: ImageSearch and errorlevel

13 May 2018, 13:58

Oh my lord, thanks for that. I really didn't think that was a problem, i usually am good with brackets when i use them. Thanks guys.
I am a noob at coding, PLEASE be patient with me :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, Google [Bot], Rohwedder and 266 guests