A better animation canceling AutoHotKey script

NathanAldenSr

Greenhorn
I discovered today that animation canceling is a thing in Stardew Valley. I found the AutoHotKey script that I'm sure everyone's seen before:


However, being a fan of game engine design (I'm writing my own game engine), I realized the sleep values could likely be tweaked a bit. Here is the resulting script:

Code:
#SingleInstance, Force
#IfWinActive, Stardew Valley

SleepForFrames(x)
{
    Sleep, x * 16.666
}

Space::

While GetKeyState("Space", "P")
{
    SendEvent, {LButton Down}
    SleepForFrames(1)
    SendEvent, {LButton Up}

    SleepForFrames(5) ; This value depends heavily on the actual FPS you're getting

    SendEvent, {r Down}{Delete Down}{RShift Down}
    SleepForFrames(1)
    SendEvent, {r Up}{Delete Up}{RShift Up}
}

SleepForFrames(1)
return
As you can see, I extracted a function called SleepForFrames to make it more obvious what is going on. I was able to reduce the delay by approximately three frames compared to the script from Reddit.

Games have what is known as a "game loop." Traditionally, the loop will run once every 1/60 of a second, or approximately 16.6 (repeating) milliseconds. Many games will process input on every frame, and it appears (although I have not verified this by looking at the code) that Stardew is no exception. Using this knowledge, we can tweak the number of frames to delay by accordingly.

I should note that low-delay timers are notoriously unpredictable on Windows. I've run into these issues myself in my own game engine. Delays less than Windows 10's high-resolution timer frequency are not guaranteed and are often longer in execution. Still, the above script worked fine on my system and it's easier for most folks to modify without needing to know how to calculate FPS.
 

FilthyGorilla

Local Legend
What exactly will be faster?
Responce time or actual hard movement ig?
Thanks nonetheless, even if I do not try it out.
 
Last edited:

FilthyGorilla

Local Legend
This my script right now, what exactly will be increased and by what measure.
I am on windows 10.
I have the keys all binded to different keys so I don't think it matters for me does it?
I feel as if it would only help for those who have it rebinded to a single key.
Screenshot (1277).png
 

qwazr

Newcomer
Necro, I know, but this is on the first page of Google and I think most people seeing this post would find this helpful.
I've updated your code for AutoHotKey V2, and I've also made some changes so that it helps clean up on repetition if you have multiple hotkeys.
Also made it adaptable for other games, should post it around some other places so more people find it...

Code:
;General Functions, these should apply to most games so feel free to try using them in scripts for other games.

GameInput(keys*)
{
    ;Sends keys in a way that game engines can understand.
    ;Input: Key(s) that you want to press.
    ;Example Usage: GameInput("a")
    ;To send multiple keys at the same time: GameInput("a","b","c")
    ;To click: GameInput("LButton")

    for index,key in keys
    {
        downtext .= "{" . key . " down}"
        uptext .= "{" . key . " up}"
    }

    Send downtext
    FrameSleep(1)
    Send uptext
}

FrameSleep(x)
{
    ;Input: how many frames you want to sleep for.
    ;Example Usage: FrameSleep(1)
    Sleep x * 16.666
}


;Stardew Valley Specific stuff

#HotIf WinActive("ahk_class SDL_app") 
;If you're planning to adapt this to another game, you should include a line like this! In fact, please just always do it wherever relevant, it saves a lot of headache.
;You can get the correct ahk_class using the Window Spy which is included with AutoHotKey.

AnimationCancel()
{
    GameInput("Del","RShift","r")
}

Space::
{
    GameInput("LButton")
    AnimationCancel()
}
For the people who know literally nothing about scripting and don't want to bother reading code no matter how simple, You'll just want to change the line Space:: to whatever other key you want (e.g. a::). This is case-sensitive so make sure to write it in lowercase! You can also do Clicks (LButton:: or RButton::) or the side buttons of your mouse (XButton4:: or XButton5::).
 
Top