I have 2 labels on each VBox, but when I update them, only the first one does update, overwriting himself each time

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

Hello, so that’s my problem. The idea of the code is when in the scene “Send data” the user sends the name, points that he has won and if he has won or lost the game in general. But when I pass that data to the other scene (with a global variable). It keeps overwriting the first one, and skipping the second label. This is my code:

func _ready():


func volverAlMenu():
escenaMenu = “res://Scenes/Menu.tscn”

func rellenarPuntuaciones():
if $Fondomenu/ColorRect/VBoxNicknames/Nickname.text.empty():
$Fondomenu/ColorRect/VBoxNicknames/Nickname.text = Player.nickname
$Fondomenu/ColorRect/VBoxPoints/Points.text = str(Player.points)
$Fondomenu/ColorRect/VBoxResult/Result.text = Player.result

elif nickname2Taken == $Fondomenu/ColorRect/VBoxNicknames/Nickname2.text.empty():
$Fondomenu/ColorRect/VBoxNicknames/Nickname2.text = Player.nickname
$Fondomenu/ColorRect/VBoxPoints/Points2.text = str(Player.points)
$Fondomenu/ColorRect/VBoxResult/Result2.text = Player.result

Hmmm… Different username, but seemingly the exact same question (and code) as this from yesterday…

Writing in multiple labels without overwriting the first one - Archive - Godot Forum

In the future, rather than opening a repeat question (and ignoring any input in the original), just continue the conversation in the original ticket. You can edit and comment in that ticket as necessary - keeping all related information in a single place…

jgodfrey | 2023-05-19 13:46

Yes, I know, he is my partner. We are doing a small final project on our class. It’s a videogame, sorry for the misunderstanding

xiaojian | 2023-05-19 14:17

As noted in the other instance of this question, it’s unclear how you intend the above code to interact with the global nickname storage. The code in question appears to (still) be called from _ready(), meaning that it’ll only execute once, when the node containing the script is added to the scene tree.

Without more information (or access to the project itself), it’s difficult to provide more input.

jgodfrey | 2023-05-19 16:54

:bust_in_silhouette: Reply From: Tom Mertz

It is a bit hard to tell what you are trying to do from the code. I’m unclear on the flow, for example, how would the second label ever get filled, when is that condition?

It seems like if you’re running this code on _ready, but that will run once on scene load and if both labels load up blank initially, then the first label will only ever get filled.

What might be easier is instead of checking if either label is already filled, store the values for each label in your global variable and use those to check/fill, then you control the labels by what’s in your global store (if you need to remove one, update the state in your global variable so there’s only a player1).

func rellenarPuntuaciones():
	var player1 = global_variable.player1;
	var player2 = global_variable.player2;
	if player1:
		$Fondomenu/ColorRect/VBoxNicknames/Nickname.text = player1.nickname
		$Fondomenu/ColorRect/VBoxPoints/Points.text = str(player1.points)
		$Fondomenu/ColorRect/VBoxResult/Result.text = player1.result
	if player2:
		$Fondomenu/ColorRect/VBoxNicknames/Nickname2.text = player2.nickname
		$Fondomenu/ColorRect/VBoxPoints/Points2.text = str(player2.points)
		$Fondomenu/ColorRect/VBoxResult/Result2.text = player2.result

that said this is just guessing at your use case. Any more info about how your game flows would be helpful. Good luck!

P.S. If you select your code and hit tab in your GDScript editor then copy and paste it in your answer it will auto format in Godot Answers.