Variable content lost mid function? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Variable content lost mid function?

13 May 2021, 12:03

Hi Folks, sorry for the long script (again). Please expand and show line numbers...

In the below code, a string is passed to the function at the bottom. The message box at line 204 tells me that the sting (stored in %MyEntry%) is indeed there. But down at line 244, the variable seems to be empty. I've also confirmed that it is empty at the bottom of the function. Of course, nothing is returned from the function.

The only thing I can think of, is that the Return at line 239 is purging the variable.... (?)

Any ideas how I can fix this?
Spoiler
ste(phen|ve) kunkel
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Variable content lost mid function?

13 May 2021, 12:42

You actually do not need that whole script to demonstrate the problem. You are displaying "MyEntry", but since that happens in a separate labeled routine, you would need to "reassign" it to use it again in a new labeled subroutine as you have inside your function. The following shows a fix. There might be one or two alternatives to this as well.

Code: Select all

Global entry
test("test1")

test(text){
 MsgBox, #%text%#
 Gui, Add, Button, Default, OK
 Gui, Show, w220
 entry = %text%
 Return
 ButtonOK:
 Gui, Submit
 MsgBox, #%entry%#
 Return
}
User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Re: Variable content lost mid function?

13 May 2021, 13:37

Thanks for the reply Mike... I can see it working with your code, but in my longer code it still doesn't work.... You're correct though... It is breaking when it goes from above to below the Return and the MyOpts: label...

The message boxes are now at 245 and 252. The string gets passed (varEntry = %MyEntry% ) just fine. But I'm still losing the content.
Spoiler
ste(phen|ve) kunkel
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Variable content lost mid function?

13 May 2021, 13:49

So show that new variable instead of the parameter.

Code: Select all

MsgBox after submit varEntry is: %varEntry% `n`nmyEntry is %varEntry%
User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Re: Variable content lost mid function?

13 May 2021, 14:30

I don't think that's it... The message box has both variables displayed (varEntry and MyEntry). They are both populated before the Return, then both empty after.

I'll zip up all the parts and attach, in case anyone cares to give it a run.

After double-clicking the exe file (which is a renamed copy of AutoHotkey.exe v1), be sure that an editor such as Notepad is selected, then activate it with !+a. Double-click "Sample schmample" which is (hopfully) the top item in the main list of the notices tab. This will call the function and launch the secondary gui (a simple checklist.)

...by the way, this is a new version of the tool I posted here
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=87791&p=389233#p389233
a while back.
ste(phen|ve) kunkel
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Variable content lost mid function?

13 May 2021, 14:35

I tried to use part of your script as a demo, though I admit I did not run your entire script.

Code: Select all

myOptionsFunc("test")
myOptionsFunc(MyEntry){
	global
	MsgBox top of function  myEntry is:`n`n %MyEntry% 
		OptSection := ""
		Loop, Parse, AllSections, `n, `r
		If RegExMatch(A_LoopField, NoteChoice . "-Opt")
			OptSection .= A_LoopField
		If RegExMatch(OptSection, "-Pun")
			vPun = 1
		IniRead, AllOptions, NoticeDefinitions.ini, %OptSection% ; must get list of keys ;var,file,sect
		vCount=
		myOptions=
		Gui, +Owner
		Gui, o:Destroy
		Gui, o:-MinimizeBox +alwaysOnTop
		Gui, o:Add, Text,, Select one or more options:
		Loop, Parse, AllOptions, `n,`r  ; Parses list.
			{
				fchar := SubStr(A_LoopField, 1, 1)
				If fchar is Number
				{
					item := SubStr(A_LoopField, 3, 99)
				}
				else
				{
					item := A_LoopField
				}
					;MsgBox, fchar: %fchar%`nitem : %item%
				Gui, o:Add, CheckBox, Checked%fchar% vCheckbox%a_index%, %item% ;Dynamically make one checkbox per item.
				;vCount = %a_index%
			}
		Gui, o:Add, Button, default xm w50 gMyOpts, Use
		varEntry = %MyEntry% ;########## added on Thursday, 5-13-2021, at 11:26 am ####
	MsgBox after passing to varEntry, it is: %varEntry% `n`nmyEntry is %MyEntry%	
	gui, o:show		
		Return
		MyOpts: ; Go here when user clicks Okay button.
		Gui, o:Submit
	MsgBox after submit varEntry is: %varEntry% `n`nmyEntry is %varEntry%
		vCount=
		Loop, parse, AllOptions, `n
			{
				fchar := SubStr(A_LoopField, 1, 1)
				If Checkbox%a_index% <> 0
				{
					If fchar is Number
					{
						item := SubStr(A_LoopField , 3, 9999)
					}
					else
					{
						item := A_LoopField
					}
					IfEqual, vPun, 1
					{
						myOptions = %myOptions%, %item%
					}
					else
					{
						myOptions = %myOptions% %item%
					}
					vCount++
				}
			}
		IfEqual, vPun, 1
		{
			myOptions := SubStr(myOptions, 3, 999999)
			IfEqual, vCount, 2
				myOptions := StrReplace(myOptions,", " . item," and " . item)
			myOptions := StrReplace(myOptions,", " . item,", and " . item)
		}
		MsgBox bottom of function.   `n`nmyEntry is %MyEntry% `n`n varEntry %varEntry%,`n`n myOptions %myOptions%
		varEntry := StrReplace(varEntry,"<Options>",myOptions) ; Replace Options tag with checked items.
		Return 
}
just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Variable content lost mid function?

14 May 2021, 08:33

    • Function parameters are always local variables.
    • Local function variables are 'cleared' when the function returns.
    • Code: Select all

      Return myEntry
      seems to be valid syntax at the end of a label contained in a function, but the returned value will be discarded.
  1. You defined a 'global' function. So why do you want to pass the value as a parameter?
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Variable content lost mid function?

14 May 2021, 09:20

Good questions; and if the parameter is not needed as a parameter, and the routine is called only once anyway, then turning this into a labeled subroutine might work just as well, while also eliminating the problem.
User avatar
kunkel321
Posts: 957
Joined: 30 Nov 2015, 21:19

Re: Variable content lost mid function?  Topic is solved

14 May 2021, 10:27

Thanks for the input. I actually haven't known whether I should be passing a parameter. I've been avoiding functions in my scripts because I don't feel like a have a good grasp on them. It's all good though.... In the future I'll just try to use them in smaller projects so I can wrap my head around them more gradually. :lol:

I actually do have a working version of this little tool that has everything "in-line." It's embedded in an If statement, so most of the time the entire section is skipped. Also, as the script currently is, that section only ever gets used once at a time. So yea, probably no need for a separate function if the code only ever runs once. I'm currently working on a version of the tool that potentially can use that sub GUI o:Gui, multiple times. That might justify having a separate function, but what I'm doing for now, is just having a loop inside the If statement.

Side note: @Mike, Again, the most recent code you posted does work. I can't see any fundamental difference between yours and mine, so IDK. Interestingly, I was going to make a version of the entire code with all of the Readini parts, removed, and the variable contents defined in the code. That way I could post the code, and folks could run it if they wanted. When I did this, that variable inside the function did seem to retain its value. Of course at that point, the code was broken in many other places. That was when I gave up and just posted the zip.
ste(phen|ve) kunkel

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: william_ahk and 156 guests