Sign in to follow this  
Followers 0
leegold

Any Way to Make Changes to Global Variables Inside a Running Function ?

4 posts in this topic

Hi,

This question might show a gap in my understanding how Autoit works but hopefully I'll learn...so I did not think this would work:

Global $z = 1
Example()
Sleep(5000)
ConsoleWrite("five sec" & @LF)
$z=0
Func Example()
    $c = 0
     While $z == 1
        ConsoleWrite($c & @LF)
        Sleep(1000)
        If $z == 0 Then
            ConsoleWrite("$z == 0" & @LF)
        EndIf
        $c=$c+1
    WEnd
EndFunc   ;==>Example

Once the function starts it's impervious to changes in $z, the code never progresses past the function call. What I actually want to do is to stop the while loop while I change somethings outside of the function then restart the while loop and have the change "stick" inside the function/while loop.

Is the only way to do this to kill the example() function, make changes, then call/restart the fuction again?

Thanks

Share this post


Link to post
Share on other sites



You just need to have something which will make the script halt the function and respond to an event then continue. For example

HotKeySet("^{F10}",'SetZ')
HotKeySet("!{F10}",'ReSetZ')
Global $z = 1
Example()
Sleep(5000)
ConsoleWrite("five sec" & @LF)
$z=0
Func Example()
    $c = 0
     While $z == 1
        ConsoleWrite($c & @LF)
        Sleep(1000)
        If $z == 0 Then
            ConsoleWrite("$z == 0" & @LF)
        EndIf
        $c=$c+1
    WEnd
EndFunc   ;==>Example


Func setZ()
    $z = 1
EndFunc

Func ResetZ()
    $z = 0
EndFunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

also, you could insert another function call inside the While -WEnd loop and that function will manage the $z variable value

Global $z = 1
Example()
Sleep(5000)
ConsoleWrite("five sec" & @LF)
$z=0
Func Example()
    $c = 0
     While $z == 1
        ConsoleWrite($c & @LF)
        Sleep(1000)
          _Manage_z() ; <-- this function will change $z
       If $z == 0 Then
            ConsoleWrite("$z == 0" & @LF)
        EndIf
        $c=$c+1
    WEnd
EndFunc   ;==>Example

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

This helps immensely. Thanks:

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0