Hawkwing Posted March 22, 2009 Share Posted March 22, 2009 If I have a function that declares a variable, then starts another function that uses that variable, the second function doesn't work. It says that I have used a variable that wasn't declared. expandcollapse popupHotKeySet ("{end}", "myfunction") Sleep (2000) Func myfunction () $myvariable = 5 myfunction2 () EndFunc Func myfunction2 () ToolTip ($myvariable) Sleep (2000) EndFuncoÝ÷ Ù8^¢yrÁ¬q©ßwZ®§vØb²+-¡Ú,¢g)à)¶¬jëh×6HotKeySet ("{end}", "myfunction") $myvariable2 = 0 $myvariable = 0 Sleep (2000) If $myvariable2 = 1 Then ToolTip ($myvariable) sleep (2000) EndIf Func myfunction () $myvariable = 5 myfunction2 () EndFunc Func myfunction2 () $myvariable2 = 1 EndFunc Does anyone know an easier way to do something like this? The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again. Link to comment Share on other sites More sharing options...
Marlo Posted March 22, 2009 Share Posted March 22, 2009 When you declare a variable inside a function AutoIt assumes that it is only a local scoped variable meaning that it will be disposed after the function has ran. You already figured out how to solve this Click here for the best AutoIt help possible.Currently Working on: Autoit RAT Link to comment Share on other sites More sharing options...
Hawkwing Posted March 22, 2009 Author Share Posted March 22, 2009 Oh, didn't know that. Now I might be able to fix a couple of my scripts. Thanks. The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again. Link to comment Share on other sites More sharing options...
herewasplato Posted March 22, 2009 Share Posted March 22, 2009 ... You already figured out how to solve this I'm not sure that the OP has figured out how to solve this since the second code is so different from the first - but it does declare the variable(s) from the first script outside of the func codeHotKeySet("{end}", "myfunction") Global $myvariable Sleep(2000) Func myfunction() $myvariable = 5 myfunction2() EndFunc ;==>myfunction Func myfunction2() ToolTip($myvariable) Sleep(2000) EndFunc ;==>myfunction2 [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
Hawkwing Posted March 22, 2009 Author Share Posted March 22, 2009 Ah, so I just have to declare the variable before the function. The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 22, 2009 Moderators Share Posted March 22, 2009 Ah, so I just have to declare the variable before the function.http://en.wikipedia.org/wiki/Scope_(programming)http://en.wikipedia.org/wiki/Variable#Scope_and_extenthttp://en.wikipedia.org/wiki/Global_variablehttp://en.wikipedia.org/wiki/Local_variable Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Hawkwing Posted March 23, 2009 Author Share Posted March 23, 2009 So declaring a variable using local lets you use it on that script, and using global lets you use it on other scripts as well, right? So what do dim and const do? The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 23, 2009 Moderators Share Posted March 23, 2009 So declaring a variable using local lets you use it on that script, and using global lets you use it on other scripts as well, right?So what do dim and const do?http://www.autoitscript.com/autoit3/docs/i...g_variables.htmYou really should consult your help file. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Hawkwing Posted March 23, 2009 Author Share Posted March 23, 2009 I did look at the help file, but I guess I failed to notice this part: Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!) Global = Forces creation of the variable in the Global scope Local = Forces creation of the variable in the Local/Function scope The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again. 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