[Help] [AutoIT] Imagesearch stack overflow

08/25/2015 22:18 hanover001#1
Hey everyone I'm playing around with auto it and I'm new. I haven't realy searched a lot for my problem but I wrote a skripts that is running. Its finding images clicking on them and everythin that I want but its running like 20-30 minutes or less or more and its stops, the scripts just exit and in the script I get this error:
"C:\Program Files (x86)\AutoIt3\Include\ImageSearch.au3" (34) : ==> Recursion level has been exceeded - AutoIt will quit to prevent stack overflow.:
Func _ImageSearchArea($findImage, $resultPosition, $x1, $y1, $right, $bottom, ByRef $x, ByRef $y, $tolerance)

Can anyone explain to me how to fix it, how the scripts doesn't stop after certain amount of time. Thanks in advance. I'm sorry for my bad english and I'm sorry if there was a thread about it and it has been answered. I haven't searched a lot.
08/25/2015 22:44 Moneypulation#2
Your problem is recursion. Recursion simply means that a function calls itself again and again. But there must be a break condition, else your function will call itself endlessly. For example:

PHP Code:
Global $counter 0
Recursion
()

Func Recursion()
$counter += 1
ConsoleWrite
($counter & @CRLF)
Recursion()
EndFunc 
A correct use of recursion would be for example:

PHP Code:
Global $counter 0
Recursion
()

Func Recursion()
$counter += 1
If $counter >= 10 Then Return
ConsoleWrite($counter & @CRLF)
Recursion()
EndFunc 
So if you call your ImageSearch function over and over again, after some time AutoIt can't handle to amount of recursive calls anymore and that is the cause of your error
08/26/2015 00:01 hanover001#3
Okey now I get it what is the real problem but I still dont undestand how to fix it maybe because I'm new with all the coding. What do I have to do I have to put a counter on every function?

Thanks for the fast reply
08/26/2015 00:03 Moneypulation#4
Quote:
Originally Posted by hanover001 View Post
Okey now I get it what is the real problem but I still dont undestand how to fix it maybe because I'm new with all the coding. What do I have to do I have to put a counter on every function?

Thanks for the fast reply
Post your code here. Else we can't really figure out the problem
08/26/2015 01:01 hanover001#5
#include <ImageSearch.au3>

HotKeySet("{F1}", "Main")
HotKeySet("{F3}", "stop")
HotKeySet("{F2}", "pause")

Global $x = 180
Global $y = 100
Global $1x, $2x, $3x, $4x, $5x, $6x, $7x, $8x, $9x, $10x, $11x, $12x, $13x, $14x, $15x, $16x = @DesktopHeight
Global $1y, $2y, $3y, $4y, $5y, $6y, $7y, $8y, $9y, $10y, $11y, $12y, $13y, $14y, $15y, $16y = @DesktopWidth


Func Main()
While 1
$lookmap = _ImageSearch("map.png", 1, $4x, $4y, 0)
If $lookmap = 1 Then
MouseClick("left", $4x + 25 + (Random(0, $x)), $4y + 50 + (Random(0, $y)))
Sleep(200)
_wait()
EndIf
WEnd
EndFunc



Func _wait()
While 1
Sleep(200)
$gotopoint = _ImageSearch("gotopoint.png", 1, $5x, $5y, 0)
If $gotopoint = 1 Then
golditem()
Else
Main()
EndIf
WEnd
EndFunc

Func item()
While 1
$item = _ImageSearch("items.png", 1, $6x, $6y, 0)
If $item = 1 Then
MouseClick("left", $6x, $6y, 1)
Sleep(200)
waititemtocollect()
Else
_wait()
EndIf
WEnd
EndFunc

Func golditem()
While 1
$golditem = _ImageSearch("goldenitems.png", 1, $7x, $7y, 0)
If $golditem = 1 Then
MouseClick("left", $7x, $7y, 1)
Sleep (200)
waititemtocollect()
Else
item()
EndIf
WEnd
EndFunc

Func waititemtocollect()
While 1
$waititemtocollect = _ImageSearch("waittocollect.png", 1, $2x, $2y, 0)
If $waititemtocollect = 0 Then
golditem()
EndIf
WEnd
EndFunc


While 1
Sleep(100)
WEnd

Func pause()
While 1
Sleep(100)
WEnd
EndFunc

Func stop()
Exit
EndFunc
08/26/2015 11:44 Moneypulation#6
The problem is that you call functions over functions without return. Try to recode it that it only has one main loop like:

Func main ()

While 1
$item = Golditem ()
If $item = True Then
Collect ()
Waitforcollect ()
Endif
Sleep (200)
Wend

Endfunc

Your other functions can have while loops too but they must have a break condition so that you will return to your main loop.
08/26/2015 18:23 hanover001#7
Okey thanks for the help I will try recoding it.

edit: well I tryed several times recoding it but I cant manage to make it so it doesnt stop