Sleep duration between 0 and 1

Ask gaming related questions (AHK v1.1 and older)
Luftballong
Posts: 4
Joined: 10 Sep 2023, 13:58

Sleep duration between 0 and 1

10 Sep 2023, 14:05

This is my current script for spamming spacebar in a game

Code: Select all

;OPTIMIZATIONS START
#SingleInstance force
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , A
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input
DllCall("ntdll\ZwSetTimerResolution","Int",5000,"Int",1,"Int*",MyCurrentTimerResolution) ;setting the Windows Timer Resolution to 0.5ms, THIS IS A GLOBAL CHANGE
;OPTIMIZATIONS END

;YOUR SCRIPT GOES HERE
F1::                 
	Suspend
	Return
*space::
	Loop                                                                                                                
	{
		GetKeyState,state,space,P
		If state = U
			Break
		Send,{space}              
		Sleep,21
	}  
It works fine but I want to optimize it to the fullest but I can't since when I put my sleep under 21 it drastically changes how many times spacebar is pressed. Kinda like how the speed changes from 1 sleep to 0 sleep.
Here's another script:
;OPTIMIZATIONS START
#SingleInstance force
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
ListLines Off
Process, Priority, , A
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input
DllCall("ntdll\ZwSetTimerResolution","Int",5000,"Int",1,"Int*",MyCurrentTimerResolution) ;setting the Windows Timer Resolution to 0.5ms, THIS IS A GLOBAL CHANGE
;OPTIMIZATIONS END

;YOUR SCRIPT GOES HERE

Code: Select all

XButton2::
    toggle := !toggle
    if (toggle) {
        SetTimer, Spam_Space, 1
    } else {
        SetTimer, Spam_Space, Off
    }
return

Spam_Space:
	SendInput {Space Down}
	sleep, 1
	SendInput {Space Up}
return
But same problem persists where I can't find an inbetween in 0 and 1 sleep
User avatar
mikeyww
Posts: 27318
Joined: 09 Sep 2014, 18:38

Re: Sleep duration between 0 and 1

10 Sep 2023, 20:50

Some answers about sleep: :arrow: Sleep.
Luftballong
Posts: 4
Joined: 10 Sep 2023, 13:58

Re: Sleep duration between 0 and 1

11 Sep 2023, 16:36

I tried messing with SleepDuration and TimePeriod but nothing changed really. To put it in in perspective heres how many letters I can type within 5 seconds with sleep 1, 2 and 0.
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" sleep 2
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" sleep 1
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" sleep 0
If anyone knows how to get sleep between 1 and 0 I would appreciate it alot.
yuu
Posts: 2
Joined: 11 Sep 2023, 03:26

Re: Sleep duration between 0 and 1

11 Sep 2023, 23:21

i want to get precise 1ms sleep, but cant do it too. the min sleep time is 1.24ms.
and i want to modify SleepDuration to 0.5, but found sleep argument must be DWORD, assigning it to a float seems impossible.
DllCall("NTDLL\NtSetTimerResolution", "UInt", 5000, "UInt", 1, "UInt*", TimerResolutionActual)
DllCall("Sleep", "UInt", SleepDuration)
User avatar
WAZAAAAA
Posts: 89
Joined: 13 Jan 2015, 19:48

Re: Sleep duration between 0 and 1

29 Oct 2023, 19:20

You need to set the Windows' timer resolution to 0.5ms to make sleep precise, and then every time you want to sleep in the script you must use the ZwDelayExecution DllCall sleep in 0.5ms increments (instead of the normal Sleep command). Like this:

Code: Select all

DllCall("ntdll\ZwSetTimerResolution","Int",5000,"Int",1,"Int*",MyCurrentTimerResolution) ;setting the Windows Timer Resolution to 0.5ms, THIS IS A GLOBAL CHANGE
DllCall("ntdll\ZwDelayExecution","Int",0,"Int64*",-5000) ;sleeping for 0.5ms
DllCall("ntdll\ZwDelayExecution","Int",0,"Int64*",-15000) ;sleeping for 1.5ms
PROS OF THIS METHOD:
- sleeps are accurate to 0.5ms increments
- low CPU consumption
CONS:
- it's a "blocking wait", meaning while it's sleeping it freezes the entire AHK thread, so for example hotkeys will be ignored while AHK is frozen for long Sleeps, and SetTimer's will be delayed (but this might not matter for your use case since the sleeps are so short)
- the Windows timer resolution is a "global change" (until you quit the script), but realistically you won't get any downside, and the resolution tends to change all the time by so many different programs anyway
YOU'RE NOT ALEXANDER

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 19 guests