How to modify the image in RichTextLabel using code ?

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

Take a look at the below function I have. It adds a 64x64 Sprite in the game but multiple times. I want to somehow connect the Image with a variable that keeps my lifepoints. For example 3 lifepoints will mean 3 hearts on screen and so on. How am I supposed to do something like that ?

>    func resistanceDisplay():
>     	var t = Texture.new()
>     	t=load("res://Textures/Heart.png")
>     	resistancelabel.add_image(t,64,64) #resistancelabel just refers to RichTextLabel
:bust_in_silhouette: Reply From: AlexTheRegent

If you want to display hearts in UI, you can use HBoxContainer or VBoxContainer (based on direction of your healthbar). To add one heart you need to add new TextureRect. To take one heart you can simply remove first child of box container (but make sure that you have hearts there).

Then to add heart:

var heart := TextureRect.new()
heart.texture = load("res://Textures/Heart.png")
boxcontainer.add_child(heart)

And to take heart:

if boxcontainer.get_child_count() > 0:
    boxcontainer.get_child(0).queue_free()