How do I save a character name input from a LineEdit node with a Button node as a variable and place that variable in my Globals script?

Godot Version 4.2

How do I save a character name input from a LineEdit node with a Button node as a variable and place that variable in my Globals script?

Hello! This question has a few parts, so I’ll break them down.

I have a naming scene that gets loaded into my main scene when the player presses the “new character” button. The naming scene has a line edit node where the player can type their character’s name, and a button below that to confirm the name. I don’t want the line edit to emit a signal for when enter is pressed, because I want this to be a mobile game, hence the confirm button. So right now I have this script for when the line edit is simply changed emitting to the name scene:

func _on_name_type_text_changed(new_text):
charname = new_text
print(new_text)

which works fine.

But what I can’t figure out is how to then save the charname variable when the confirm button is pressed, since that’s a whole different signal also in the naming scene, and then from there how to get the charname variable into my globals script so it can be saved and referenced for the rest of the game.

Any thoughts would be really appreciated!

1 Like

Perhaps your LineEdit node needs a reference to the player, such as @export var player: Player = null, and then in the _on_name_type_text_changed method, you could set player.xxx = new_text.

Wait a moment, you have a global script? Why not simply Global.xxx = charname?

1 Like

Sorry, could you explain this more? I’m not sure what I’d be adding Global. onto

For instance, let’s say you implemented a singleton that extends Control:

extends Control

var player_name: String = ""

# Assuming this singleton is named 'Global'

And you have a function that updates whenever the player types their name:

func _on_name_type_text_changed(new_text):
Global. player_name = new_text

Oh my god this was so simple. THANK YOU so much.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.