Global variables and stuff Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tcwvdxom
Posts: 9
Joined: 04 Oct 2017, 23:28

Global variables and stuff

04 Oct 2017, 23:49

HI! Here is my current script (simple auto-fire for gaming):

Code: Select all

~rbutton::
time1 = 1000
time2 = 2000

loop
{
keywait rbutton,D
sendplay {X down}
sleep %time1%
sendplay {X up}
sleep %time2%
}

keywait rbutton,U
sendplay {X up}
As you can see there are 2 variables - time1 and time2. As I read already there is a way to make them global variables. If I somehow manage to make them global, can I assign another hotkeys to change (increase/decrease) the global variables "on the fly"? For example while RBUTTON is pressed down, pressing PAGEUP increases time1 by 100.
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Global variables and stuff  Topic is solved

05 Oct 2017, 00:20

at the moment your variables are in the global space of your script, so your can already add the extra hotkey to do what you need, if you at some point move the variables in to a function or class then you'ed need to make the function global or the variables super global
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
tcwvdxom
Posts: 9
Joined: 04 Oct 2017, 23:28

Re: Global variables and stuff

05 Oct 2017, 02:16

Thanks, your reply cleared things up :)

During trial and error, this script (change variable with hotkey) came out for testing purpose. Maybe someone will find it useful.

Code: Select all

~xbutton2::
var1 := 100
var2 := 10
var3 := 0
var4 := 0

loop
{
var3:=var1 + var4
keywait xbutton2,D
SplashTextOn,,, [%var1%] - [%var2%] - [%var3%] - [%var4%]
Sleep 10
SplashTextOff
}

pgup::
var4+=var2
return

pgdn::
var4-=var2
return
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: Global variables and stuff

05 Oct 2017, 02:30

you may want to be more careful and use functions to encapsulate the variable scope and use the global keyword

Code: Select all

global gMyGlobal := 0

~xbutton2::
xButton2Func() ;Function to encapsulate scope
{
    ct := gMyGlobal ;ct is local
    loop, %ct%
    {
        ;Dostuff n times
    }
}
xButton2Func() ;call the function
return

pgup::
gMyGlobal +=1
return

pgdn::
gMyGlobal -=1
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Descolada, fiaztv1, JKJadan, Pianist and 214 guests