Breaking a loop using a function that doesnt have a loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
racqu
Posts: 3
Joined: 04 Aug 2022, 12:58

Breaking a loop using a function that doesnt have a loop

Post by racqu » 15 Aug 2022, 23:09

I write my script sections into functions to accomplish specific things, then combine the functions into a separate looped function. Is there a way to break the loop of the function by writing in something into one of the specific functions?

I have tried every way that I know of but as I am still new to advanced scripting, I'm sure that I'm missing something simple

Here is the specifics of what I'm referring to:

The specific function

Code: Select all

Obs4() {
PixelSearch, ObsX, ObsY, 947, 659, 1225, 1053, 0xFF00FF00, 1, Fast RGB
	If ErrorLevel
		{
		PixelSearch, ObsX, ObsY, 947, 659, 1225, 1053, 0xFFFF0000, 1, Fast RGB
			If ErrorLevel	
			{
			Random, RockX, 1475, 1500
			Random, RockY, 1049, 1076
			MouseMove, RockX, RockY, 5
			SleepClick()
			Click
			SleepShort()
			PixelSearch, TreeX, TreeY, 1282, 484, 1858, 1058, 0xFF00FF00, 1, Fast RGB
				If ErrorLevel
					Reload
				Else
					{
					Random, RandX, 5, 25
					Random, RandY, 2, 11
					Tree1X:= (TreeX + RandX)
					Tree1Y:= (TreeY + RandY)
					MouseMove, Tree1X, Tree1Y, 5
					SleepClick()
					Click
					;need to break the loop of the Train() function and restart that function
					}
			}
			Else
			{
			Random, RandX, 5, 35
			Random, RandY, 5, 40
			Obs1X:= (ObsX + RandX)
			Obs1Y:= (ObsY + RandY)
			MouseMove, Obs1X, Obs1Y, 5
			SleepClick()
			Click
			}
		}
	Else
		{
		Random, RandX, 5, 35
		Random, RandY, 5, 40
		Obs1X:= (ObsX + RandX)
		Obs1Y:= (ObsY + RandY)
		MouseMove, Obs1X, Obs1Y, 5
		SleepClick()
		Click
		}
}
The Looped Function

Code: Select all

Train() {

Loop, {
	
	Obs1()
	SleepMed()
	MarkCheck()
	
	Obs2()
	SleepMed()
	MarkCheck()
	
	Obs3()
	SleepMed()
	MarkCheck()
		
	Obs4()		
	SleepShort()
	MarkCheck()
		
	Obs5()
	SleepLong()
	MarkCheck()
	
	Obs6()
	SleepLong()
	MarkCheck()
	
	Finish()
	SleepShort()
	
	Tree()
	SleepMed()
	MarkCheck()
	
	}
}

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

Re: Breaking a loop using a function that doesnt have a loop

Post by boiler » 15 Aug 2022, 23:42

You can return a value from one of the "inner" functions to the main function, which would check the return value and break the main function's loop (or not) depending on the value. Simple example:

Code: Select all

Main()
MsgBox, Main loop broken
return

Main() {
	loop {
		if Inner1()
			break
		if Inner2()
			break
	}
}

Inner1() {
	static count := 1
	ToolTip, % "Inner1: Iteration " count++
	Sleep, 500
	return count > 10
}

Inner2() {
	static count := 1
	ToolTip, % "Inner2: Iteration " count++
	Sleep, 500
	return count > 10
}

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Breaking a loop using a function that doesnt have a loop

Post by BoBo » 16 Aug 2022, 07:24

You could also use a While/Until(-Loop) to act on that trigger.

Post Reply

Return to “Ask for Help (v1)”