Player shield
In this section, you’ll add a shield to the player and a display element to the HUD
showing the current shield level.
First, add the following to the top of the player.gd
script:
signal shield_changed @export var max_shield = 100.0 @export var shield_regen = 5.0 var shield = 0: set = set_shield func set_shield(value): value = min(value, max_shield) shield = value shield_changed.emit(shield / max_shield) if shield <= 0: lives -= 1 explode()
The shield
variable works similarly to lives
, emitting a signal whenever it changes. Since the value will be added to by the shield’s regeneration, you need to make sure it doesn’t go above the max_shield
value. Then, when you emit the shield_changed
signal, you pass the ratio of shield
/ max_shield
rather than...