← Pixel Dreamer Studio

A parry window that is the same length on every screen

Compare the time of the press with the time of the hit, and lock out a press that parried nothing.

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.

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

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