← Pixel Dreamer Studio

Refill the guard meter only after the hits stop

Reset the time since the last hit on every hit, and refill only once it passes the delay.

If the meter refills all the time, or the delay only counts from the first hit, it refills in the middle of a combo and the guard never breaks. In our Godot 4.7 test, 6 blocked hits of 20 every 0.6 s, with a refill of 30 per second, only took those meters down to 71 and 41. Restart the delay on every hit: the same combo broke the guard at 2.4 s, as 5 hits of 20 should.

The code

Call take_hit() with the guard damage when a blocked hit lands, and tick(delta) every frame. Connect guard_broken to your stagger and Guard Break effect. The same script works for a shield or stamina bar that should only recover after a pause.

extends Node
## Guard meter that refills only after a pause in the attacks.
## Call take_hit(amount) when a blocked hit lands and tick(delta) every frame.

signal guard_broken

@export var max_guard := 100.0
@export var refill_per_second := 30.0
@export var refill_delay := 1.0  # seconds without a hit before it starts to refill

var guard := 100.0
var _since_hit := 999.0


func take_hit(amount: float) -> void:
	_since_hit = 0.0  # every hit restarts the wait, not only the first one
	if guard <= 0.0:
		return
	guard -= amount
	if guard <= 0.0:
		guard = 0.0
		guard_broken.emit()


func tick(delta: float) -> void:
	_since_hit += delta
	if _since_hit >= refill_delay and guard < max_guard:
		guard = minf(guard + refill_per_second * delta, max_guard)

What we measured

6 hits of 20 at 0, 0.6, 1.2, 1.8, 2.4 and 3.0 s, a refill of 30 per second with a 1 second delay, 60 fps for 5 seconds. Two runs with the same numbers.

Why it happens

A refill delay is meant to mean: not hit for a while. A delay that starts once and is never restarted only means: hit at least once, a second ago. Restarting it on every hit makes the meter wait for a real pause, so pressure from fast attacks adds up the way it looks on screen.

What to change

Only block what comes from the front: block from the front with a dot product.

Ready-made defense effects: Combat Defense VFX for Godot 4 has 29 presets in Stylized and Retro PSX looks, including Parry Spark, Perfect Parry, Block Wood, Block Metal, Block Magic and Guard Break.

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 Guard Break, Parry Spark, Perfect Parry, Block Wood, Block Metal and Block Magic.

More how-tos: Hazards, traps and attacks