My alternate lives UI

I opted to represent the lives with icons rather than a number.

To do this I used three TextureRects on the HUD node, named Lives 3, Lives 2, Lives 1, then implemented this function in the HUD script:

Screenshot 2023-10-10 at 13.45.51

Then I connect the took_damage signal from the player script, so when the player is hit, the lives_UI function checks the number of lives in the main game script and then hides the corresponding sprite. “…/…” seems to be the path for a parent of the node your script is on, I just dragged the game node over from the scene menu.

3 Likes

Cool, I like the look of that. Nice one :slight_smile:

This looks super cool and in my opinion much better than just the number representing the amount of lives left! To save you a bit of time, I noticed how on line 12 you wrote “$“Lives 3”.visible=false”, which does work but you can actually just type “.hide()” instead of the “.visible=false” thing. Very well done!

1 Like

There’s another way to do this that lets you have as many lives as you want.
In the inspector for the lives TextureRect, set expand mode to ignore size, and stretch mode to tile.

Then in the hud script:

@onready var lives = $Lives # Your lives texturerect

func set_lives(new_lives):
    lives.size.x = 126 * new_lives

126 is the width of the orb.png. If using a different png, you’ll have to change this accordingly.

3 Likes

If you’re keen on exploring the UI elements on your own then there is a neat way to use VBoxContainer and HBoxContainer to organise the score and lives - I also used life textures instead of text. I created an orb scene and instantiated it as a child of a HBoxContainer repeatedly until I reached a maximum lives value. Then when the player lost (or gained lives) I disabled (or enabled) visibility of each orb.
The HBoxContainer automatically organised the textures horizontally and the visible property made it simple to adjust what was shown on-screen.

1 Like

Thank you! Looks a lot better. Instead of calling the player script, I changed the game code slightly:

f

func _on_player_took_damage():
	lives -= 1
	print(lives)
	if lives == 0:
		player.destroyed()
	hud._lives_UI()
1 Like

Privacy & Terms