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_changedexport (int) var max_shieldexport (float) var shield_regenvar shield = 0 setget set_shield

The shield variable will work similarly to lives, emitting a signal to the HUD whenever it changes. Save the script and set max_shield to 100 and shield_regen to 5 in the Inspector.

Next, add the following function, which handles changing the shield's value:

func set_shield(value):    if value > max_shield:        value = max_shield    shield = value    emit_signal("shield_changed", shield/max_shield)    if shield <= 0:        self.lives -= 1

Also, since some things, ...

Get Godot Engine Game Development Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.