Imagesearch macro

Ask gaming related questions (AHK v1.1 and older)
LadyGamer
Posts: 61
Joined: 14 Oct 2017, 07:28

Imagesearch macro

Post by LadyGamer » 04 Jul 2022, 03:16

Hello!, been a while since the last time i used ahk, also my code writing is really bad, so i hope someone can write this macro, please.

I need an infinite loop with this actions.

. image1: search for this image on a specific screen area, if "not found" send "e", this will make the image to show up, when image1 is detected wait 5 to 7 seconds, then send "w", then start from the begining again.

. image2: IT will appear randomly on certain area of the screen, when it appears, image1 actions should stop from working to not interfere with this actions , then resume image1 actions after this is done.

. image3: Same as above, but this image should have also priority of exection over image2, so both actions don't take place at the same time.

Basically:

Code: Select all

; #1
; stop while image2 and image3 are being executed, then resume.
imagesearch image1 not on the screen
send e
imagesearch image1 on the screen
sleep 5 to 7 (random sleep time)
send w

; #2
; if image3 is found at the same time, wait for those actions first, then resume this one.

imagesearch image2 on the screen
send e
sleep 10
send x

; #3
; always priority of execution
imagesearch image3 on the screen
send e
sleep 10
send z
[Mod edit: [code][/code] tags added.]

The images i'm using are on the same folder as the script.

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Imagesearch macro

Post by mikeyww » 04 Jul 2022, 07:30

Code: Select all

#Persistent
timerFreq  = 200
dir        = %A_ScriptDir%\images
png       := ["macros", "pipe", "microHDMI"]
image     := []
For each, fn in png
 If !FileExist(file := dir "\" fn ".png") {
  MsgBox, 48, Error, File not found. Aborting.`n`n%file%
  Return
 } Else image.Push(file)
CoordMode, Pixel
SetTimer, 3, %timerFreq%
SetTimer, 2, %timerFreq%
SetTimer, 1, %timerFreq%
1:
ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, % image.1
If !ErrorLevel {
 ToolTip, 1: Sleeping
 SoundBeep, 1500
 Random, rnd, 5000, 7000
 Sleep, rnd
 ToolTip
 SoundBeep, 1000
 Send w
} Else Send e
Return

2:
ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, % image.2
If ErrorLevel
 Return
SetTimer, 1, Off
Send e
ToolTip, 2: Sleeping
SoundBeep, 1500
Sleep, 10000
ToolTip
SoundBeep, 1000
Send x
SetTimer, 1, %timerFreq%
Return

3:
SoundBeep, 2500
ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, % image.3
If ErrorLevel
 Return
SetTimer, 1, Off
SetTimer, 2, Off
Send e
ToolTip, 3: Sleeping
SoundBeep, 1500
Sleep, 10000
ToolTip
SoundBeep, 1000
Send z
SetTimer, 1, %timerFreq%
SetTimer, 2, %timerFreq%
Return

LadyGamer
Posts: 61
Joined: 14 Oct 2017, 07:28

Re: Imagesearch macro

Post by LadyGamer » 05 Jul 2022, 06:44

mikeyww wrote:
04 Jul 2022, 07:30
@mikey
Hey thanks mikey , glad to see you again i hope you are doing well.
I've tested the script, but im not sure if i set my images correctly, also it needs to search only a small portion of the screen because the image it's too small, when I tried to do that it doesn't work.
This is what i got, i just set the first image to test.

Code: Select all

#Persistent
timerFreq  = 200
dir        = %A_ScriptDir%\images
png       := ["image1", "image2", "image3"] ; those are my img files
image     := []
For each, fn in png
 If !FileExist(file := dir "\" fn ".png") {
  MsgBox, 48, Error, File not found. Aborting.`n`n%file%
  Return
 } Else image.Push(file)
CoordMode, Pixel
SetTimer, 3, %timerFreq%
SetTimer, 2, %timerFreq%
SetTimer, 1, %timerFreq%
1:
ImageSearch, OutputVarX, OutputVarY, 773, 970, 804, 980, *50 % image.1 ;i've tried this and the "50" shade variation can't be used, the script sends error, so i tried without it and the error is gone but the search doesn't seem working, idk if the image is not detected or maybe i just wrote the search line incorrectly. Also im not sure if i should keep "image.1" or change it for somethign else. (btw in an old and very simple script i had, without the "50" the script didn't find the image).

If !ErrorLevel {
 ToolTip, 1: Sleeping
 SoundBeep, 1500
 Random, rnd, 5000, 7000
 Sleep, rnd
 ToolTip
 SoundBeep, 1000
 Send w
} Else Send e
Return

2:
ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, % image.2
If ErrorLevel
 Return
SetTimer, 1, Off
Send e
ToolTip, 2: Sleeping
SoundBeep, 1500
Sleep, 10000
ToolTip
SoundBeep, 1000
Send x
SetTimer, 1, %timerFreq%
Return

3:
SoundBeep, 2500
ImageSearch,,, 0, 0, A_ScreenWidth, A_ScreenHeight, % image.3
If ErrorLevel
 Return
SetTimer, 1, Off
SetTimer, 2, Off
Send e
ToolTip, 3: Sleeping
SoundBeep, 1500
Sleep, 10000
ToolTip
SoundBeep, 1000
Send z
SetTimer, 1, %timerFreq%
SetTimer, 2, %timerFreq%
Return

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Imagesearch macro

Post by mikeyww » 05 Jul 2022, 07:02

Use a separate two-line script to get the search working first. First line is the search. On second line, display the ErrorLevel.

LadyGamer
Posts: 61
Joined: 14 Oct 2017, 07:28

Re: Imagesearch macro

Post by LadyGamer » 05 Jul 2022, 07:40

mikeyww wrote:
05 Jul 2022, 07:02
Use a separate two-line script to get the search working first. First line is the search. On second line, display the ErrorLevel.
im sorry for my ignorance, i don't know how to do it

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Imagesearch macro

Post by mikeyww » 05 Jul 2022, 07:47

Use ImageSearch line from the script, but with a path to a known static PNG image. Skip variation. If needed, put CoordMode, Pixel before it. After, use MsgBox % ErrorLevel.

LadyGamer
Posts: 61
Joined: 14 Oct 2017, 07:28

Re: Imagesearch macro

Post by LadyGamer » 05 Jul 2022, 08:49

@mikeyww
I made the changes, now the images are being detected, but it has some flaws.
Also I changed the sent keys to no get confused when testing.
For example when the image3 is on the screen the macro sends this patern each x seconds "e1d" "e1d" "e1d" "e1d" etc, also when image2 is on the screen it sometimes sends "c11b". looks like something is mixed on the script.

Code: Select all

#Persistent
timerFreq  = 200
dir        = %A_ScriptDir%
png       := ["image1", "image2", "image3"]
image     := []
For each, fn in png
 If !FileExist(file := dir "\" fn ".png") {
  MsgBox, 48, Error, File not found. Aborting.`n`n%file%
  Return
 } Else image.Push(file)
CoordMode, Pixel
SetTimer, 3, %timerFreq%
SetTimer, 2, %timerFreq%
SetTimer, 1, %timerFreq%
1:
ImageSearch, OutputVarX, OutputVarY, 773, 970, 804, 980, *50 %A_WorkingDir%\image1.png 
If !ErrorLevel {
 ToolTip, 1: Sleeping
 SoundBeep, 1500
 Random, rnd, 5000, 7000
 Sleep, rnd
 ToolTip
 SoundBeep, 1000
 Send a
} Else Send 1
Return

2:
ImageSearch, OutputVarX, OutputVarY, 774, 230, 1058, 400, %A_WorkingDir%\image2.png
If ErrorLevel
 Return
SetTimer, 1, Off
Send b
ToolTip, 2: Sleeping
SoundBeep, 1500
Sleep, 10000
ToolTip
SoundBeep, 1000
Send c
SetTimer, 1, %timerFreq%
Return

3:
SoundBeep, 2500
ImageSearch, OutputVarX, OutputVarY, 774, 230, 1058, 400, %A_WorkingDir%\image3.png
If ErrorLevel
 Return
SetTimer, 1, Off
SetTimer, 2, Off
Send d
ToolTip, 3: Sleeping
SoundBeep, 1500
Sleep, 10000
ToolTip
SoundBeep, 1000
Send e
SetTimer, 1, %timerFreq%
SetTimer, 2, %timerFreq%
Return
image2 and 3 coords are the same because they're next to each other, i just cover one with notepad open to test.

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Imagesearch macro

Post by mikeyww » 05 Jul 2022, 09:16

How to debug your program? Follow the script line by line. Use MsgBox to display the value of each variable, conditional statement, search, and function. That will enable you to pinpoint the problem quickly. Is 1 being sent? Does that mean that timer "1" is running? If so, then why is it running? What enabled it to run? Is there more than one line that can start timer 1? If so, which lines are they, and which line actually started the timer? This is the process of investigation. You can use ListLines, etc., or visual confirmations as noted here.

The script seems to be doing what you told it to do. If you need to alter it, you can adjust the logic, order, flow, commands, etc. Look back to your original decription in your first post; see if the behavior matches.

LadyGamer
Posts: 61
Joined: 14 Oct 2017, 07:28

Re: Imagesearch macro

Post by LadyGamer » 06 Jul 2022, 04:21

I tried with your instructions, but I couldn't make it work :/ , as I said before im really bad at ahk, i only know the very basics.
The script ignores the sleep time between keys, ex, "d" "e" are sent togheter, also when image3 disappears and the macro start looking again for image1, the search starts with an "e", the spam looks like this: e11111111.

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Imagesearch macro

Post by mikeyww » 06 Jul 2022, 06:06

The script is just doing what it says to do, so you would need to examine it and adjust the logic to match your needs. I cannot do that.

You have the tools here: ImageSearch searches for the image. SetTimer starts or stops a labeled subroutine. You just need to put them in the order in which you want them to happen.

I believe that the script matches the description that you provided, so a suggestion for the next step is to examine your description rather than the script itself.

Best wishes.

Post Reply

Return to “Gaming Help (v1)”