My AHK script is not working as intended: pause/unpause & autoclicker

Ask gaming related questions (AHK v1.1 and older)
ReyAHK
Posts: 14
Joined: 11 Mar 2021, 09:21

Re: My AHK script is not working as intended: pause/unpause & autoclicker

Post by ReyAHK » 23 Apr 2024, 09:50

i tried doing this

Code: Select all

~*q::
If (AltLmb == 0) 
 {
        AltLmb := 1
        a := 3
}
Gosub Update
cooling2(hk) {
 Static wait := QAbilityCD
 now := A_TickCount
 If cooling2.HasKey(hk) && cooling2[hk] > now
  Return True
 cooling2[hk] := now + wait
}
LmbCD := 100
return

#If !cooling(A_ThisHotkey)

~*LButton::
If (AltLmb = 0)
    a++
If (AltLmb = 1)
    Gosub Reset
Gosub AltCheck
Gosub Update
cooling(hk) {
 Static wait := LmbCD
 now := A_TickCount
 If cooling.HasKey(hk) && cooling[hk] > now
  Return True
 cooling[hk] := now + wait
}
    SendInput, {LButton}
KeyWait, LButton, D
    Settimer, Clik, % LmbCD
KeyWait, LButton, U
    SetTimer Clik, Off

Sleep 10
LmbCD := (CooldownOfLmb*1000)
Return
But the cooldown code cant read variable "LmbCD". I don't know I how I can do so.

Also here's the entire code:

Code: Select all

#SingleInstance
#MaxThreadsPerHotkey 5
#UseHook

Suspend on
Global cooling := {}
Global cooling1 := {}
Global cooling2 := {}

;-------Settings-------


MouseOffSet := 100 ;Mouse coordinate offset before spinning 180
TurnRange := 100 ;Range spin
Fast := 0 ;Speed of the turn
MonitorResolution := 1920 ;Resolution of the monitor

;-Cooldown-

CooldownOfLmb := 0.5 ;Lmb cooldown in seconds
CooldownOfQ := 5 ;Q cooldown in seconds


;-------End_of_Settings-------
a := 0
AltLmb := 0
LmbCD := (CooldownOfLmb*1000)
QAbilityCD := (CooldownOfQ*1000)
HalfOfMonitorResolution := (MonitorResolution / 2)
LeftSpin := (-1*TurnRange)
LeftBack := ((-1*CenterCoordinateZero)+TurnRange)
RightBack := (-1*(CenterCoordinateZero+TurnRange))
NegMouseOffSet := (MouseOffSet*(-1))

AimOffSet:
MouseGetPos, xpos
CenterCoordinateZero := (xpos-HalfOfMonitorResolution)
Return

\::
Suspend
return

Gosub Reset

u::
Gosub AimOffSet
ToolTip % "X = " CenterCoordinateZero
Return

Update:
ToolTip % "AltLmb = " AltLmb "`n"
        . "a = " a "`n"
Return

AltCheck:
If (a = 3)
    AltLmb := 1
    ALmb := True
If (a > 3)
    Gosub Reset
return

Reset:
AltLmb := a := 0
ALmb := False
Return

Clik:
SendInput,{LButton} 
a++
Gosub AltCheck
Gosub Update
Return

#If !cooling1(A_ThisHotkey)

~*f::
Send, f
If (AltLmb == 0)
{
    Sleep 200
    SendInput, {LButton}
    a++
}
Gosub AltCheck
Gosub Update
cooling1(hk) {
    Static wait := 300
    now := A_TickCount
    If cooling1.HasKey(hk) && cooling1[hk] > now
     Return True
    cooling1[hk] := now + wait
   }
return

#if ALmb

~s & LButton::
Sleep 200
Gosub AimOffSet
LeftSpin := (-1*TurnRange)
LeftBack := ((-1*CenterCoordinateZero)+TurnRange)
RightBack := (-1*(CenterCoordinateZero+TurnRange))
If (CenterCoordinateZero >= MouseOffSet) 
 {
    MouseMove TurnRange, 0, Fast, R
    Sleep 10
    SendInput, {LButton}
    a++
    Sleep 100
    MouseMove RightBack, 0, Fast, R
}
If (CenterCoordinateZero <= NegMouseOffSet) 
 {
    MouseMove LeftSpin, 0, Fast, R
    Sleep 10
    SendInput, {LButton}
    a++
    Sleep 100
    MouseMove LeftBack, 0, Fast, R
}
Gosub AltCheck
Gosub Update
return

#If !cooling2(A_ThisHotkey)

~*q::
If (AltLmb == 0) 
 {
        AltLmb := 1
        a := 3
}
Gosub Update
cooling2(hk) {
 Static wait := QAbilityCD
 now := A_TickCount
 If cooling2.HasKey(hk) && cooling2[hk] > now
  Return True
 cooling2[hk] := now + wait
}
LmbCD := 100
return

#If !cooling(A_ThisHotkey)

~*LButton::
If (AltLmb = 0)
    a++
If (AltLmb = 1)
    Gosub Reset
Gosub AltCheck
Gosub Update
cooling(hk) {
 Static wait := LmbCD
 now := A_TickCount
 If cooling.HasKey(hk) && cooling[hk] > now
  Return True
 cooling[hk] := now + wait
}
    SendInput, {LButton}
KeyWait, LButton, D
    Settimer, Clik, % LmbCD
KeyWait, LButton, U
    SetTimer Clik, Off

Sleep 10
LmbCD := (CooldownOfLmb*1000)
Return
User avatar
mikeyww
Posts: 28872
Joined: 09 Sep 2014, 18:38

Re: My AHK script is not working as intended: pause/unpause & autoclicker

Post by mikeyww » 23 Apr 2024, 10:12

Inserting a function definition into a hotkey subroutine does not call the function. You would call the function by naming it on its own line, and providing any needed parameters in parentheses. Therefore, for clarity, I generally recommend moving functions to the bottom of your script; at least, that is what I typically do in most cases. When you do that, you will see that you need to call any functions that you want to execute.

You can learn how to debug your script. I usually start by simplifying it to essential parts. I then want to inspect the values of variables, function calls (values returned), and conditional statements. You can do this by displaying the values, logging them, or using one of AHK's debugging commands or tools. I find that most bugs are identified by using these approaches. KeyHistory is also often helpful when troubleshooting hotkeys, mouse clicks, keyboard activity, and Send commands.

That is my advice. You do not need to wonder what the script does, because you can use the script to find out! Best wishes for success. I will sign off here; I think that you have the tools that you need to get the rest of the way to your goal!
ReyAHK
Posts: 14
Joined: 11 Mar 2021, 09:21

Re: My AHK script is not working as intended: pause/unpause & autoclicker

Post by ReyAHK » 29 Apr 2024, 08:37

I have FInished the code:
~

Code: Select all

#SingleInstance
#MaxThreadsPerHotkey 5
#UseHook

Suspend on
Global cooling1 := {}
Global cooling2 := {}

;-------Settings-------


;-Game_Settings-

Sensitivity := 
DPI :=

;-Code_Settings-

MouseOffSet := 100 ;Mouse coordinate offset before spinning 180
TurnRange := 100 ;Range spin
Fast := 0 ;Speed of the turn
MonitorResolution := 1920 ;Resolution of the monitor

;-Cooldown-

CooldownOfLmb := 0.5 ;Lmb cooldown in seconds
CooldownOfQ := 5 ;Q cooldown in seconds


;-------End_of_Settings-------

EDPI := Sensitivity * DPI
a := 0
AltLmb := 0
LmbCD := CooldownOfLmb * 1000
QAbilityCD := CooldownOfQ * 1000
HalfOfMonitorResolution := (MonitorResolution / 2)
LeftSpin := (-1*TurnRange)
LeftBack := ((-1*CenterCoordinateZero)+TurnRange)
RightBack := (-1*(CenterCoordinateZero+TurnRange))
NegMouseOffSet := (MouseOffSet*(-1))
LButtonCooldown := 0
;TurnRange/EDPI

AimOffSet:
MouseGetPos, xpos
CenterCoordinateZero := (xpos-HalfOfMonitorResolution)
Return

\::
Suspend
return

Gosub Reset

u::
Gosub AimOffSet
ToolTip % "X = " CenterCoordinateZero
Return

Update:
Gosub AimOffSet
ToolTip % "AltLmb = " AltLmb "`n"
        . "a = " a "`n"
        . "Value test = " CenterCoordinateZero "`n"
Return

AltCheck:
If (a = 3)
    AltLmb := 1
    ALmb := True
If (a > 3)
    Gosub Reset
return

Reset:
AltLmb := a := 0
ALmb := False
Return

Clik:
SendInput,{LButton} 
a++
Gosub AltCheck
Gosub Update
Return

~*LButton::
if (A_TickCount > LButtonCooldown) {
If (AltLmb = 0)
    a++
If (AltLmb = 1)
    Gosub Reset
Gosub AltCheck
Gosub Update
  LButtonCooldown := A_TickCount + LmbCD
}
    SendInput, {LButton}
KeyWait, LButton, D
    Settimer, Clik, % LmbCD
KeyWait, LButton, U
    SetTimer Clik, Off

Return

#If !cooling1(A_ThisHotkey)

~*f::
Send, f
If (AltLmb == 0)
{
    Sleep 200
    SendInput, {LButton}
    a++
}
Gosub AltCheck
Gosub Update
cooling1(hk) {
    Static waitC := 300
    nowC := A_TickCount
    If cooling1.HasKey(hk) && cooling1[hk] > nowC
     Return True
    cooling1[hk] := nowC + wait
   }
return

#if ALmb

;~s & LButton::
;Sleep 100
;Gosub AimOffSet
;LeftSpin := (-1*TurnRange)
;LeftBack := ((-1*CenterCoordinateZero)+TurnRange)
;RightBack := (-1*(CenterCoordinateZero+TurnRange))
;If (CenterCoordinateZero >= MouseOffSet) 
; {
;    MouseMove TurnRange, 0, Fast, R
;    Sleep 10
;    SendInput, {LButton}
;    a++
;    Sleep 100
;    MouseMove RightBack, 0, Fast, R
;}
;If (CenterCoordinateZero <= NegMouseOffSet) 
; {
;    MouseMove LeftSpin, 0, Fast, R
;    Sleep 10
;    SendInput, {LButton}
;    a++
;    Sleep 100
;    MouseMove LeftBack, 0, Fast, R
;}
;Gosub AltCheck
;Gosub Update
;return

#If !cooling2(A_ThisHotkey)

~*q::
If (AltLmb == 0) 
 {
        AltLmb := 1
        a := 3
}
Gosub Update
cooling2(hk) {
 Static waitB := 5000
 nowB := A_TickCount
 If cooling2.HasKey(hk) && cooling2[hk] > nowB
  Return True
 cooling2[hk] := nowB + wait
}
if (LButtonCooldown > 0) {
    LButtonCooldown := A_TickCount + 90
    }
return
`
After this I created 2 other codes for other skillsets. But one of them has a bug, when I press LButton for the first time the [HaveLmbArrow] variable is set to 0 when it should have been set to 1.

Heres the bugged code:
`

Code: Select all

Mode := 0
ArrowAmount := 0
HaveLmbArrow := 0

TooUpdate:
ToolTip % "Mode = " Mode "`n"
        . "ArrowAmount = " ArrowAmount "`n"
        . "HaveLmbArrow = " HaveLmbArrow "`n"
Return

ModeChecker:
Sleep, 1
If (Mode >= 3) {
    Mode := 0
}

*$LButton::
if (HaveLmbArrow = 0) {
    HaveLmbArrow := 1
    While (Mode != 0) {
        Gosub ModeChange
        Sleep, 89
    }
} else {
    HaveLmbArrow := 0
    if (ArrowAmount = 1) {
        While (Mode != 2) {
            Gosub ModeChange
            Sleep, 89
        }
    }
}
SendInput,{LButton}
Return

ModeChange:
Send, f
Mode++
Gosub ModeChecker
Return
`
Heres the entire code for this new skillset code:
`

Code: Select all

#SingleInstance

Suspend on

\::
Suspend
return

Mode := 0
ArrowAmount := 0
HaveLmbArrow := 0

CRTIMER := 79

TooUpdate:
ToolTip % "Mode = " Mode "`n"
        . "ArrowAmount = " ArrowAmount "`n"
        . "HaveLmbArrow = " HaveLmbArrow "`n"
Return

u::
Mode := 0
ArrowAmount := 0
HaveLmbArrow := 0
ToolTip
Return

ModeChecker:
Sleep, 1
If (Mode >= 3) {
    Mode := 0
}
Return

*$LButton::
Gosub LButton
Gosub TooUpdate
Return

LButton:
if (HaveLmbArrow = 0) {
    HaveLmbArrow := 1
    While (Mode != 0) {
        Gosub ModeChange
        Sleep, 89
    }
} else {
    HaveLmbArrow := 0
    if (ArrowAmount = 1) {
        While (Mode != 2) {
            Gosub ModeChange
            Sleep, 89
        }
    }
}
SendInput,{LButton}
Return

*$f::
Gosub ModeChange
Gosub TooUpdate
Return

~*e::
ArrowAmount := 1
While (Mode != 2) {
    Gosub ModeChange
    Sleep, 89
}
Gosub TooUpdate
Return

*$q::
While (Mode != 2) {
    Gosub ModeChange
    Sleep, 89
}
If (ArrowAmount >= 1) {
    ArrowAmount := 0
}
Send, q
Gosub TooUpdate
Return

ModeChange:
Send, f
Mode++
Gosub ModeChecker
Return
ReyAHK
Posts: 14
Joined: 11 Mar 2021, 09:21

Re: My AHK script is not working as intended: pause/unpause & autoclicker

Post by ReyAHK » 29 Apr 2024, 23:38

Its fix now
Post Reply

Return to “Gaming Help (v1)”