Local object remember its value in new run Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
byzod
Posts: 89
Joined: 21 Jun 2021, 06:46

Local object remember its value in new run

19 Jul 2021, 07:29

If I didn't made it wrong, ahk(v1.x) treat declared variable as local variable unless it has global before it, and local variable is not available out of it's function scope, and will be released when reach hotkey/function end or return

But info in test code below is not working as I expected

Where did I made mistake? Is info := "" mandatory for object (I think)?

Test code:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; Press num*
NumpadMult::
{
	MouseGetPos mX, mY
	; info := ""  ; if omitted, info won't be released in last run
	
	if(mX > 500){
		info := GetInfo({"x":mX, "y":mY})
	}
	
	dataStr := ""
	for i,v in info.data {
		dataStr .= i . ": " . v . "`n"
	}
	result := "pos: " . mX . ", " . mY . "`ndatastr: `n" . dataStr
	
	ToolTip % result
	
	return
}

GetInfo(data){
	info := {Count: 0	,data: {}}
	for name, value in data {
		info.Count++
		info.data[name] := value
	}
	return info
}
Demo:
An2.gif
An2.gif (153.88 KiB) Viewed 399 times
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Local object remember its value in new run  Topic is solved

19 Jul 2021, 09:22

the mistake is thinking hotkey labels are functions. in v1 theyre not. theyre just labels - the braces uve added serve no purpose. info is a global variable(as is any other ure declaring in ur hotkey subroutine). either clear it manually, or actually declare a function and pair it with the hotkey

in v2, what ure saying would have worked as u were expecting
byzod
Posts: 89
Joined: 21 Jun 2021, 06:46

Re: Local object remember its value in new run

20 Jul 2021, 21:42

swagfag wrote:
19 Jul 2021, 09:22
the mistake is thinking hotkey labels are functions. in v1 theyre not. theyre just labels - the braces uve added serve no purpose. info is a global variable(as is any other ure declaring in ur hotkey subroutine). either clear it manually, or actually declare a function and pair it with the hotkey

in v2, what ure saying would have worked as u were expecting
I set the #warn and it warn me that, but I didn't understand why it's global, now I got it, thank!

btw, so it's impossible to declare "local" variable in hotkey scope in v1?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Local object remember its value in new run

23 Jul 2021, 09:28

hotkey subroutines(as u have written them in ur example) are labels. labels cant introduce new scopes
if u want local variable for use with ur hotkeys, assign the hotkey a function. whether u do that like so https://www.autohotkey.com/docs/Hotkeys.htm#Function or using the Hotkey() function is irrelevant

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 238 guests