ahha Posted May 8, 2022 Posted May 8, 2022 I seem to have lost my sense of how the Global scope works. In this example $p is Global but I can't seem to set $p in function __One and then pass that version of $p to function __Two. I thought you could set a Global to a new value from within a function and it would affect the Global value. I've stared and played with this code for over an hour and I'm missing something really basic. Help appreciated. #AutoIt3Wrapper_run_debug_mode=Y ; use this to debug in console window <--- LOOK Global $p = 999 MsgBox(0, "DEBUG", "MAIN - $p = '" & $p & "'") __One() Exit Func __One() ;Global $p ;this seems to make no difference - comment it out to see - how does one affect the Global in a function? MsgBox(0, "DEBUG", "Entering Func __One() - $p = '" & $p & "'") For $p = 1 to 3 MsgBox(0, "DEBUG", "IN Func __One() - $p = '" & $p & "'") __Two() Next EndFunc Func __Two() MsgBox(0, "DEBUG", "Entering __Two() $p = '" & $p & "'") EndFunc
Sidley Posted May 9, 2022 Posted May 9, 2022 My understanding is that changing the global variable within a function only changes the value for the scope of that function, if you need to change the value within the program as a whole you need to use the ByRef keyword. Passing it by reference will alter the variable outside the function: #AutoIt3Wrapper_run_debug_mode=Y ; use this to debug in console window <--- LOOK Global $p = 999 MsgBox(0, "DEBUG", "MAIN - $p = '" & $p & "'") __One($p) Exit Func __One(ByRef $p) ;Global $p ;this seems to make no difference - comment it out to see - how does one affect the Global in a function? MsgBox(0, "DEBUG", "Entering Func __One() - $p = '" & $p & "'") For $p = 1 to 3 MsgBox(0, "DEBUG", "IN Func __One() - $p = '" & $p & "'") __Two() Next EndFunc Func __Two() MsgBox(0, "DEBUG", "Entering __Two() $p = '" & $p & "'") EndFunc
Solution Subz Posted May 9, 2022 Solution Posted May 9, 2022 (edited) In the OP, you're declaring the variable two times: Global $p = 999 ;~ global scope For $p = 1 To 3 ;~ local scope As Sidley mentioned you can use ByRef or you can just change the value of the variable without declaring it, example: Global $p = 999 MsgBox(0, "DEBUG", "MAIN - $p = '" & $p & "'") __One() Func __One() MsgBox(0, "DEBUG", "Entering Func __One() - $p = '" & $p & "'") For $i = 1 to 3 $p = $i MsgBox(0, "DEBUG", "IN Func __One() - $p = '" & $p & "'") __Two() Next EndFunc Func __Two() MsgBox(0, "DEBUG", "Entering __Two() $p = '" & $p & "'") EndFunc Edited May 9, 2022 by Subz Mixed Sidleys post and the OP
ahha Posted May 9, 2022 Author Posted May 9, 2022 Subz - so there's a difference in For $p = 1 to 3 MsgBox(0, "DEBUG", "IN Func __One() - $p = '" & $p & "'") __Two() Next versus For $i = 1 to 3 $p = $i MsgBox(0, "DEBUG", "IN Func __One() - $p = '" & $p & "'") __Two() Next I thought in the For $p = 1 to 3 case $p would affect the Global $p - apparently not. I thought the Global $p overrode any $p in a function that did not have $p declared as local. From: https://www.autoitscript.com/wiki/Variables_-_using_Global,_Local,_Static_and_ByRef - A Global variable is visible throughout your script - any part of the script can both read its value and, importantly, change it. - A Local variable exists only within the function in which it is declared and is destroyed when the function terminates. It is invisible to any other function unless it is passed as a parameter. Thanks I'll have to remember the For loop acts differently and explicitly assign it like you suggested.
Subz Posted May 9, 2022 Posted May 9, 2022 It's about the declaration of the variable, i.e. prefixing a variable with Local, Global etc... or in this case when used with For..In..Next (see remarks) it's declaring the variable as Local $p. Using: $p = $i is modifying the variable value but not declaring it as Local or Global, this can get confusing as AutoIt doesn't require you to declare variables scope unless you use the MustDeclareVars so for example: $g_sGlobalVar1 = "Global Var, although not explicitly declared" Global $g_sGlobalVar2 = "Global Var, explicitly declared" Local $g_sGlobalVar3 = "Global Var, as it's outside of a function, even though it was declared as Local" _MyFunc() Func _MyFunc() $sLocalVar1 = "Local Var, although not explicitly defined" Local $sLocalVar2 = "Local Var, explicitly declared" $g_sGlobalVar2 = "Global Var, which has already been declared above, this would change it's value" Global $g_sGlobalVar4 = "Global Var, while you can declare global variables, it is not recommended and would only be available after _MyFunc() is called" For $i = 1 to 3 ;~ $i is declared as a local variable $i += 1 ;~ $i value is modified not being declared Next EndFunc Hope that makes sense, although my terminology might be off. pixelsearch and Xandy 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now