Use global variable in Hotkey Block (Autohotkey) - autohotkey

My Autohotkey script doesn't work. Global Variables seem to have a null value in the "^1" block. How do I declare global variables that I can use in multiple hotkey blocks?
#if (true)
global allActsRowY := 76
global act1X := 249
global allTownsY := 133
global allTownsX := 245
global clickDelay := 30
;Act 1 WP Town Shortcut
^1::
{
Click, left, %act1X%, %allActsRowY%
Sleep, clickDelay
Click, left, %allTownsX%, %allTownsY%
}
return
#if
Edit: The global variables were declared in the if-block. I made an edit to the code.

I can only assume this isn't your whole script and you have e.g. hotkeys definitions, or something else, above all that. Which means that code execution never reaches those variable definitions.
You want to have the definitions in the script's auto-execute section.
Also worth noting that you're not defining the variables as global, you're defining them as super-global, which is unnecessary and kind of bad practice.
You can just remove the word global from the definitions, variables already in the global scope if you define them outside of functions/classes. And hotkey blocks always have access to the global scope without any extra steps (in AHKv1).

Related

Understanding Why My AutoHotkey Variable Isn't Being Assigned

I am trying to assign a string to a variable, and then have a hotkey send that variable's value.
I already have a functioning example, where I use f1 to send a text string with some of the automatic Autohotkey variables such as %A_MM% %A_DD% and %A_YYYY% for quick and easy datestamps:
f1::
Send, (WTC %A_MM%/%A_DD%/%A_YYYY% %A_Hour%:%A_Min%) :{space} ; press f1
Return
I tried writing this to test:
v_test := "testing string in v_test"
f2::
MsgBox, %v_test%
RETURN
But the message box come up blank. Why isn't the message box displaying "testing string in v_test"?
The variables were never initialized because the variable was not contained within the auto-execute section of the autohotkey script.
To fix this, I had to put the variable declaration at the top of the script, before any RETURNs, EXITs, hotkeys, or hotscripts. I also could have written the variable as GLOBAL, inside of a function, then called the function.

User to change a variable in a compiled macro

I have a variable with a value stored in an AutoHotKey macro. I want the user to be able to change the variable whenever they want and to have the macro use the new value stored in the variable when the macro is launched in the future. Is it possible to change a variable in a running macro and have the macro use the new variable value the next time the macro is launched? Alternatively, is there a way for the user to change a variable in a compiled macro? I'm also wondering about how to the user interface would be like for the user to be able to change the variable when the user chooses to change the variable?
Sure, you can store the variable in a text file. This will be the most simple and reliable method to change a variable and store it permanently.
Here is an example. Create a file "config.txt" in the same directory and put the text there. Pressing F1 will reload the text file into variable v.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
fname := "config.txt" ; define file name (must be in the same directory)
loadvar(fname) ; load file
return ; end main
; loadvar function
loadvar(fname){
global v
FileRead, v, %fname%
tooltip %v%
}
; reload variable
F1::
loadvar(fname)
return

How to change a variable for the whole program

So I have a program with a Tkinter interface. I used just one class and a few commands but when I change a variable in a command it doesn' t change it for the whole program, but only in the command. How can I make that variable change for the whole program in a command?
Use global to declare a global variable
global myVar

Replicate ++ operator via AutoHotKey script?

Let's say I'm programming in a text editor and want to iterate a variable:
i++
Now, let's say for whatever reason, this language doesn't ++ but rather:
i = i + 1
this is annoying when you are used to the first syntax. I want a script which when receiving ++ translates this to a series of commands I send (control shift left arrow, etc, this is not the part I'm having trouble with).
However I cannot seem to get this method to execute:
+ & +::
Msgbox test
return
For whatever reason though, this is not fully being called - I do not see the keystroke for + when typing so I know it is somehow getting to that method, but, not registering the second +.
How can I call a method using the keystrokes ++ as a trigger?
There's a really great library called RegEx Powered Dynamic Hotstrings. And replacing someVar++ with someVar = someVar + 1 reeks of RegEx!
This one line will do what you want:
hotstrings("(\w+)\+\+", "%$1% = %$1% {+} 1")
This will work for every variable name that's alphanumeric (plus underscore): [a-zA-Z0-9_]
Of course, this won't work for every language since it strongly depends on the syntax. For example, some languages use := to assign expressions, other languages need a semicolon to complete a statement and so on...
Hotstrings WILL work for this occasion, however, you might need the "?" modifier for it to grab even within a "word" try it like this:
:*?:++::
ClipboardOld := ClipboardAll,Clipboard := ""
Send, +^{Left}^c
Clipwait
Send, ^{Right} = %Clipboard% {+} 1
Clipboard := ClipboardOld,ClipboardOld := ""
return
This also preserves the clipboard.

AutoHotKey: hotstring variable's?

How do I call global variables in hotstring functions.
This works:
::hlw::
hlwvar = Hello World
sendInput %hlwvar%
return
This doesn't:
hlwvar = Hello World
::hlw::
sendInput %hlwvar%
return
I got this answer from "Joel T. 33 / M / Seattle, WA" through Aardvark. I'm posting it here because it was quite useful.
--
Your second form should actually work; try pasting just those 4 lines into a new script and run it to see. Most likely the problem is that in your second example, "hlwvar = Hello World" is not actually being executed because it isn't at the top of the script. When AHK first runs a script, it starts from the top and executes until it encounters a "return" or a hotstring/hotkey definition. Therefore, you should always define your global vars and any other global setup at the top of your script, and once all the script "initialization" stuff is done there, end it with a "return". Then put all your hotstrings/hotkeys/functions below that point.
One thing I like to do is put all my global stuff into a function, e.g.
Init()
{
global
someglobalvar = myvalue
return
}
Then at the top of my script I have
Init()
return
This makes it really easy to identify at a glance where my init stuff lives, as well as move the init routine elsewhere if desired. Note that the "global" keyword must be the first command in a function definition if you want all of the variables assigned inside said function to be available globally.
As Chris mentioned the following two codes work exactly the same for me:
::hlw::
hlwvar = Hello World
sendInput %hlwvar%
return
and
hlwvar = Hello World
::hlw::
sendInput %hlwvar%
return