Allowing Player to Enter Name

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

hello! i’m new to Godot (and programming in general) so i apologize if this is a simple thing, but i haven’t been able to find a clear answer on how to store a player-entered name so it can be used in dialogue or events throughout different scenes within the project.

i have a singleton in my project settings, and a scene with a ControlNode/Panel/VBox to contain a Line Edit and Button. the Line Edit sends a signal to my singleton.gd (attached to scene ControlNode):

var player_name
func _on_LineEdit_text_entered(new_text):
	player_name = new_text
	print(player_name)

the above does actually print whatever gets entered in the Line Edit, so it’s recognizing the text input. however, the Button to accept the entered name and move onto the first scene has this script attached:

func _on_Button_pressed():
	get_tree().change_scene("res://Scenes/TestWorld.tscn")
	print("Welcome " + str(singleton.player_name))

which only prints “Welcome Null”. i’ve tried different configurations with ‘get_node’ or ‘get_text’ and they always print Null, or leave it blank, or break from some type of error. i am new to all this, so maybe i’m misunderstanding how singletons work, or referencing the player_name incorrectly, but any help on this would be most appreciated!

tl;dr can a singleton be used to store the text entered into a LineEdit node (such as a player name)? if so, what’s the syntax to store and reference it?

:bust_in_silhouette: Reply From: Klagsam

In your scene you have var playernamewhich is the variable playername IN your scene. What you have to do is to declare another variable e.g. playername in your singleton and then in your lineEdit scene singleton.playername = newtext

Otherwise you just store the text locally in your lineedit scene, but not globally in your singleton

thank you so much, it’s working now!

axierratic | 2019-12-10 14:02