help in code to fix script with image Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 17 Sep 2021, 14:42

there is the coordinate it returns, the image is below
https://imgur.com/a/7FlDSzj

then return this image :
https://imgur.com/a/DOxawfH

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

Re: help in code to fix script with image

Post by boiler » 17 Sep 2021, 14:46

Green Astronaut wrote: already this code:
No, you did not already run that code, at least not in what you described. I just changed the starting x to 910 and left y at 0. You also changed the starting y to 39. That tells us that the 910 is the problem not the 39. We had to verify that to isolate the issue.

Now change the 910 to 713 just to make sure it works again (it should). Then try different values between 713 and 910 and see when it works and when it doesn’t (and what coordinates it reports each time it finds it). What is the highest number for the starting x for which it still works? What does it report for the found location?

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 17 Sep 2021, 15:11

I tested it with a different value, but I'm thinking that the problem is the error level. because I changed the x = 880 and now it works. but it is recognizing the entire image, and I would like it to recognize it only when it is at the end of the bar, I will put three images to explain it better

full bar:
https://imgur.com/a/efuggGP

end of bar :
https://imgur.com/a/IchxCUT
(I would like the script to start the actions when finding the purple color of the bar in this way)

image of the bar I'm using:
https://imgur.com/a/7PBCWHS

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

Re: help in code to fix script with image

Post by boiler » 17 Sep 2021, 15:34

When you change it to 800, it’s not finding the whole image. It’s finding the part that starts at at least x = 800. The MsgBox in the version I posted will tell you exactly where it was found. Keep changing it from 800 to higher values and see what the highest value it will find it with.

What you said about ErrorLevel doesn’t make sense, so there’s no need to discuss that further.

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 17 Sep 2021, 15:47

whenever I put 800 or any higher value it's returning the same as in this image below :

https://imgur.com/a/XD4PFzC

should I test this value?

I put my mouse over the coordinate 894 39 and noticed that the black color was found being 0x000000 as shown in the image below:
https://imgur.com/a/JZoet3J

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

Re: help in code to fix script with image

Post by boiler » 17 Sep 2021, 17:02

If your mouse is where that ToolTip says “Right-click for more options”, then it makes sense. It’s finding the right side of the bar. It’s what I expected — when you search starting at 910, it’s too far to the right, so there isn’t enough bar left to find your reference image. So if you want to find something to the right of the middle part, it will be at least at x = 894 but less than x = 910 because that’s too far over. If you want to find it at 910, then you have to reduce the width of your reference image, probably by 10 or 15 pixels.

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 18 Sep 2021, 06:17

got it, I reduced the image by 15 pixels and kept having the same problem.
what would be an ideal size for the image to be recognized in the game when i put the coordinate of the place i need the verification to take place?

the image below shows the entire bar in pixels:
https://imgur.com/a/cziRV5Q

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

Re: help in code to fix script with image

Post by boiler » 18 Sep 2021, 07:21

It doesn’t need to be very wide at all. You could make it only 10 pixels wide or even less. If it’s not finding it at that location, you should double check to make sure the coordinates where you think it can be found are accurate. You should also make sure the bar doesn’t have some slight change in color values preventing it from being found there. Adding some allowable color variation in your ImageSearch should compensate for that.

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 18 Sep 2021, 07:43

@boiler @mikeyww

is there any way i can use pixelsearch instead of imagesearch ?
for the search to happen between x = 910 y = 39 to x = 990 y = 48 ?

if you don't find any of the colors below,
0x8E00BD
0x8A00B7
0x8200B0
0x7D00A9
0x7A00A3

I think the search would be simpler this way.
in this case instead of two image searches we could replace the first image search by pixel search

how would the code below look like?

Code: Select all

#Persistent
dir    = Imagens
image1 = %dir%\manashield.png
image2 = %dir%\utamo.png
Loop, 2
 If !FileExist(image := image%A_Index%)
  MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Return

Check:
SoundBeep, 1500
CoordMode, Pixel
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image1%
If ErrorLevel
 Return
MsgBox, 64, Image1, The first image was found.
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return

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

Re: help in code to fix script with image

Post by mikeyww » 18 Sep 2021, 09:10

Code: Select all

#Persistent
If !FileExist(image := "Imagens\utamo.png")
 MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Check:
SoundBeep, 1500
CoordMode, Pixel
For each, color in [0x8E00BD, 0x8A00B7, 0x8200B0, 0x7D00A9, 0x7A00A3]
 PixelSearch, x, y, 910, 39, 990, 48, %color%,, Fast RGB
Until !ErrorLevel
If (x = "")
 Return
MsgBox, 64, Success, The first image was found.
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 18 Sep 2021, 09:32

I just don't understand one thing. whenever it finds the colors before the PixelSearch coordinate, x, y, 910, 39, 990, 48,
and it finds the image the script sends the "numpadmult" key and I would like it to ignore itself if it finds itself before that coordinate.
the code looks perfect I just don't understand why this problem occurs.
even if he found the color out of the coordinate he sends the "numpadmult" key

would there be any way to ignore the sending of this key if the script finds it outside the specific coordinate?
Last edited by Green Astronaut on 18 Sep 2021, 09:34, edited 1 time in total.

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

Re: help in code to fix script with image

Post by mikeyww » 18 Sep 2021, 09:34

Sure. You can adjust the coordinates of the ImageSearch so that they are restricted to your region of interest-- I think already noted earlier in this thread?

When the pixel search fails, the routine does not search for the image.

When the image search fails, F4 is sent. To send nothing instead, you can change "{F4}"to "".
Last edited by mikeyww on 18 Sep 2021, 09:37, edited 1 time in total.

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 18 Sep 2021, 09:37

my coordinate of interest is this 910, 39, 990, 48, but even when it finds the color outside of this coordinate it sends both "numpadmult" and "f4" keys

I wanted to ignore this, and send the keys only if found in these coordinates
Last edited by Green Astronaut on 18 Sep 2021, 09:40, edited 1 time in total.

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

Re: help in code to fix script with image

Post by mikeyww » 18 Sep 2021, 09:38

That is quite a surprise, partly because F3 is not found in this script.

I am interested to know how you proved that the color was found outside of the coordinates, because the command in the script searches only inside the coordinates.

If the pixel search fails, no keys will be sent, but the timer will continue to run, so the routine will be executed repeatedly.

You can display the final ErrorLevel from the pixel search to learn more about whether the search succeeded or failed.
Last edited by mikeyww on 18 Sep 2021, 09:41, edited 1 time in total.

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 18 Sep 2021, 09:40

sorry I typed wrong it's the "f4" key
Last edited by Green Astronaut on 18 Sep 2021, 09:43, edited 1 time in total.

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

Re: help in code to fix script with image

Post by mikeyww » 18 Sep 2021, 09:42

If you wish to stop the timer upon a particular result, you can add a line to do that.

It's best not to edit your posts after someone responds-- creates confusion for readers. You can create a new post with a correction if needed.

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 18 Sep 2021, 09:45

I noticed that the search is failing because I determined these pixel search coordinates only on a piece of the bar, and even though the bar is full it checks I'll put an image below to explain it better

(I'm sorry I was just researching an old post you helped me with and I ended up reliving it by accident)

I'll explain better now what's happening

quando o script começa se ele não encontrar o pixel de cor roxa na barra na coordenada especifica: 910, 39 , 990, 48,
image below:
https://imgur.com/a/IchxCUT

make me send the keys f4" and "numpadmult"

but even when the bar is completely purple like this:
image below
https://imgur.com/a/efuggGP
it sends the "f4" and "numpadmult" keys

sending key logic is fine. it searches for the pixel and then goes to the image search and everything is working perfectly,
but I don't understand why any action happens, even when the purple bar is completely full.
I would just like the actions to occur if the bar were found that way : https://imgur.com/a/IchxCUT
so I determined these search coordinates for pixel :910, 39, 990, 48,
Last edited by Green Astronaut on 18 Sep 2021, 09:57, edited 2 times in total.

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

Re: help in code to fix script with image

Post by mikeyww » 18 Sep 2021, 09:55

You can add MsgBox lines to determine the results at each stage. Display the ErrorLevel results of each search, instead of guessing what is happening. If you display the ErrorLevel values, you will understand the results of the searches.

The only way two keys could be sent is upon multiple iterations. That is why I mentioned stopping the timer if that is what you need.

Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: help in code to fix script with image

Post by Green Astronaut » 18 Sep 2021, 10:03

I need the timer to be on so that the check happens all the time because the game's actions are unpredictable.
the purple bar works like this:
as I lose life, the purple bar decreases to the right side.
and when it arrives at the location of this image below :
https://imgur.com/a/IchxCUT
I would like the image to be checked to know what action to take.
the "f4" key will only be sent if the image is not found.
because the image is a warning that I'm in cooldown and I can't send the "f4" key until the image disappears
this cooldown takes 14 seconds after the "f4" key is pressed and to immediately remove the cooldown and the image disappear would be pressing the "numpadmult" key which immediately removes the cooldown of the spell that is on the "f4" key.
but the "numpadmult" key is used so that if the image is found at the moment the purple bar is not being found at coordinates 910, 39, 990, 48,
makes use of an item where it automatically removes this cooldown and makes the image disappear.

ps: so I would like the actions to be done only when the colors
[0x8E00BD, 0x8A00B7, 0x8200B0, 0x7D00A9, 0x7A00A3]
were found at these coordinates and never before
Last edited by Green Astronaut on 18 Sep 2021, 10:10, edited 1 time in total.

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

Re: help in code to fix script with image

Post by mikeyww » 18 Sep 2021, 10:08

If you wish, you can post the ErrorLevel results of each search.

I am seeing inconsistent descriptions in your posts about the need. You can clarify this by writing all of the rules in clear and plain language, or by making a flow diagram, and by including specific examples of what should happen, step by step.

Your initial post refers to what happens when the second image is not found. Your new post refers to what happens if the first image is not found. This is a discrepancy.

A lucid description is simple because there are only four possibilities (permutations) among the two searches.

Post Reply

Return to “Gaming Help (v1)”