How do I stick a filepath in a variable for use in ImageSearch()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
locoleos
Posts: 4
Joined: 27 Mar 2023, 06:57

How do I stick a filepath in a variable for use in ImageSearch()

Post by locoleos » 27 Mar 2023, 07:20

I want to stick a filepath in a variable so that I can pass it to a function and use it in an Imagesearch.

Code: Select all


^4:: ;Ferie
	Billede:=%A_ScriptDir%\fravaer.png
	FindImage(%Billede%)
	Mouseclick left
return

FindImage(Image)
{
	loopCount := 0
	startOfIteration:
	loopCount++
	while(loopCount < 150)
	
	{
		ImageSearch foundX, foundY, 0, 0, 1920, 1080, *30 *w10*loopcount *h-1 %Image%
		sleep 30
		if(ErrorLevel = 0)
			{
			;MsgBox image found: %foundX% x %foundY%
			MouseMove foundX, foundY, 0
			return
			}
		else  if (ErrorLevel = 1) 
		 	{
			goto startOfIteration
			}
		else (ErrorLevel = 2)
			{
				MsgBox Error Something went wrong!
				return
			}
	
	MsgBox Options Chain Center not found!
	}
Return 
}
Is something like what I think should work. But it throws me an exception when I try to run it, declaring the \ in definition of the Billede variable an illegal character.

If I embed the filepath in the function without passing it as an argument it works, but obviously that makes it rather pointless as a function.

This kind of makes me suspect that I need a way to tell Billede that it's a variable that's supposed to be storing filepaths, but AHK script is not a typed language so I have no idea how to go about doing that. Help?

gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: How do I stick a filepath in a variable for use in ImageSearch()

Post by gregster » 27 Mar 2023, 07:29

Both := and functions use expression syntax:

Code: Select all

Billede := A_ScriptDir "\fravaer.png"
	FindImage(Billede)

locoleos
Posts: 4
Joined: 27 Mar 2023, 06:57

Re: How do I stick a filepath in a variable for use in ImageSearch()

Post by locoleos » 27 Mar 2023, 07:57

Should I use something else to pass the filepath along then? Because even if I define Image:= A_ScriptDir "\fravaer.png" locally inside the function, it still doesnt work when I pass it to ImageSearch. ImageSearch seems to accept %A_ScriptDir%\fravaer.png and nothing else.

gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: How do I stick a filepath in a variable for use in ImageSearch()

Post by gregster » 27 Mar 2023, 08:05

No, it's equivalent (if done right); Imagesearch can handle variables.
If you are unsure, show the updated code.

What means "still doesnt work"? Still the same error message?
Or what's the errorlevel of Imagesearch then?

Btw, if your script directory is also the script's working directory, you could just use as a relative path:

Code: Select all

Billede := "fravaer.png"

Post Reply

Return to “Ask for Help (v1)”