imagesearch then click

Ask gaming related questions (AHK v1.1 and older)
jgcampbell300
Posts: 7
Joined: 16 Sep 2019, 12:52

imagesearch then click

17 Feb 2020, 15:41

ok so I am having a bit of trouble learning how this works.
So I have 5 different images, if it comes up image 1 it should hit f1 any of the other images it should hit f2. I cant seem to get things right so here is my latest attempt.

Code: Select all

F7::
  
  send, {f1}
  sleep, 70-140


  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\Pix\Water.png
    if (ErrorLevel = 2)
      MsgBox Could not conduct the search. 
    else
      send, {f2}
      sleep, 100-150 


  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\Pix\Fertilize.png
    if (ErrorLevel = 2)
      MsgBox Could not conduct the search. 
    else
      send, {f2}
      sleep, 100-150 


  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\Pix\Pick.png
    if (ErrorLevel = 2)
      MsgBox Could not conduct the search. 
    else
      send, {f2}
      sleep, 100-150 


  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\Pix\Harvest.png
    if (ErrorLevel = 2)
      MsgBox Could not conduct the search. 
    else
      send, {f2}
      sleep, 100-150 
      
I had it working ok at one point but lost that script ... and almost had it working to click %foundx%, %foundy% but i think game guard is jacking with the info i get back from the game... it always says its some where around 1650x48 when it is far far off from that like 900x650 or anywhere else on screen ... gave up on that for now ... but if you guys have any input on that issue i would be greatfull
jgcampbell300
Posts: 7
Joined: 16 Sep 2019, 12:52

Re: imagesearch then click

17 Feb 2020, 17:30

Ok Next try .... but im getting the else's jacked up I think

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.



F1::Pause
Loop,
{
  Random, randsleep, 85, 150
  Send, {F1}
  Sleep, %randsleep%
  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Check.png
    if ErrorLevel = 2
      tooltip Could not conduct the search.
    else if ErrorLevel = 1
      ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Water.png
        if ErrorLevel = 2
          tooltip Could not conduct the search.
        else if ErrorLevel = 1
          ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Fertilize.png
            if ErrorLevel = 2
              tooltip Could not conduct the search.
            else if ErrorLevel = 1
              ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Pick.png
                if ErrorLevel = 2
                  tooltip Could not conduct the search.
                else if ErrorLevel = 1
                  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Harvest.png
                    if ErrorLevel = 2
                      tooltip Could not conduct the search.
                    else if ErrorLevel = 1
                      Sleep, %randsleep%
                      Return
                    else if ErrorLevel = 0
                      Send, {F2}
                      Sleep, %randsleep%
                      Return
             
                else if ErrorLevel = 0
                  Send, {F2}
                  Sleep, %randsleep%
                  Return
            else if ErrorLevel = 0
              Send, {F2}
              Sleep, %randsleep%
              Return
        else if ErrorLevel = 0
          Send, {F2}
          Sleep, %randsleep%
          Return
    else if ErrorLevel = 0
      Sleep, %randsleep%
      Return
}


esc::
    msgbox script stopped
exitapp
jgcampbell300
Posts: 7
Joined: 16 Sep 2019, 12:52

Re: imagesearch then click

17 Feb 2020, 17:59

Ok think i found a problem with the way i was useing If's ... but gota go to work so maybe when i get home.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
[code]#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.



F1::Pause
Loop,
{
  Random, randsleep, 85, 150
  Send, {F1}
  Sleep, %randsleep%
  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Check.png
    if (ErrorLevel = 2){
      tooltip Could not conduct the search.
    }else if (ErrorLevel = 1){
      ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Water.png
        if (ErrorLevel = 2){
          tooltip Could not conduct the search.
        }else if (ErrorLevel = 1){
          ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Fertilize.png
            if (ErrorLevel = 2){
              tooltip Could not conduct the search.
            }else if (ErrorLevel = 1){
              ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Pick.png
                if (ErrorLevel = 2){
                  tooltip Could not conduct the search.
                }else if (ErrorLevel = 1){
                  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Harvest.png
                    if (ErrorLevel = 2){
                      tooltip Could not conduct the search.
                      }else if (ErrorLevel = 1){
                      Sleep, %randsleep%
                      Return
                    }else{
                      Send, {F2}
                      Sleep, %randsleep%
                      Return
                }else{ 
                  Send, {F2}
                  Sleep, %randsleep%
                  Return
            }else{
              Send, {F2}
              Sleep, %randsleep%
              Return
        }else{
          Send, {F2}
          Sleep, %randsleep%
          Return
    }else{
      Sleep, %randsleep%
      Return
}


esc::
    msgbox script stopped
exitapp

jgcampbell300
Posts: 7
Joined: 16 Sep 2019, 12:52

Re: imagesearch then click

18 Feb 2020, 12:17

Well at least it dosnt throw an error ... but still gota fix it ... dosnt work yet

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
CoordMode, Pixel, Screen  ; Interprets the coordinates below as relative to the screen rather than the active window.

F8::
  Loop
  { Click
    sleep 100
      If (!GetKeyState("F8","p"))
    break
  }
Return

$F9::
  Loop
  { Click, R
    sleep 100
      If (!GetKeyState("F9","p"))
    break
  }
Return

f10::
  Loop, 5 
  {
    Send 1
    Sleep, 100
    Send, 2
    Sleep, 100
    Send, 3
    Sleep, 100
    Send, 4
    Sleep, 100
    Send, 5
    Sleep, 100
    Send, 6
    Sleep, 100
  break
  }
Return

F7::Pause
Loop,

  Random, randsleep, 85, 150
  Send, {F1}
  Sleep, %randsleep%
    
  ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Check.png
  If (ErrorLevel = 2)
  {
   ToolTip , Error no search.
  }
  Else If (ErrorLevel = 1)
  {
   ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Water.png
   If (ErrorLevel = 2)
   {
    ToolTip , Error no Search.
   }
   Else If (ErrorLevel = 1)
   {
    ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Fertilize.png
    If (ErrorLevel = 2)
    {
     ToolTip , Error no Search.
    }
    Else If (ErrorLevel = 1)
    {
     ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Pick.png
     If (ErrorLevel = 2)
     {
      ToolTip , Error no Search.
     }
     Else If (ErrorLevel = 1)
     {
      ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *150 %A_ScriptDir%\pix\Pick.png
      If (ErrorLevel = 2)
      {
       ToolTip , Error no Search.
      }
      Else If (ErrorLevel = 1)
      {
       ToolTip , Done Starting over.
      }
      Else 
      {
       Send, {F2}
       Sleep, %randsleep%
      }      
     }
     Else 
     {
      Send, {F2}
      Sleep, %randsleep%
     }
    }
    Else 
    {
     Send, {F2}
     Sleep, %randsleep%
    }
   }
   Else 
   {
    Send, {F2}
    Sleep, %randsleep%
   }
  }
  Else
  {
    ToolTip , Done Starting over.
  }
jgcampbell300
Posts: 7
Joined: 16 Sep 2019, 12:52

Re: imagesearch then click

18 Feb 2020, 13:38

this one works kina but is buggy as crap ... i am running out of ideas

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.



F7::
  Random, randsleep, 76, 100
  Send, {F1}
  Sleep, %randsleep%
    
 ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *100 %A_ScriptDir%\pix\Check.png
  If (ErrorLevel = 2)
  {
   ToolTip , Error no search.
  }
  Else If (ErrorLevel = 1)
  {
   ;Sleep, %randsleep%
   ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *100 %A_ScriptDir%\pix\Water.png
   If (ErrorLevel = 2)
   {
    ToolTip , Error no Search.
   }
   Else If (ErrorLevel = 1)
   {
    ;Sleep, %randsleep%
    ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *100 %A_ScriptDir%\pix\Fertilize.png
    If (ErrorLevel = 2)
    {
     ToolTip , Error no Search.
    }
    Else If (ErrorLevel = 1)
    {
     ;Sleep, %randsleep%
     ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *100 %A_ScriptDir%\pix\Pick.png
     If (ErrorLevel = 2)
     {
      ToolTip , Error no Search.
     }
     Else If (ErrorLevel = 1)
     {
      ;Sleep, %randsleep%
      ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *100 %A_ScriptDir%\pix\Harvest.png
      If (ErrorLevel = 2)
      {
       ToolTip , Error no Search.
      }
      Else If (ErrorLevel = 1)
      {
       ;Sleep, %randsleep%
       ToolTip , (Nothing Found) Done Starting over.
      }
      Else 
      {
       ;Sleep, %randsleep%
       ToolTip , (Harvest Found) Done Starting over.
       Send, {F2}
       Sleep, %randsleep%
      }      
     }
     Else 
     {
      ;Sleep, %randsleep%
      ToolTip , (Pick) Done Starting over.
      Send, {F2}
      Sleep, %randsleep%
     }
    }
    Else 
    {
     ;Sleep, %randsleep%
     ToolTip , (Fertilizer Found) Done Starting over.
     Send, {F2}
     Sleep, %randsleep%
    }
   }
   Else 
   {
    ;Sleep, %randsleep%
    ToolTip , (Water Found) Done Starting over.
    Send, {F2}
    Sleep, %randsleep%
   }
  }
  Else
  {
   ;Sleep, %randsleep%
   ToolTip , (Check Found) Done Starting over.
  }

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: imagesearch then click

18 Feb 2020, 13:41

When you put a command on the same line as the hotkey label like F7::Pause, then nothing below it gets executed. Putting a command on the same line means you want only that line executed when you press that hotkey. If you want a block of commands to be executed, they need to start on the next line. I'm not sure if you really want to pause it every other time you press it then execute the code below it, but it won't even do that unless you put Pause on the line below the F7::.

I haven't looked at the other details, but that's the main thing that would prevent that code from executing.

Edit: I see you changed that in another post you just made while I was typing the above.
jgcampbell300
Posts: 7
Joined: 16 Sep 2019, 12:52

Re: imagesearch then click

18 Feb 2020, 14:52

Ya ... i was thinking i would work on a way to pause/resume the script later after it works. I think i may be hitting some kind of timing issues ... it randomly works now but very unstable
As you can see I am very new to this stuff so im looking for any suggestions I can get on how to do things in a more better way. Thanks for your assist by the way.
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: imagesearch then click

18 Feb 2020, 17:00

The logic looks correct, although I'm not sure you need to nest the if/then statements. You could have just one after the other because only one will be true at a time. And if you can do that, then you could just cycle through all the files in your pix directory in a loop like this (assuming you want to use all the files in that directory):

Code: Select all

F7::
	Random, randsleep, 76, 100
	Send, {F1}
	Sleep, %randsleep%
	
	loop, Files, %A_ScriptDir%\pix\*.png, F
	{
		ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *100 %A_LoopFileFullPath%
		If (ErrorLevel = 2)
			ToolTip , Error no search.
		Else If (ErrorLevel = 1)
		{
			;Sleep, %randsleep%
			ToolTip , (Nothing Found) Done Starting over.
		}
		Else 
		{
			;Sleep, %randsleep%
			SplitPath, A_LoopFileFullPath,,,, Item
			ToolTip , (%Item% Found) Done Starting over.
			Send, {F2}
			Sleep, %randsleep%
		}
	}
return
That makes it a lot more manageable so you can try different things and not have to change it in a bunch of places.
jgcampbell300
Posts: 7
Joined: 16 Sep 2019, 12:52

Re: imagesearch then click

19 Feb 2020, 07:40

thanks ... that helped alot,
So I have gotten a bit further but out of time again...
It kind of works now but loop gets stuck and it dosnt recognize the picture some times
I think i can get it to recognize by redoing my pix or maybe increasing the *n but the loop im kind of stuck on
Anyways heres what i have so far ... any ideas

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

nvalue = 125

F7::
	Random, randsleep, 100, 200
	Send, {F1}
	Sleep, %randsleep%

loop,
{	
	ImageSearch, FoundCX, FoundCY, 0, 0, A_ScreenWidth, A_ScreenHeight, *%nvalue% %A_ScriptDir%\Check.png
	If (ErrorLevel = 2)
		ToolTip , Error no Search Preformed.
		
	Else If (ErrorLevel = 1)
	{
		loop, Files, %A_ScriptDir%\pix\*.png, F
		{
			Sleep, %randsleep%
			ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *%nvalue% %A_LoopFileFullPath%
			Sleep, %randsleep%
			If (ErrorLevel = 2)
				ToolTip , Error no search Preformed.
			Else If (ErrorLevel = 1)
			{
				ToolTip , 2. (Nothing Found) Done Starting over.
			}
			Else 
			{
				SplitPath, A_LoopFileFullPath,,,, Item
				ToolTip , (%Item% Found) Done Starting over.
				Send, {F2}
				Sleep, %randsleep%
				Send, {F1}
				Sleep, %randsleep%
			}
		}
		return
	}
	Else
	{
		ToolTip , 1. (Nothing Found) Trying Again.
		Send, {F1}
		Sleep, %randsleep%
	}			
}
return
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: imagesearch then click

19 Feb 2020, 08:45

If it sometimes works and sometimes doesn't, it might be because there is some background in the picture that changes. If so, you should make your reference images so that they contain no background at all. The smaller you can make the reference image and still be unique, the better. If you can't totally get rid of the background because of their irregular shapes, you can black out the background pixels in MS Paint, and use the option *TransBlack in your ImageSearch command. That will have it ignore those pixels when it performs the search.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 102 guests