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
ImageSearch and errorlevel Topic is solved
- donaldthejohn
- Posts: 25
- Joined: 27 Mar 2018, 20:23
ImageSearch and errorlevel
I am a noob at coding, PLEASE be patient with me
Re: ImageSearch and errorlevel
this is what your code looks like to the interpreter:
tl;dr avoing getting into the habit of writing braceless code
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
- donaldthejohn
- Posts: 25
- Joined: 27 Mar 2018, 20:23
Re: ImageSearch and errorlevel
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 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
Re: ImageSearch and errorlevel
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)donaldthejohn wrote:what would be the purpose of a return.
'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
}
Re: ImageSearch and errorlevel
Hey bud,
Use braces or the interpreter will assume only the first line is part of the executing sequence under the statement.
Like so:
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!
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
}
Cheers!
I am your average ahk newbie. Just.. a tat more cute.
- donaldthejohn
- Posts: 25
- Joined: 27 Mar 2018, 20:23
Re: ImageSearch and errorlevel
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
Re: ImageSearch and errorlevel Topic is solved
Hey bud,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
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
I am your average ahk newbie. Just.. a tat more cute.
- donaldthejohn
- Posts: 25
- Joined: 27 Mar 2018, 20:23
Re: ImageSearch and errorlevel
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
Who is online
Users browsing this forum: RandomBoy and 234 guests