Newcomer here. I can't seem to get ImageSearch to work no matter what I do.

Ask gaming related questions (AHK v1.1 and older)
YaBoiRexy
Posts: 1
Joined: 13 Mar 2024, 15:06

Newcomer here. I can't seem to get ImageSearch to work no matter what I do.

Post by YaBoiRexy » 13 Mar 2024, 15:17

Hello. I'm trying to automate a simple task for fortnite. Simply, I want AHK to look for the "Festival" button, and press it if it finds it. If not, I want it to scroll down in the menu and try again until it finds it. However, it seems unable to find the image no matter what I do. The only way it's been able to find the image is if I set the variance of ImageSearch to 255 but that doesn't seem very helpful. Even with variance 150 it does not find the image. What do I do? I've tried taking multiple screenshots, different sizes, used jpg png and gif files, opening the app in fullscreen, windowed fullscreen, and windowed, but nothing seems to work. It just does. not. find. the image. Below is my code:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
CoordMode, Mouse, Screen

Input, UserInput, L1, ]
#Persistent
SetTimer, CheckButtonPosition, 4000 ; Check every second
return



; Checks to see if we see the button then runs all 3 error levels
CheckButtonPosition:
    ImageSearch, TopLeftXOfImage, TopLeftYOfImage, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Users\jack\Downloads\festival_icon.jpg


if ErrorLevel = 1 ; If the image is not found
{
    ; Scroll down to the next page
    Send, {WheelDown}
    Sleep, 1000
}

if ErrorLevel = 0 ; If the image is found
{
    ; Move the mouse to the button and click
    Click, %TopLeftXOfImage%, %TopLeftYOfImage%
	Pause
}
 

if ErrorLevel = 2 ; If there is an error that prevents the search
{
	MsgBox, not_working
}
return
II also tried using PixelGetColor, but that doesn't work either. It insists that the colour of the specified pixel does not match what I've specified in the if command, even when Windows Spy confirms it's just what the code should be looking for. Below is my second try (with PixelGetColor instead of ImageSearch) but this does not work either:

Code: Select all

CoordMode, Pixel, Screen
Input, UserInput, L1, ]

PixelGetColor, colour, 219, 241
if (colour == 00FFA4)
{
Click, 219, 241
}
else
{
MsgBox, nope
}
I'm really interested in learning this so if someone could point out what I've done wrong (I'd honestly settle for either solution) then I'd be so happy. :D

[Mod action: Moved topic from AHK v2 help to v1 help, based on posted code. ]

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

Re: Newcomer here. I can't seem to get ImageSearch to work no matter what I do.

Post by mikeyww » 14 Mar 2024, 09:54

Welcome to this AutoHotkey forum!

Capture a small plain PNG image so that you can test with it. You capture and then display that image.

Create a 3-line test script.
  1. On the first line, set your CoordMode.
  2. On the second line, search for the image.
  3. On the third line, display the ErrorLevel from the search. The value will be 0, 1, or 2.
This test with three lines will enable you to troubleshoot the process.

Clicking on a found image typically requires matching coord modes for Pixel and Mouse. If you change only one, you will typically get unexpected results.

Troubleshooting a pixel color has three steps.
  1. MouseMove to the coordinate.
  2. Check the color.
  3. Display the color's value.
  • By default, coordinates are relative to the active window.
  • By default, colors are in Blue-Green-Red format.
These tests require no timers, clicks, or Send commands. You are retrieving and displaying information.

Post Reply

Return to “Gaming Help (v1)”