Variable value disappears Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Noo0B
Posts: 73
Joined: 26 Jan 2018, 19:54

Variable value disappears

30 Aug 2018, 19:46

Hello everyone,
Thanks for reading.
This code does not seem to work, and I'm just starting to troubleshoot my gui variable forking:

Code: Select all

f1::
Swap:=Swap1-2
;Swap:=Swap2-2
Swap1Tab:=2
Swap2Tab:=2
		If (Swap=Swap1-2 AND Swap1Tab=2 OR Swap=Swap2-2 AND Swap2Tab=2)
			MsgBox, % "Swap is: " Swap ", Swap1Tab is: " Swap1Tab ", Swap2Tab is: " Swap2Tab
		Else
			MsgBox, Nothing happened here
Return
f2::
Swap=Swap1-2
;Swap=Swap2-2
Swap1Tab=2
Swap2Tab=2
		If (Swap=Swap1-2 AND Swap1Tab=2 OR Swap=Swap2-2 AND Swap2Tab=2)
			MsgBox, % "Swap is: " Swap ", Swap1Tab is: " Swap1Tab ", Swap2Tab is: " Swap2Tab
		Else
			MsgBox, Nothing happened here
Return
Does anyone have any idea what's up?
Thanks again in andvance.
gregster
Posts: 2660
Joined: 30 Sep 2013, 06:48

Re: Variable value disappears  Topic is solved

30 Aug 2018, 21:09

Depends on what the goal is. (Btw, I would rather use parentheses in the conditions with the ANDs and ORs to make the priorities clear. Afaik, ANDs take otherwise precedence of ORs - that might well be intended here, but helpers cannot know)

But what should Swap1-2 stand for? A literal string or is Swap1 a variable? In your f2 case, it will be seen as a literal string. Thus, in an if-expression-statement you will have to use quotation marks to check for it...

In your f1 case (Swap1 as a variable), I think the blank variable causes the "problem", but you don't say what you expect to happen... . Compare https://autohotkey.com/docs/Variables.htm#Operators :
Except where noted below, any blank value (empty string) or non-numeric value involved in a math operation is not assumed to be zero. Instead, it is treated as an error, which causes that part of the expression to evaluate to an empty string. For example, if the variable X is blank, the expression X+1 yields a blank value rather than 1.
Try this:

Code: Select all

f1::
Swap1 := 0	; make the blank variable non-blank
Swap:= Swap1 - 2	; 0 - 2 = -2
;Swap:=Swap2-2
Swap1Tab:=2
Swap2Tab:=2
		If (Swap=Swap1-2 AND Swap1Tab=2 OR Swap=Swap2-2 AND Swap2Tab=2)
			MsgBox, % "Swap is: " Swap ", Swap1Tab is: " Swap1Tab ", Swap2Tab is: " Swap2Tab
		Else
			MsgBox, Nothing happened here
Return
f2::
Swap=Swap1-2	; a string is assigned
;Swap=Swap2-2
Swap1Tab=2
Swap2Tab=2
		If (Swap="Swap1-2" AND Swap1Tab=2 OR Swap="Swap2-2" AND Swap2Tab=2)
			MsgBox, % "Swap is: " Swap ", Swap1Tab is: " Swap1Tab ", Swap2Tab is: " Swap2Tab
		Else
			MsgBox, Nothing happened here
Return
Noo0B
Posts: 73
Joined: 26 Jan 2018, 19:54

Re: Variable value disappears

03 Sep 2018, 02:25

Hi, many thanks for your reply and sorry for the poorly expanded post.
There's no %Swap1%. but "Swap1-2" and "Swap2-2". I understand now that the "_" instead of "-" would have been a better choice on the moment I decided to try that approach.
So only my f2 example is valid, which is pretty much how it goes since the values get loaded from the .ini file.
I'm trying to double deref about 6k lines of code in 1.5k.
I wrote about an hour of example code to show the error but that actually worked.
So more troubleshooting for now and thank you, without your reply I'd never know the code could actually work.
Kind regards.
User avatar
jeeswg
Posts: 6547
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Variable value disappears

03 Sep 2018, 02:51

- So, since you probably wanted underscores, not hyphens, your script should probably look like this:
- Note: both of your F1 and F2 subroutines ended up being the same.

Code: Select all

F1::
F2::
Swap := Swap1_2
;Swap := Swap2_2
Swap1Tab := 2
Swap2Tab := 2

if (Swap=Swap1_2 AND Swap1Tab=2 OR Swap=Swap2_2 AND Swap2Tab=2)
	MsgBox, % "Swap is: " Swap ", Swap1Tab is: " Swap1Tab ", Swap2Tab is: " Swap2Tab
else
	MsgBox, Nothing happened here
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
gregster
Posts: 2660
Joined: 30 Sep 2013, 06:48

Re: Variable value disappears

03 Sep 2018, 04:26

Noo0B wrote:Hi, many thanks for your reply and sorry for the poorly expanded post.
There's no %Swap1%. but "Swap1-2" and "Swap2-2". I understand now that the "_" instead of "-" would have been a better choice on the moment I decided to try that approach.
So only my f2 example is valid, which is pretty much how it goes since the values get loaded from the .ini file.
I'm trying to double deref about 6k lines of code in 1.5k.
I wrote about an hour of example code to show the error but that actually worked.
So more troubleshooting for now and thank you, without your reply I'd never know the code could actually work.
Kind regards.
Well, if Swap1-2 should actually be a literal string, then you can also use expression syntax (F1 case), but you also will have to add quotation marks:

Code: Select all

Swap:= "Swap1-2"
; and 
If (Swap="Swap1-2" AND Swap1Tab=2 OR Swap="Swap2-2" AND Swap2Tab=2)
; ...
Noo0B
Posts: 73
Joined: 26 Jan 2018, 19:54

Re: Variable value disappears

03 Sep 2018, 05:03

Yeah that was what I was using all the time but somehow it stopped working for me, you may want to see my last post to get an idea what began to happen.
I used quotes both on the variables and ifs to get it going.
I'll try to get bck on it later.
I realized the script was derailing because of something else.
I finally understood why the usual message for a variable for two controls, with ERROR for the variable, was showing up.
it showed on the if (Swap=Swap1_2) tab1 part, with the 1_1 variable.
The literal parts of the variables are the same on the code and tab 1 was using the checkbox codes for gui1.
I believe all the sections i wanted to merge might not get to be crammed together without some modification, e.g. making the gui identification variable suffix _2 or _1 part of the literal component of the derefed variables.
I suppose AHK reads all the code first and uses values for the first gui at the second gui part of the sub, and "if" is only for execute.
gregster
Posts: 2660
Joined: 30 Sep 2013, 06:48

Re: Variable value disappears

03 Sep 2018, 05:51

Noo0B wrote:I suppose AHK reads all the code first and uses values for the first gui at the second gui part of the sub, and "if" is only for execute.
Not really sure what this means, but doesn't sound accurate. Obviously variables can be changed during runtime and AHK will use the up-to-date variable contents during the flow of code execution.
Noo0B
Posts: 73
Joined: 26 Jan 2018, 19:54

Re: Variable value disappears

04 Sep 2018, 02:44

gregster wrote:
Noo0B wrote:I suppose AHK reads all the code first and uses values for the first gui at the second gui part of the sub, and "if" is only for execute.
Not really sure what this means, but doesn't sound accurate. Obviously variables can be changed during runtime and AHK will use the up-to-date variable contents during the flow of code execution.
True on that.
The variable and the ifs needed quotes to get the if's conditions to work, otherwise they would just run through.
So I guess it matches my previous experience with literal strings.
I went back and forth between 2 and 3 subs for guis for a while.
2 being for hk's 1 and 2 "_1" suffixes and the next to the "_2" suffix and its gui tab, that sufficed.
3 subs was getting a "_2" separate gui sub for each hotkey, but it helped me solidify the need for the quotes.
I'm not sure if I would be able to go back to a single sub for the two guis though, I sure its possible, but guess i'm going to go back at it and try joining them later.
Right now I'm at the read/write gui, wich goes with an inner sub that sets a double var deref loop for read and writes:

Code: Select all

LCAdd:
	L:= 1
	C:= 1
	Loop {
		IniRead, %Swap%RadioL%L%C%C%Hotkey%Hk%, Config.ini, %Swap%Position, %Swap%RadioL%L%C%C%Hotkey%Hk%
		C+= 1
			If (C > 12) {
					If (L = 5)
						Break
				C:= 1
				L+= 1
			}
	}
	C:= 1
	L:= 1
Return

;and


LCCoordAdd:
	L:= 1
	C:= 1
	Loop {
		IniWrite, % %Swap%RadioL%L%C%C%Hotkey%Hk%, Config.ini, %Swap%Position, %Swap%RadioL%L%C%C%Hotkey%Hk%
			If (%Swap%RadioL%L%C%C%Hotkey%Hk% = 1) {
				%Swap%Hotkey%Hk%_X:= 1240 + (C*53)
				%Swap%Hotkey%Hk%_Y:= 560 + (L*54)
			}
		C+= 1
			If (C > 12) {
					If (L = 5)
						Break
				C:= 1
				L+= 1
			}
	}
	C:= 1
	L:= 1
Return

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], MannyKSoSo, tmplinshi and 197 guests