If you count the parry window in frames, it is a different length on every monitor: 9 frames is 300 ms at 30 fps and under 40 ms at 240. In our Godot 4.7 test a 9-frame window took presses up to 165 ms before the hit at 60 fps, 65 ms at 144 and 40 ms at 240. Store when the button went down and compare it with when the hit lands instead: a 0.15 s window took 145 to 165 ms at all four frame rates. Then add a short lockout after a press that parried nothing, or mashing the button parries every attack.
The code
Save it as parry.gd and make one per character with var parry = preload("res://parry.gd").new(). Call parry.press(now) when the parry button goes down and parry.hit(now) when an attack lands, with now = Time.get_ticks_msec() / 1000.0 both times. hit() returns "perfect", "parry" or an empty string when the hit goes through.
extends RefCounted
## Parry timing in seconds, so the window is the same at any frame rate,
## with a short lockout so mashing the button does not parry everything.
## Call press() when the parry button goes down and hit() when an attack lands.
## Pass the same clock to both, for example Time.get_ticks_msec() / 1000.0.
var window := 0.15 # pressed this long before the hit, or less = parry
var perfect := 0.05 # pressed this close to the hit = perfect parry
var lockout := 0.35 # a press that parried nothing ignores the next presses this long
var _pressed_at := -INF
var _ignore_until := -INF
func press(now: float) -> void:
if now < _ignore_until:
return # still locked out by a press that missed
_pressed_at = now
_ignore_until = now + lockout
func hit(now: float) -> String: # "perfect", "parry" or "" when the hit lands
var ago := now - _pressed_at
if ago < 0.0 or ago > window:
return ""
_pressed_at = -INF # one press parries one hit
_ignore_until = -INF # a parry that worked lifts the lockout
return "perfect" if ago <= perfect else "parry"
What we measured
Frame rates simulated in Godot 4.7: a press or a hit counts on the first frame at or after it happens. The numbers were the same in two runs.
- 51 single presses from 0 to 250 ms before an attack, 5 ms apart, with a 9-frame window: 51 parried at 30 fps, 34 at 60, 14 at 144 and 9 at 240. The earliest press that still worked was 250 ms before the hit (the earliest we tried), 165, 65 and 40 ms.
- The same presses with the script and a 0.15 s window: 34, 30, 31 and 30 parried, the earliest 165, 145, 150 and 145 ms.
- 50 attacks at random times 0.8 to 2 s apart, at 60 fps. Mashing every 100 ms parried all 50 with no lockout and 19 with a 0.35 s lockout. Mashing every 50 ms: 50 and 27.
- One press per attack, 0 to 250 ms early: 28 of 50 parried with or without the lockout, 9 of them perfect.
- What the lockout costs: a press 300 ms early followed by one 50 ms early parried without a lockout and failed with it. With the first press 450 ms early, both parried.
Why it happens
A frame is not a fixed amount of time. At 30 fps it lasts 33 ms and at 240 fps about 4 ms, so a window of 9 frames is long on a slow screen and nearly impossible on a fast one. Times measured with a clock do not depend on how many frames passed in between. The press and the hit still only register on frames, which is why the seconds version moved between 145 and 165 ms: one frame either way at 30 fps. Without a lockout, a window that remembers the last press also rewards pressing all the time, because there is always a press inside the last 150 ms. With the lockout, mashing parried no more often than one press per attack: 27 and 19 against 28.
What to change
window: how early a press still counts.perfect: the part of it that counts as a perfect parry.lockout: how long a press that parried nothing blocks the next ones. A parry that works clears it.- An attack pressed during a swing that should still come out: an input buffer. Blocking only what comes from the front: a dot product check. A guard bar that should not refill under attack: a regen delay.
What the player sees: Combat Defense VFX for Godot 4 has Parry_Spark and Parry_Perfect as one-shots, with Parry_Perfect_Blue and PSX versions of both: spawn the one that matches what hit() returned at the hit point with PDVFX.spawn() and call play().
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Combat Defense VFX for Godot 4 has 29 ready-made defensive combat effects in Stylized and Retro PSX looks, including Parry Spark, Perfect Parry, Block Wood, Block Metal, Block Magic and Guard Break.
More how-tos: Hazards, traps and attacks
- Turn a 2D character left without the flicker
- A line or rectangle attack that hits what the player sees
- A scare that plays once, and only for the player
- An attack that hits even when a frame is slow
- Growing one cloud or aura zone grows all of them
- A pair of portals that sends you through once
- An expanding shockwave that hits each enemy once
- Explosion damage that walls block, on the frame it goes off
- A fire or poison zone that keeps hurting while you stand in it
- A spike trap that hits whoever is already standing on it
- Who is inside an AoE attack circle
- Block only attacks from the front
- A sword hitbox that hits each enemy once per swing
- Slows and roots that overlap
- Blink forward without going into or through walls
- Knockback that the walk code does not cancel
- Random spots spread evenly inside a circle
- Keep a pressure plate down while anything is still on it
- Refill the guard meter only after the hits stop
- Heal from the damage that actually landed
- Let the shield take what it can and pass the rest of the hit to health
- A saw blade that slides along its path at a steady speed
- Explosion damage that reaches big enemies
- A spinning laser that hits everything it sweeps over