Is there a better way?

Ask gaming related questions (AHK v1.1 and older)
cheatH12
Posts: 1
Joined: 24 Oct 2020, 09:57

Is there a better way?

Post by cheatH12 » 24 Oct 2020, 12:27

Here is a snippet of my script:

Code: Select all

$x::
	MouseClick, Left
	sleep, 275
	MouseClick, Left
	sleep, 355
	MouseClick, Left
	sleep, 405
	send, {r}
	sleep, 50
	send, {r}
	sleep, 240
return
It's a simple attack script. I hold down "x" and the commands are sent over and over as long as the hotkey is down, like in a loop.
The issue is I'm most often not in a position to complete the sequence, most of the time I need to dodge enemy attacks. When i'm forced to release the hotkey, the script attempts to finish out the current iteration and those extra actions interferes with what I currently want my character to do.
What I want is for the script to abort the current iteration as soon as the hotkey isn't down, then start a new iteration when the hotkey is pressed again.
I tried a few ideas but only this did what I wanted:

Code: Select all

$x::
	if (GetKeyState("x","P")){
		MouseClick, Left
		sleep, 275
	}
	if (GetKeyState("x","P")){
		MouseClick, Left
		sleep, 355
	}
	if (GetKeyState("x","P")){
		MouseClick, Left
		sleep, 405
	}
	if (GetKeyState("x","P")){
		send, {r}
		sleep, 50
	}
	if (GetKeyState("x","P")){
		send, {r}
		sleep, 240
	}
return
The script checks if the hotkey is held down before sending the next action.
It works the way I want it to, but it's far from elegant.
Would anybody show me a better way to do this?

Rohwedder
Posts: 7647
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Is there a better way?

Post by Rohwedder » 25 Oct 2020, 02:35

Hallo,
try:

Code: Select all

x Up::x:=False
$x::
	x := True ;keeps the respective thread active
	MouseClick, Left
	Sleep(275,x)
	MouseClick, Left
	Sleep(355,x)
	MouseClick, Left
	Sleep(405,x)
	send, {r}
	Sleep(50,x)
	send, {r}
	Sleep(240,x)
return

Sleep(Time,ByRef On:=True)
{ ;like "Sleep, Time", but if On becomes Zero, the Thread ends
    End:= A_TickCount + Time
    While, S:= End-A_TickCount > 0
        IF On
            Sleep,S>100?100:S
        Else Exit
} ;a long Sleep will be segmented to be able to interrupt it fast
The x Up thread deletes the variable x, which keeps the $x thread active.
Autohotkey returns to the $x thread and exits it.

Post Reply

Return to “Gaming Help (v1)”