The usual first version is: if the shield is up, the hit goes to the shield, otherwise to health. In our Godot 4.7 test a 50 point shield and 100 health hit for 20, 20, 20, 30 and 15, 105 in all, ended with 55 health instead of 45, and the shield showed -10. One 80 point hit on a full shield did no health damage at all. Taking only what the shield can hold and passing the rest on gave 45, and 70 after the 80 point hit.
The code
Add the script to the character, call take_damage(amount) for every hit, and connect shield_broken to your shield break effect.
extends Node
## Energy shield that takes damage first. Whatever the shield cannot hold goes
## on to health, and the shield never shows less than 0.
signal shield_broken
var shield := 50.0
var health := 100.0
func take_damage(amount: float) -> void:
var absorbed := minf(shield, amount)
shield -= absorbed
health = maxf(health - (amount - absorbed), 0.0) # the rest of the hit
if absorbed > 0.0 and shield <= 0.0:
shield_broken.emit()
What we measured
Shield 50, health 100. Two runs with the same numbers.
if shield > 0: shield -= damage, else health: the five hits left health 55 and shield -10. One 80 point hit: health 100, shield -30.- The same with the shield clamped at 0: health 55, shield 0, and the 80 point hit still did nothing to health.
- The script: health 45 after the five hits and 70 after the 80 point hit, shield 0, shield_broken sent once each time.
Why it happens
Both first versions decide where the whole hit goes, so the part the shield could not hold disappears: 10 points of the third hit, 30 of the 80. Clamping hides the negative number, but it does not bring the lost damage back.
What to change
- Armor or resistances: lower
amountbefore calling take_damage, so the shield and health see the same hit.
Shield that refills after a pause: a meter that refills while still being hit. Beams that stop at the shield: laser beam stops at the hit. A spinning laser that skips targets: check the swept arc.
Ready-made shield and beam effects: Sci-Fi & Energy VFX for Godot 4 has 33 ready-made sci-fi effects in Stylized and Retro PSX looks, including Laser, Plasma, Mining and Repair beams that stop where they hit, an Energy Shield Bubble and a Black Hole.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Sci-Fi & Energy VFX for Godot 4 has 33 ready-made sci-fi effects in Stylized and Retro PSX looks, including Laser, Plasma, Mining and Repair beams that stop where they hit, an Energy Shield Bubble and a Black Hole.
More how-tos for Sci-Fi & Energy VFX for Godot 4: A laser beam that stops where it hits, the moment you aim
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 parry window that is the same length on every screen
- 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
- 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