← Pixel Dreamer Studio

An attack that hits even when a frame is slow

Value keys only show where the animation landed. A Call Method key runs even when its frame is jumped over.

If an attack animation switches a hitbox on with one value key and off with the next, a frame that steps over both never sees it on. In our Godot 4.7 test a hitbox on for 0.05 s was seen in 3 frames at 60 fps, 1 at 20 and 15 fps, and 0 at 12 fps or when a single frame at 60 fps took 0.1 s. A Call Method key at the same moment was called once in every case, and a hit window opened from it, which stays open for at least one check, was checked at least once every time.

The code

Save it as hit_window.gd. On the attacker, add var window := preload("res://hit_window.gd").new() and a function func open_hit_window(): window.open(0.05), and call it from a Call Method track key where the swing starts, instead of switching the hitbox with value keys. Each frame, check for hits while window.is_open(), then call window.tick(delta). Call window.close() if the attack is cancelled.

extends RefCounted
## An attack's hit window that a low frame rate or a stutter cannot skip.
## Open it from a Call Method key in the attack animation, check for hits
## while it is open, then tick it. It stays open for the given seconds, and
## always for at least one check, even if the whole window fell inside one frame.
##   var window := HitWindow.new()
##   func open_hit_window(): window.open(0.05)        # Call Method key at the swing
##   func _physics_process(delta):
##       if window.is_open():
##           check_hits()
##       window.tick(delta)

var _left := 0.0
var _must_check := false


func open(seconds: float) -> void:
	_left = seconds
	_must_check = true


func is_open() -> bool:
	return _left > 0.0 or _must_check


func tick(delta: float) -> void:  # call once per frame, after checking hits
	if is_open():
		_must_check = false
		_left -= delta


func close() -> void:  # the attack was cancelled (stagger, dodge)
	_left = 0.0
	_must_check = false

What we measured

A 0.5 s swing with a value key that sets hitbox_on true at 0.20 s and false at 0.25 s, and a Call Method key at 0.20 s. The AnimationPlayer was stepped by hand with advance() at a fixed frame time, with Callback Mode Method set to Immediate, and after each step we counted whether a hit check would see the hitbox. Two runs gave the same counts.

Why it happens

A value track with discrete keys sets the property to the key at or before the time the animation reached. When one frame moves the animation from before 0.20 s to after 0.25 s, the value it lands on is false, and true never happens. A Call Method track calls the keys the animation passed during the frame, so in our test the key at 0.20 s ran even in the frame that jumped from before it to after it. The script turns that one call into a window measured in seconds, and keeps it open until it has been checked at least once.

What to change

What the hit looks like: the Battle VFX Bundle for Godot 4 has 169 ready-made combat effects from 5 packs: gun effects, spell projectiles with cast, trail and hit, block and parry, AoE telegraphs and status effects.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Battle VFX Bundle for Godot 4 has 169 ready-made combat effects from 5 packs: gun effects, spell projectiles with cast, trail and hit, block and parry, AoE telegraphs and status effects.

More how-tos for Battle VFX Bundle for Godot 4: Fast projectiles that never fly through enemies

More how-tos: Hazards, traps and attacks