need help with simplifying logic Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lunacy471
Posts: 55
Joined: 04 Nov 2022, 19:52

need help with simplifying logic

Post by lunacy471 » 04 Nov 2022, 20:04

Hi,
Sorry to be a bother but i've been trying to think of a way to accomplish this simple task and need a bit of help.

First of all I intend to have a dropdownlist gui with the numbers 30, 20, and 10 as its contents.
Next I want each number to run through a series of image searches corresponding to the number chosen and every number below that.

Code: Select all

If number=30
{
image search 30.jpg
image search 29.jpg
image search 28.jpg
~~~ 1.jpg
}

If number=20
{
image search 20.jpg
image search 19.jpg
~~~ 1.jpg
}
hopefully you can get the gist of what im trying to do. the issue is, is there any way to simplify and compress the code down without having to copy paste all the image search codes into each if statement essentially tripling the length of the code, because i intend on using a number that goes to 1000 and this seems really inefficient to do 100 times.


[Mod edit: Added [code][/code] tags.]

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

Re: need help with simplifying logic  Topic is solved

Post by boiler » 04 Nov 2022, 20:25

You would combine all the groups into one so all you need to do is set the top number and it will loop the appropriate number of times, changing the image file number appropriately as it does. No if blocks, no multiple ImageSearch lines. The whole thing can be done in a few lines. Something like:

Code: Select all

loop, % number {
	ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % "image " . (number - A_Index + 1) . "png"
	if !ErrorLevel
		Results.Push({x: FoundX, y: FoundY})
}
Also, don’t use jpg images with you ImageSearch commands. Use a lossless format like png.

lunacy471
Posts: 55
Joined: 04 Nov 2022, 19:52

Re: need help with simplifying logic

Post by lunacy471 » 04 Nov 2022, 21:58

boiler wrote: You would combine all the groups into one so all you need to do is set the top number and it will loop the appropriate number of times, changing the image file number appropriately as it does. No if blocks, no multiple ImageSearch lines. The whole thing can be done in a few lines. Something like:

Code: Select all

loop, % number {
	ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % "image " . (number - A_Index + 1) . "png"
	if !ErrorLevel
		Results.Push({x: FoundX, y: FoundY})
}
Also, don’t use jpg images with you ImageSearch commands. Use a lossless format like png.
ok, i can kinda see where youre going with the code, but could you break down this part for me, I don't really get how it works,
% "image " . (number - A_Index + 1) . png
% means variable right, so in the case my images are all saved into C:desktop\folder\, and the names are just 10.png down to 1.png, where would i put that in? im assuming (number-A_Index+1) takes care of the image names, but where do I add the rest of the path in?
would it be like this?
ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % "C:desktop\folder\ " . (number - A_Index + 1) . "png"

so in the case i select 10 as my number does it count down from 10 or up towards 10? I am still pretty new to ahk and haven't really seen this kind of use for image search yet

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

Re: need help with simplifying logic

Post by boiler » 04 Nov 2022, 23:02

lunacy471 wrote: % "image " . (number - A_Index + 1) . png
% means variable right
A % followed by a space at the beginning of a command parameter means we are forcing an expression, so this parameter will be an expression rather than using command (or legacy) syntax.

lunacy471 wrote: so in the case my images are all saved into C:desktop\folder\, and the names are just 10.png down to 1.png, where would i put that in? im assuming (number-A_Index+1) takes care of the image names, but where do I add the rest of the path in?
would it be like this?
ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % "C:desktop\folder\ " . (number - A_Index + 1) . "png"
Yes, exactly — except I accidentally left the dot out of the extension, so it would be ".png" on the end instead of "png". You also left out the \ after the drive letter, so it would be:

Code: Select all

ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % "C:\desktop\folder\ " . (number - A_Index + 1) . ".png"
…but usually there is the username in a desktop path, so that should be in there too, but instead of explicitly putting all that in there, we can just use the built-in variable to the desktop, like this:

Code: Select all

ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % A_Desktop . "\folder\ " . (number - A_Index + 1) . ".png"

lunacy471 wrote: so in the case i select 10 as my number does it count down from 10 or up towards 10? I am still pretty new to ahk and haven't really seen this kind of use for image search yet
The built-in variable A_Index contains the number of the loop iteration, so in this case it is 1 the first time through, then it becomes 2, then 3, all the way to 10. That’s why I put the math into the expression (number - A_Index + 1), which will be 10 the first time through the loop (10 - 1 + 1), then 9 the next time through (10 - 2 + 1), and so on until the last time where it is (10 - 10 + 1), which equals 1.

lunacy471
Posts: 55
Joined: 04 Nov 2022, 19:52

Re: need help with simplifying logic

Post by lunacy471 » 04 Nov 2022, 23:31

ok so I have been trying to implement it in

so for example I add my gui and dropdownlist like so

Code: Select all

Gui, Add, DropDownList, x170 y70 w40 r10 vDDL gDDLSubmit, 10|20|30|

DDLSubmit:
Gui, Submit, NoHide
Tooltip, % DDL
return


loop, % DDL
 {
ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % "C:\desktop\folder\ " . (DDL - A_Index + 1) . ".png"
	if ErrorLevel = 0
                {
                  do this
                }
}
would this be correct? so if I pick the number 30 in my drop down list it will execute the loop 30 times and search for image from C:\desktop\folder\30.png one by one till 1.png if im understanding correctly?

also where would the number for the variance go?
the *20 after the coordinate range, i tried putting it in but it brings up an error
CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 485, 439, 500, 457,*20 C:\desktop\folder\30.png


[Mod edit: Added [code][/code] tags.]

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

Re: need help with simplifying logic

Post by boiler » 05 Nov 2022, 00:19

You script is correct except the loop would never run. What would cause it to start? Or did you leave out part of the script?

What you showed for variance would not cause an error. Show the actual error message you received.

When posting code, please add code tags like I did to your last post.

lunacy471
Posts: 55
Joined: 04 Nov 2022, 19:52

Re: need help with simplifying logic

Post by lunacy471 » 05 Nov 2022, 02:52

boiler wrote: You script is correct except the loop would never run. What would cause it to start? Or did you leave out part of the script?

What you showed for variance would not cause an error. Show the actual error message you received.

When posting code, please add code tags like I did to your last post.
oh sorry about that, i was just typing in theoretical code just to solve the logic of it first, I finally got things to work somehow by just putting the whole path into its own variable and then using the variable to put into the image search code, it works great now! I appreciate all the help, thanks a lot :superhappy:

lunacy471
Posts: 55
Joined: 04 Nov 2022, 19:52

Re: need help with simplifying logic

Post by lunacy471 » 04 Dec 2022, 05:03

boiler wrote: You would combine all the groups into one so all you need to do is set the top number and it will loop the appropriate number of times, changing the image file number appropriately as it does. No if blocks, no multiple ImageSearch lines. The whole thing can be done in a few lines. Something like:

Code: Select all

loop, % number {
	ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % "image " . (number - A_Index + 1) . "png"
	if !ErrorLevel
		Results.Push({x: FoundX, y: FoundY})
}
Also, don’t use jpg images with you ImageSearch commands. Use a lossless format like png.
Hello, its me again, sorry I have a new question.. not sure if im supposed to create a new topic since its related to the same code but is there a way to change this code into a function that can be called instead of using loops?

like with the code above

Code: Select all

TheSearch()
{
	ImageSearch, FoundX, FoundY, 0, 0, 1000, 400, % "image " . (number - A_Index + 1) . "png"
	if ErrorLevel = 0
		Results.Push({x: FoundX, y: FoundY})

	if ErrorLevel = 1
	{
		if number <= 3  
			{
			number++
			TheSearch()
			}
		
		if number > 3       ; number is greater than 3 it just resets number to 0 without calling the function again
			{
			number = 0
			}
	}   
	
}
	
}
not sure if im doing this correctly but if the image isn't found it will add 1 to the number var to use for the next image search then call the function to essentially loop the same as the first one. Loop 3x checking a different image each time based on . (number - A_Index + 1) . "png".. if found it does whatever i need it to do and ends. if not found it continues on till it either reaches 3 rotations or finds an image. does it look correct or can it be made more cleaner?

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

Re: need help with simplifying logic

Post by boiler » 04 Dec 2022, 10:25

I don't think calling the function from within the function is helpful in this case. It makes the logic more complicated to follow and for no reason. You need to realize where it's going to continue from within one instance of the function when it returns from the one it spawned, and that has to continue to completion, as each one does.

Just make the function complete all the logic within its own instance. Make an attempt at that and post it if you need help. I'm not going to try to unravel your recursive function approach. If you want to continue with that approach, maybe someone else will be willing to help you.

Post Reply

Return to “Ask for Help (v1)”