Sign in to follow this  
Followers 0
Hawkwing

Declaring Variables

9 posts in this topic

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.

HotKeySet ("{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.

Share this post


Link to post
Share on other sites



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 :D


Click here for the best AutoIt help possible.Currently Working on: Autoit RAT

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

...

You already figured out how to solve this :D

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 code
HotKeySet("{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]

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.htm

You 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.

Share this post


Link to post
Share on other sites

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.

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