Jump to content

Declaring Variables


Recommended Posts

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.

Link to comment
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.

Link to comment
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]

Link to comment
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.

Link to comment
Share on other sites

  • Moderators

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

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

  • Moderators

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.

Link to comment
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...