Jump to content


recursion and global vars


  • Please log in to reply
4 replies to this topic

#1 Zaav

Zaav
  • Guests

Posted 29 June 2012 - 09:20 AM

stop = 0 

aquireTarget() {
	send {Tab}
	sleep 1000
	tHP:=targetHP()
	if stop
		exit
	else if ( tHP < 135.0 )
		aquireTarget()
	else
		battle()	
}

~RButton::
  stop = 1 
return

%stop% gets changed, but apparently there is a %stop% var for every recursion and the top level never notices the change..

How do i get this to work as it should in normal programing languages?

#2 Guests

  • Guests

Posted 29 June 2012 - 09:39 AM

Make stop global variable <!-- m -->http://www.autohotke...ions.htm#Locals<!-- m -->

#3 MasterFocus

MasterFocus
  • Moderators
  • 4126 posts

Posted 29 June 2012 - 09:57 AM

Exactly what Guest said.
And maybe something like this:
stop := 0 

aquireTarget() {
  [color=#FF0000]Global stop[/color]
  Loop {
    Send, {TAB}
    Sleep, 1000
    If stop
      Exit
    If ( ( tHP := targetHP() ) < 135 )
      Break
  }
  battle()
}

~RButton:: stop := 1


#4 Guests

  • Guests

Posted 29 June 2012 - 11:09 AM

Awesome, thanks.
I thought it already was a global variable with me declaring it outside of the procedure .. that's what i get for assuming stuff.

#5 Lexikos

Lexikos
  • Administrators
  • 8832 posts

Posted 29 June 2012 - 12:03 PM

stop := 0 is an assignment, not a declaration. The issue isn't whether stop is global (it is global), but whether stop inside the function refers to the global variable or a separate local variable with the same name:

All variables accessed or created inside a function are local by default (except super-global variables and built-in variables such as Clipboard, ErrorLevel, and A_TimeIdle).
Source: AutoHotkey_L - Functions

"Super-global" variables work the way you assumed, but they are supported only by AutoHotkey_L.