Trying to create a AHK that clicks and then checks for an image and clicks elsewhere

Ask gaming related questions
bigpapa1222
Posts: 8
Joined: 20 Sep 2017, 16:31

Trying to create a AHK that clicks and then checks for an image and clicks elsewhere

14 Nov 2017, 20:25

[code=autohotkey file=Untitled.ahk][/code]
I'm trying to create a piece of code that loops and checks for an image and if that image exists it clicks on the middle of that image. During that loop it also checks for a different image and if that image exists it clicks on a point waits then clicks on another point. The rate of which the second image appears is semi-random so therefore I don't believe a "sleep" command will work.
This is my code:
Loop
{
ImageSearch, , , 0, 0 , 1920, 1080, D:\Users\Devin\Desktop\gamekit\ratephotos1.PNG
If(ErrorLevel = 0)
{
Click, 1258, 774 ; this needs to be the middle of the image not a point.
Sleep 1500
}
Else if(ErrorLevel = 2)
{
Msgbox, Can't find image
}
ImageSearch, , , 644, 323, 1273, 580, D:\Users\Devin\Desktop\gamekit\ratephotos.PNG
If(ErrorLevel = 0)
{
Click, 835, 435
Sleep 1500
Click, 959, 510
Sleep 3000
}
Else if(ErrorLevel = 2)
{
Msgbox, Can't find image
}
}
F5::ExitApp
User avatar
Exaskryz
Posts: 2688
Joined: 17 Oct 2015, 20:28

Re: Trying to create a AHK that clicks and then checks for an image and clicks elsewhere

14 Nov 2017, 22:25

So the first question about making it click the center: ImageSearch is able to output the X and Y coordinates of the top left of the coordinate of where the picture was found. You're not capturing those at the moment, but if you capture those, then add to the X half of the width of your image, and to the Y half of the height of your image, you can use those for your Click.

Your Loop does run full throttle until it finds an image, which may or may not be desirable. You could consider changing into a SetTimer command to repeat each search independently at whichever rate you need. But as is in the second image search, the Sleep 1500 is necessary if you want it to wait before clicking the second point. The Sleep 3000 is optionally up to you, if you need a cooldown after that second click before it should find any image.
bigpapa1222
Posts: 8
Joined: 20 Sep 2017, 16:31

Re: Trying to create a AHK that clicks and then checks for an image and clicks elsewhere

15 Nov 2017, 17:38

Does this seem correct?:
#Persistent
SetTimer, checkstar, 1500

checkstar:
ImageSearch, topLX, topLY, 0, 0, 1920, 1080, D:\Users\Devin\Desktop\gamekit\ratephotos1.PNG
If(ErrorLevel = 0)
{
Click, %topLX%/2, %topLY%/2
}
Else if(ErrorLevel = 2)
{
Msgbox, , ,Image not found, 2500
}
ImageSearch, , , 0, 0, 1920, 1080, D:\Users\Devin\Desktop\gamekit\ratephotos2.PNG
If(ErrorLevel = 0)
{
Click, 835, 435
Sleep 5000
Click, 959, 510
Sleep 3000
}
Else if(ErrorLevel = 2)
{
Msgbox, , ,Image not found, 2500
}
}
F5::ExitApp
User avatar
Exaskryz
Posts: 2688
Joined: 17 Oct 2015, 20:28

Re: Trying to create a AHK that clicks and then checks for an image and clicks elsewhere

16 Nov 2017, 02:17

That looks like it might work. Not entirely sure about the Click command, as I don't think it supports expressions. You need to create a new variable and use just that variable in the Click command. And simply dividing by 2 isn't going to click the center actually.

Imagine that the image is found at coordinates 400, 400. If the image is 100 pixels wide and 100 pixels tall, that means the image is in a square from 400,400 to 500,500 -- the center is 450, 450. Dividing by 2 gets you to 200,200 which is not even on the picture. Instead, you take 100/2 + 400 = 450.

I don't know about a way for AHK to retrieve the width and height of the image in ImageSearch, but maybe it's already an inbuilt function. But I recommend that you hard code it in this case. If the image is 100x200 (100 wide, 200 tall), you know half the width is 50 and height is 100.

Code: [Select all] [Download] GeSHi © Codebox Plus

ImageSearch, topLX, topLY, 0, 0, 1920, 1080, D:\Users\Devin\Desktop\gamekit\ratephotos1.PNG
If(ErrorLevel = 0)
{
clickX:=topLX + 50
clicky:=topLY + 100
Click, %clickX%, %clickY%
}


Otherwise your code seems OK. You might double check that that MsgBox TimeOut is right though; I think it is in seconds, not milliseconds. But that hopefully has no impact on your code because ErrorLevel = 2 is a more severe error like the image file doesn't exist on your computer.
bigpapa1222
Posts: 8
Joined: 20 Sep 2017, 16:31

Re: Trying to create a AHK that clicks and then checks for an image and clicks elsewhere

16 Nov 2017, 18:21

[code=autohotkey file=Untitled.ahk][/code]So I got the msgbox saying Image two is not found and I only want it to run to check for the second image if the first one isn't found. Thanks for the help so far!
Code:
#Persistent
SetTimer, checkstar, 1500

checkstar:
ImageSearch, topLX, topLY, 0, 0, 1920, 1080, D:\Users\Devin\Desktop\gamekit\ratephotos1.PNG
If(ErrorLevel = 0)
{
clickX:=topLX + 50
clicky:=topLY + 100
Click, %clickX%, %clickY%
}
Else if(ErrorLevel = 1)
{
Msgbox, , ,Image not found1, 1500
}
ImageSearch, , , 0, 0, 1920, 1080, D:\Users\Devin\Desktop\gamekit\ratephotos2.PNG
If(ErrorLevel = 0)
{
Click, 835, 435
Sleep 1500
Click, 959, 510
Sleep 3000
}
Else if(ErrorLevel = 1)
{
Msgbox, , ,Image not found2, 2500
}
F5::ExitApp

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 6 guests