Tonik Posted February 15, 2023 Share Posted February 15, 2023 I'm trying to create a script that starts a timer when I press "o" and stops the timer when I press "p". Here is what I've got: HotKeySet('o', 'StartTimer') HotKeySet('p', 'EndTimer') $hTimer=0 Func StartTimer() Local $hTimer = TimerInit() EndFunc Func EndTimer() Local $fDiff = TimerDiff($hTimer) ConsoleWrite ($fDiff) ConsoleWrite (@crlf) EndFunc While 1 WEnd When I run this, I pressed "o" followed by three "p" keypresses and received this output in the console: 29362443.6463 29363759.7981 29365535.129 29366384.2509 I'm honestly not sure how to interpret this output to try and trouble shoot it - does anyone have any ideas where I'm messing up? Thank You in advance. Link to comment Share on other sites More sharing options...
Solution Trong Posted February 16, 2023 Solution Share Posted February 16, 2023 You are wrong to use local variables. Just set it as the global variable the script will fix: HotKeySet('q', '_Exit') HotKeySet('o', 'StartTimer') HotKeySet('p', 'EndTimer') Global $hTimer=0 Func StartTimer() ConsoleWrite ('> TimerInit !' & @CRLF) $hTimer = TimerInit() EndFunc Func EndTimer() Local $fDiff = TimerDiff($hTimer) ConsoleWrite ('- TimerDiff: ' & $fDiff & @CRLF) EndFunc Func _Exit() ConsoleWrite ('! Exit !' & @CRLF) Exit EndFunc While 1 Sleep(1) WEnd Subz 1 Regards, Link to comment Share on other sites More sharing options...
mistersquirrle Posted February 16, 2023 Share Posted February 16, 2023 (edited) Like Trong said, you're using the variables incorrectly, however you don't need to use Global variables. Local $hTimer in the main script body is fine, your issue was that you were redeclaring the variable in the function scope, which for that function was overriding/ignoring the script local variable. Check out this page: https://www.autoitscript.com/wiki/Best_coding_practices And this one about scope: https://www.autoitscript.com/wiki/Variables_-_using_Global,_Local,_Static_and_ByRef But a quick note about scope: Global - When declared anywhere, it's available to this script and any included files. It's not recommended to declare Globals in functions, as they won't exist until the function runs. Local - When used in a function, it is only available to that function while it's running. Once the function ends, it's no longer available and its values are gone. Declaring a variable here will use that version inside of an already existing variable under that name. Local Static - This can be used in a function to keep the variable and its value between function calls. Normally variables are deleted when a function ends. With a Static variable, it's kept between each call, so it can referenced or changed and persist. Also a quick note about your script: While 1 WEnd This ^ is a bad idea, it'll cause your CPU to be at 100% constantly (though just on one core, since AutoIt is single threaded). As in Trongs example, add a sleep to your loop. Note that the minimum value for Sleep is actually 10, so using 1-9 are = 10: https://www.autoitscript.com/autoit3/docs/functions/Sleep.htm Edited February 16, 2023 by mistersquirrle Removed notes about Local in the Global scope Subz 1 We ought not to misbehave, but we should look as though we could. Link to comment Share on other sites More sharing options...
Tonik Posted February 16, 2023 Author Share Posted February 16, 2023 Thank both of you for your help and information! Link to comment Share on other sites More sharing options...
Subz Posted February 16, 2023 Share Posted February 16, 2023 @mistersquirrle Local $hTimer in the main script of the body, would be the same as declaring it as a global variable. So while you could use local, imho, it is better to have the scope declared correctly. mistersquirrle 1 Link to comment Share on other sites More sharing options...
mistersquirrle Posted February 16, 2023 Share Posted February 16, 2023 2 hours ago, Subz said: @mistersquirrle Local $hTimer in the main script of the body, would be the same as declaring it as a global variable. So while you could use local, imho, it is better to have the scope declared correctly. I see, I misunderstood Local in the Global scope. I didn't know that Local in the main script body (Global scope) is still considered a Global variable. I always assumed that if you made a variable Local it would only be available to that file, and not in any #includes. Reading the Best Practices page closer it mentions that a Local variable in the Global scope is still considered a Global variable: https://www.autoitscript.com/wiki/Best_coding_practices I confirmed this making a file with just a Local declaration, including it in another file and printing that Local variable from the #include. I apologize I didn't know that's how it worked. Also, it's mentioned here: https://www.autoitscript.com/wiki/Variables_-_using_Global,_Local,_Static_and_ByRef Thanks for bringing it to my attention. We ought not to misbehave, but we should look as though we could. Link to comment Share on other sites More sharing options...
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