Created line edits in code and need to find a way to reference the text that the user will type in them

Godot Version

4.0

Question

for a multiplayer game I have basically a form that asks for how many players and their names. once the number of players is given that number of LineEdit Nodes is created to type the names of the players. Once submitted, how do i store those names to use later? its stored in what i think is a list but the output is printing the wrong thing. It prints the name of the root node instead of the names of the players

node set up:
PlayerSelect (control)
-vboxcontainer
–richLabelText
–numPlayersInput (LineEdit)
–confirmNumButton (Button)
–NameContainer (vboxContainer)
–startButton (Button)

extends Control

@onready var num_players_input = $VBoxContainer/NumPlayersInput
@onready var confirm_num_button =$VBoxContainer/ConfirmNumButton
@onready var name_container = $VBoxContainer/NameContainer
@onready var start_button = $VBoxContainer/StartButton

var players = []
var current_player_index = 0


func _on_confirm_num_button_pressed():
	var names = name_container.get_children()
	for c in names:
		c.queue_free()
	var num_players = int(num_players_input.text)
	
	if num_players <= 0 || num_players >= 9:
		num_players_input.clear()
		return
	for i in range(num_players):
		var name_input = LineEdit.new()
		name_input.placeholder_text = "player %d name" %(i+1)
		name_container.add_child(name_input)
		
	num_players_input.clear()


func _on_start_button_pressed():
	players.clear()
	for child in name_container.get_children():
		if child is LineEdit:
			var name = child.text.strip_edges()
		if name != "":
			players.append({ "name": name, "score": 0, "completed": [] })

	if players.size() > 0:
		print("Players:", players)
		hide()

Seems like your unindent is causing an error, might also be good practice to avoid shadowing variables, I think there’s a warning for shadowing enabled by default.

1 Like