Precise Timing Golf Swing Topic is solved

Ask gaming related questions (AHK v1.1 and older)
shanuck
Posts: 2
Joined: 01 Sep 2017, 12:26

Precise Timing Golf Swing

19 Jan 2018, 20:21

Hello I am new to AHK and have searched the forums to no avail looking for the definitive best way to achieve a precision timer. This is the script I have came up with, just wondering if it is the best i can do, the timing only hits the mark about 50% of the time. Thanks in advance. I dont mind reading and doing my own work if you just send me the link of a topic I perhaps have missed

Code: Select all

#NoEnv  
;SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
CoordMode, Mouse, Screen
Px=1243
Py=1050
^Numpad0::     ;////////100% Driver and Irons//driver
{
MouseClickDrag, Left, %Px%, %Py%, Px, Py - 400, 5
Critical
Sleep, 2319.5
Click, Left
}
return

NumpadSub::reload
User avatar
eventhorizon
Posts: 158
Joined: 27 Oct 2016, 14:22

Re: Precise Timing Golf Swing  Topic is solved

20 Jan 2018, 07:02

See the help file for the SetTimer command to try and get something closer to what you need. There are some notes in that entry that may be important for what you're trying to accomplish. Instead of running continuously, timers can be set to run in One-Shot mode meaning they only run once when called. This can be handy when you need a timer to start on command or run at non-periodic starting times
A computer lets you make more mistakes faster than any invention in human history – with the possible exceptions of handguns and tequila.
shanuck
Posts: 2
Joined: 01 Sep 2017, 12:26

Re: Precise Timing Golf Swing

29 Jan 2018, 12:01

eventhorizon wrote:See the help file for the SetTimer command to try and get something closer to what you need. There are some notes in that entry that may be important for what you're trying to accomplish. Instead of running continuously, timers can be set to run in One-Shot mode meaning they only run once when called. This can be handy when you need a timer to start on command or run at non-periodic starting times
Thank you for the reply, I am reading up on set timer.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Precise Timing Golf Swing

01 Feb 2018, 10:38

1) AFAIK, there is zero difference between the accuracy of SetTimer and the accuracy of Sleep.
2) 2319.5 Neither Sleep nor SetTimer support decimals.
3) Your sleep is having zero effect on the speed of the mouse movement - that is controlled by this line: MouseClickDrag, Left, %Px%, %Py%, Px, Py - 400, 5
The sleep is merely controlling the time between release of the drag and the click.
4) If you wish for more accurate timers (But I doubt this is your issue), then use MicroTimer
User avatar
eventhorizon
Posts: 158
Joined: 27 Oct 2016, 14:22

Re: Precise Timing Golf Swing

01 Feb 2018, 20:03

Another possible way to improve the timing would be through a custom routine to use the A_TickCount variable to time the interval between swings. I use this method in many of my mmo game fight routines to get more precise timing between one skill firing off and the next one. Since A_TickCount is a variable you can use it in your own variable to wait from the beginning or end of one event to the start of the next event similar to SetTimer or Sleep. Here's a simple example that pops a second message box after the first is closed...

Code: Select all

; this produces the first message box that automatically shuts down after 3 seconds
MsgBox, 0x1000, Line%A_LineNumber% First Msgbox, This is the first message box, 3
; start the custom delay routine
reopenDelay := 3000 ;<- this is the time to wait for the second message box
reopen := (A_TickCount + reopenDelay) ;<- wait 3 seconds 
while (A_TickCount < reopen)
{	;do nothing
}
MsgBox, 0x1000, Line%A_LineNumber% Second Msgbox, This is the second message box, 3
exitapp
By changing the value of the reopen delay either manually or programmatically you can have variable sleeps in your routines or within the "do nothing loop you can have your script do other stuff unlike what sleep does. Since sleep basically shuts down your script unconditionally for a definite period of time the A_TickCount loop like SetTimer lets you do other stuff until it times out. Of course, the more stuff you try to do inside the loop the more the loop timing will degrade.
A computer lets you make more mistakes faster than any invention in human history – with the possible exceptions of handguns and tequila.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Precise Timing Golf Swing

02 Feb 2018, 06:32

Code: Select all

while (A_TickCount < reopen)
{	;do nothing
}
DO NOT do this. It will use LOTS of CPU time.
On my work PC, this code alone uses ~5% CPU

However, with MicroTimer, I can have a 1 MILLISECOND timer running and the CPU usage is like 0.1%

Code: Select all

#SingleInstance force
#Persistent

asm := CLR_LoadLibrary("MicroTimer\MicroTimer.dll")
; Use CLR to instantiate a class from within the DLL
mt := asm.CreateInstance("MicroTimer")
MyTimer := mt.Create(Func("Test"), 1)
MyTimer.SetState(1)
return

Test(){
	;do nothing
}

^Esc::
	ExitApp

User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Precise Timing Golf Swing

02 Feb 2018, 07:04

Since sleep basically shuts down your script unconditionally for a definite period of time the A_TickCount loop like SetTimer lets you do other stuff until it times out
No it does not. A loop is nothing like SetTimer. AHK_L is a single-threaded language. If one thread is active (Sleeping or being in a loop that does nothing DOES NOT relinquish control to another pseudo-thread) then another cannot be. SetTimer however fires the timer code, and once that code returns, resumes processing of other stuff.
This also extends to hotkeys - they will interrupt the current thread and not relinquish until they complete.

Code: Select all

CoordMode, ToolTip, Screen

while (true)
{	
    ToolTip, MAIN Loop %A_TickCount%, 0, 0, 1
    Sleep 10
}
return

F12::
    ToolTip, HOTKEY CODE %A_TickCount%, 500, 0, 2
    Sleep 1000
    return
Last edited by evilC on 02 Feb 2018, 22:40, edited 1 time in total.
User avatar
eventhorizon
Posts: 158
Joined: 27 Oct 2016, 14:22

Re: Precise Timing Golf Swing

02 Feb 2018, 22:10

Well, obviously I'm mistaken with my examples so i'll retire the field. Timing in any real time manner is truly a black art. So many ways to do it. Good luck.
A computer lets you make more mistakes faster than any invention in human history – with the possible exceptions of handguns and tequila.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Precise Timing Golf Swing

02 Feb 2018, 22:43

A black art indeed. I suffered under the same misconceptions for a good while myself. It also does not help that a lot of example code given (And even some documentation entries) use bad practices in the name of simplicity / brevity.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 65 guests