Show a variable with sprites

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godot_Starter

I have a Player with a

var Livepoints = 20

I want to shoe these Livepoints with small HeartSprites. I have twenty of them. Theier names are like this:
Livepoint1
Livepoint2
Livepoint3

Now I want that there are only so much sprites visible as much Livepoints the Player has.

What’s the point of updating it if the value hasn’t changed?

jgodfrey | 2020-03-14 01:12

Ok, then it is doesnt matter when it is upgradet.
(I edit the question)

Godot_Starter | 2020-03-14 08:30

:bust_in_silhouette: Reply From: 1izNoob

Just to make sure I understand you right: you want to display the health of the player by showing hearts on the screen?

I am not sure why you want to name the sprites differently, do the hearts look different?
Assuming that there is only 1 heart sprite, I would approach it like this:

  1. Load the sprite into whatever scene you want to use it in (I guess making a HUD scene with a control node would be best)

Like this:

const HEART = load('path_to_your_heart')
  1. On the HUD scene create something that can hold and organise the hearts, like a HBoxContainer node.

  2. Now you want to create a function that adds a heart sprite to that HBoxContainer for every point of health the player has.

Something like this:

func add_hearts():
    for heart in Lifepoints:
        $HBoxContainer.add_child(HEART.instance())

Once you actually play the game, you want to eventually add some more functionality that whenever the player gets hit or gains a lifepoint, that you empty the entire HBoxContainer and then add the hearts again depending on the new amount of lifepoints.

Hope that helps.

P.S. I don’t have Godot open in front of me, but I am reasonably sure it would work like that. But you might need to tinker around with it.

But now every heart is on the same place. How can I fix that?

Godot_Starter | 2020-03-14 13:24

The HBoxContainer should take of that. I assume you are putting the heart into a sprite node? Replace that sprite node with a textureRect node and they should appear next to each other.

1izNoob | 2020-03-14 14:47