wrong syntax in for loop Which reference to image object Topic is solved

Ask gaming related questions (AHK v1.1 and older)
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

wrong syntax in for loop Which reference to image object

Post by HiSoKa » 22 Feb 2021, 02:53

hello,
this me code i trying to search for two image, if found some image do something ,if found other image do something else.
this is mikey code with some adjustment by me ,
i dont know why image search didn't respond ,
i know wrong syntax in line 11,

Can anyone correct it for me please.

Code: Select all

CoordMode, Pixel
CoordMode, Mouse


NoHarm = img5.png
Harm = img6.png

f1::
loop
{	
	For k, v in ["%NoHarm%" , "%Harm%"] 
{
	ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, %A_ScriptDir%\%v%
	If ErrorLevel
	Continue
	Gosub, %v%
	Break
}
Return
}


NoHarm:
MsgBox, No Harm Found!
Return
		
Harm:
MsgBox, Found Harm!
Return	
			
			
			
End::ExitApp  ; Exit script with Escape key
^!p::Pause    ; Pause script with Ctrl+Alt+P
^!s::Suspend  ; Suspend script with Ctrl+Alt+S
^!r::Reload   ; Reload script with Ctrl+Alt+R

thanks in advance..
Last edited by BoBo on 22 Feb 2021, 04:05, edited 1 time in total.
Reason: Moved to Gaming section.

just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: wrong syntax in for loop Which reference to image object

Post by just me » 22 Feb 2021, 03:25


User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: wrong syntax in for loop Which reference to image object  Topic is solved

Post by Hellbent » 22 Feb 2021, 03:31

This? Can't test (obviously).

Code: Select all

#SingleInstance, Force
CoordMode, Pixel, Screen 
CoordMode, Mouse, Screen
Images := { NoHarm: A_ScriptDir "\img5.png" , Harm: A_ScriptDir "\img6.png"}
return

f1::
	Loop, 	{
		for k, v in Images	{
			ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, % v
			if(!ErrorLevel){
				Gosub, % k
				Break, 2
			}
		}
	}
	return
	
NoHarm:
MsgBox, No Harm Found!
Return
		
Harm:
MsgBox, Found Harm!
Return	
			
End::ExitApp  ; Exit script with Escape key
^!p::Pause    ; Pause script with Ctrl+Alt+P
^!s::Suspend  ; Suspend script with Ctrl+Alt+S
^!r::Reload   ; Reload script with Ctrl+Alt+R	

just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: wrong syntax in for loop Which reference to image object

Post by just me » 22 Feb 2021, 03:35

Addition: You must also remove the double-quotes.

And you should use use Hellbent's solution.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: wrong syntax in for loop Which reference to image object

Post by HiSoKa » 22 Feb 2021, 03:42

@just me & @Hellbent ,,,
thanks u guys , my code work well now .. :) :)

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: wrong syntax in for loop Which reference to image object

Post by Hellbent » 22 Feb 2021, 04:05

@just me thx :D , but im curiosity i trying to figout why my code didn't work , i followed Your instructions
i delete double-quotes and percent sign, still didn't work Regarding my method.

Code: Select all

f1::
loop
{	
	For k, v in [ NoHarm , Harm] ;<---- This is an array. The contents are "img5.png" and "img6.png"
{
	ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, % A_ScriptDir "\" v
	If ErrorLevel
	Continue
	Gosub, %v%  ;<----- v is equal to "img5.png" or "img6.png". There is no label called "img5.png" or "img6.png"
	Break
}
Return
}


User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: wrong syntax in for loop Which reference to image object

Post by HiSoKa » 22 Feb 2021, 04:14

@Hellbent, sorry i deleted my reply by mistake, thx a lots for your time hellbent ..



this is my previous reply before hellbent answer, i deleted it by mistake:
@just me thx :D , but im curiosity i trying to figout why my code didn't work , i followed Your instructions
i delete double-quotes and percent sign, still didn't work Regarding my method.
Last edited by HiSoKa on 22 Feb 2021, 04:22, edited 1 time in total.

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: wrong syntax in for loop Which reference to image object

Post by Hellbent » 22 Feb 2021, 04:20

HiSoKa wrote:
22 Feb 2021, 04:14
@Hellbent, sorry i deleted my reply by mistake, thx a lots for your time hellbent ..
Np, hope that helped.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: wrong syntax in for loop Which reference to image object

Post by HiSoKa » 22 Feb 2021, 08:46

i have some problem ,
why in my code i cant write name the label as name the object which contents images path (Harm & NoHarm)

this is the code:

Code: Select all

#SingleInstance, Force
CoordMode, Pixel, Screen 
CoordMode, Mouse, Screen
NoHarm = img5.png
Harm = img6.png

f1::
loop
{	
	For k, v in [ NoHarm , Harm]
{
	ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, % A_ScriptDir "\" v
	If ErrorLevel
	Continue
	Gosub, %v%
	Break
}
Return
}

NoHarm:
MsgBox, No Harm Found
return

Harm:
MsgBox, Found Harm
return

End::ExitApp  ; Exit script with Escape key
^!p::Pause    ; Pause script with Ctrl+Alt+P
^!s::Suspend  ; Suspend script with Ctrl+Alt+S
^!r::Reload   ; Reload script with Ctrl+Alt+R	


ahk code.png
ahk code.png (35.17 KiB) Viewed 506 times
when i changed the Label name to name of that Image in the top of script (img5.png, img6.png), everything worked well..

i tried too much but i don't know what is wrong ,
can any one correct to me please..

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: wrong syntax in for loop Which reference to image object

Post by Hellbent » 22 Feb 2021, 09:10

Hellbent wrote:
22 Feb 2021, 04:05

Code: Select all


	Gosub, %v%  ;<----- v is equal to "img5.png" or "img6.png". There is no label called "img5.png" or "img6.png"

Use the first code I posted.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: wrong syntax in for loop Which reference to image object

Post by HiSoKa » 22 Feb 2021, 09:15

i will do, It's the best way ;)

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: wrong syntax in for loop Which reference to image object

Post by Hellbent » 22 Feb 2021, 09:21

HiSoKa wrote:
22 Feb 2021, 09:15
i will do, It's the best way ;)

What you had is equivalent to

Code: Select all


Gosub, img5.png 


img5.png:  ;<------- So your labels would need these names and NOT "Harm" or "NoHarm"
	Tooltip, Hi
	return


User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: wrong syntax in for loop Which reference to image object

Post by HiSoKa » 22 Feb 2021, 09:43

i got it hellbent ..

now in your first code i can rename label like the object name ,
but in my code i can't , must use image name for the label for some reasons.
anyway it's ok .

your first code work well without any problem
and i'm comfortable with it because name of label like name of image object so this is a clear to me and readable easily.

just i thought there are easy way to fix my code as i needed.

Greetings

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: wrong syntax in for loop Which reference to image object

Post by Hellbent » 22 Feb 2021, 09:57

HiSoKa wrote:
22 Feb 2021, 09:43
just i thought there are easy way to fix my code as i needed.
You can fix your code in a way that is almost exactly like you had it, but you would need to do a bunch of if statements and that isn't the best solution.

For example.

*Note: I'm only going to type out enough to show what I mean.

Code: Select all

;Using a switch (requires a recent version of ahk (in the last year or so))
;********************************************************************************************
Switch v
{
	Case "img5.png":
		gosub, NoHarm
	Case "img6.png":
		gosub, Harm
	Default:
		Msgbox, You will never see this msg.	
}

;If
;**************************************************
if( v = "img5.png" )
	gosub, NoHarm
else if( v = "img6.png" )
	gosub, Harm

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: wrong syntax in for loop Which reference to image object

Post by HiSoKa » 22 Feb 2021, 10:02

thanks you Hellbent for the valuable information . im sure that's will help me..

Post Reply

Return to “Gaming Help (v1)”