5

I've seen a similar topic on sof but its solution did not help me. This is ticking my mind and basically all i want is to have some method of accessing and modifying a value that will maintain its last changed state through out my macros in my single .ahk file.

See example below ,

~Home::Suspend

XButton1::

tog()

return

LButton::

shot()

return



var := "1"

tog(){
var *= -1
}

shot(){

If (var = "1") {

    Loop, 1 {

        Send {k}
        Sleep 65
        Send {WheelDown}
        Sleep 100
        Send {WheelUP}
        Sleep 10

    }

} Else {

    Send {k}

}

}

I am aware that the above is incorrect, and i tried to use"global" in my functions but i just couldn't get my desired effect.

matiszac
  • 115
  • 2
  • 11
  • 1
    Possible duplicate of [Global variable does not have global scope](https://stackoverflow.com/questions/10198900/global-variable-does-not-have-global-scope) – Stevoisiak Aug 21 '17 at 15:26

3 Answers3

4

Using the "global" should work. Something like:

shot(){
    global var
    If (var = "1") {

That points the 'var' variable in the shot() function to the existing 'var' variable defined outside the function.

Ani
  • 566
  • 3
  • 4
  • HMM i tried that but its not working... maybe you try it ? let me know if it works so ill know if the problem is me – matiszac Oct 12 '12 at 05:41
  • 1
    The code in the question has `var := "1"` where it will never be executed - *after* `return` - so the variable will be initially blank. This answer shows the correct way to declare a global variable for use in a function. – Lexikos Nov 01 '12 at 07:40
  • This is what worked for me. I was missing the 'global' keyword. – Lubo Kanev Jan 19 '17 at 09:19
4

I had the same issue and after some trial and error I found my mistake which is the same as in the provided code:

The correct way to declare a global is before other functions

var := "1"

XButton1::
     ; code...
return

The code in the OPs script will hit the return first and never declare the variable

XButton1::
   ; code...
return     ; Returns Here

var := "1" ; Incorrect Will Not Be Declared

I just wanted to provide this as an answer because while I did see this information in one of the comments, I didn't see it until after I'd already spent an additional hour figuring it out myself. As this is the answer I needed, having it as an actual prominent answer may help someone else save time.

willprot
  • 41
  • 1
  • Also keep in mind that doing an `#include` after a `return` has the same effect. The second file will be included as if the code was written at that location, but varialbes won't be declared. – 816-8055 Apr 20 '17 at 12:12
3

What I did, especially since I sometimes have multiple scripts running that need to access the same variable, is to place the var in a .ini file. I also use this to preserve the variable value after a restart. The solution is somewhat slower since the data is saved to the hard disk / SSD, but it works beautifully.

Example of writing the value "S" to variable "State" in group "Finish"

IniWrite, S, C:\2Podcasts\FinishOptions.ini, Finish, State

In an other script (other AutoHotKey instance), I read the value and assign it to the variable "FinishOption".

IniRead, FinishOption, C:\2Podcasts\FinishOptions.ini, Finish, State

If you want to toggle values (True/False), you could use this. This will do an IF on the current value of the variable AND set the variable to the opposite value.

If (MyLoop := !MyLoop) ; Toggle the variable "MyLoop" True/False
{
    Do something
}
Else
{
    Do something else
}
return
Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • 1
    Thanks man the ini file should work fine for me as i wont be concerned with speed. Also thanks for the tip on toggeling. – matiszac Oct 12 '12 at 11:12