Change a specific section of a label's text

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

Hello! So, I’m making an ingredient counter, where it receives a signal everytime the object has been added to the recipe. It technically works, but the new text either replaces all of the previous text (like the milk value on the code underneath here) or adds it to the side without any space whatsoever.

Here is the code:

extends Label
var Milk = 0
var Macaron = 0
var Cookie = 0




func _ready():
	pass
	
func _on_milky_newIngr(value):#signal emitted when the milk value gets altered
	Milk = value
	text = str("Milk:",Milk)


func _on_Macaron_newMIngr(value):
	Macaron = value
	text += str("Macaron:",Macaron)


func _on_Items_newCookieIngr(value):
	Cookie = value
	text += str("Cookie:",Cookie)

What I wanted to happen:
1st instance (no ingredients added) the label would display:
Cookie: 0
Milk: 0
Macaron: 0

2nd instance, if the cookie was added, the label would update ONLY the number beside the cookie, like this:
Cookie: 1
Milk: 0
Macaron: 0

And so, I was thinking if there is a way to replace that part of the text specifically?

Updated to fix code formatting…

jgodfrey | 2023-02-26 23:00

:bust_in_silhouette: Reply From: AndyCampbell

Hello.
I think I would make that 3 labels (one for each ingredient). Or more likely 3 rows with 2 labels in each row., one static label for the ingredient name (eg "Cookie: ") and another label for the value - that way you can align them all nicely using containers or a grid Using Containers — Godot Engine (stable) documentation in English

However, if you really want to have a single label string for everything, you look at using string formatting so you can substitute the variables for Milk, Macron and Cookie into a preformated string

extends Label
var Milk = 0
var Macaron = 0
var Cookie = 0

func _ready():
	updateLabel()


func onmilky_newIngr(value):#signal emitted when the milk value gets altered
	Milk = value
	updateLabel()


func onMacaron_newMIngr(value):
	Macaron = value
	updateLabel()


func onItems_newCookieIngr(value):
	Cookie = value
	updateLabel()


func updateLabel():
	text = "Cookie: {0}\nMilk: {1}\nMacaron: {2}".format([Cookie, Milk, Macaron])
:bust_in_silhouette: Reply From: jgodfrey

While you could devise a way to update only portions of your label string, it’d probably be more trouble that it’s worth. I’d probably do one of the following:

Use a separate label for each separate ingredient. That way, an update to a given ingredient’s string wouldn’t impact the other ingredient labels.

— or —

To do it with a single label, you could simple recreate the entire string (all ingredients) when any of them changes. So (untested), but something like:

func _ready():
    update_labels()

func _on_milky_newIngr(value):
    Milk = value
    update_labels()

func _on_Macaron_newMIngr(value):
    Macaron = value
    update_labels()

func _on_Items_newCookieIngr(value):
    Cookie = value
    update_labels()

func update_labels():
    var st = "Cookies: %d\nMilk: %d\nMacaron: %d"
    text = st % [Cookie, Milk, Macaron]

Just realized this is quite similar to @AndyCampbell’s answer above (I just got sidetracked for a bit before submitting my response). :slight_smile:

jgodfrey | 2023-02-26 23:22