To store a variable in my singleton.

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


My edit_line buton receive a text(name) that I’d like to store in my ‘playerstats’ singleton. What’s the syntax please?

:bust_in_silhouette: Reply From: Zylann


func on_receive_text(name):
    TheNameOfYourSingleton.player_name = name

Thxs, look what I wrote for my script:

func on_receive_text(new_text):
				Playerstats.player_name = new_text

With my Playerstats singleton script:

    extends Node

var player_name = null

There are no snags on first view, but how do I check it?
I tried to print player_name on my next scene, with that script:

func ready():

but it’s not found…

Syl | 2018-04-08 21:35

This gets the node but you don’t do anything with it, so that line does nothing.

This prints “player_name”. Not the value of player_name.

To print the value of your player name (or get it for use as a normal variable), write this:

Zylann | 2018-04-08 21:38

So Playerstats.player_name = new_textfind directly my singleton?

And nothing is printed still…

Syl | 2018-04-08 21:56

What is your code and what do you want it to do? It seems you have a lot of confusion about how access to node variables works…

Zylann | 2018-04-08 21:58

Yes, as I’m making my first steps inthere. Here’s the scene: Imgur: The magic of the Internet

As said, I want to store the entered name in my Playerstats singleton, wich is almost empty, as I’ve just created it. Only that line in it: var player_name = null

My Edit_line buton got that code:

extends LineEdit

func on_receive_text(new_text):
   Playerstats.player_name = new_text

func _on_AnimationPlayer2_animation_finished(anim_name):

And on the next scene(Soul), I try to check the storage of my name with this script:

extends Node2D

func ready():

Syl | 2018-04-08 22:12

So if you follow the flow you just wrote, player_name will be empty if on_receive_text wasn’t called before you swich to the Soul scene.

What I understood in the first post is that you have a scene in which the player enters a name, which is then stored in a singleton. So did you connect a signal from the LineEdit to your on_receive_text function? (it’s not automatic, I assumed you had this working already)

Zylann | 2018-04-08 22:21

There’s no on receive_text signal, so I signaled it now in my animation script with:

func _on_LineEdit_text_entered(new_text):
	 Playerstats.player_name = new_text and play("change")

But there’s nothing printed still…

Syl | 2018-04-08 22:43

FYI, you don’t have to call the function _on_LineEdit_text_entered, you can connect text_entered to the function you want (for example, on_text_received), as long as the amount of parameters is the same. But using the default function name like you did is a bit easier to read though.

What you wrote is wrong. The and operator takes two boolean and returns wether both are true or not. new_text is not a boolean, it’s a string. I don’t know what play returns, probably just null. Anyways, that’s not what you want to store in player_name.

You should simply write:

func _on_LineEdit_text_entered(new_text):
    Playerstats.player_name = new_text

Zylann | 2018-04-09 00:47

Ok, thxs. But still nothing printed. Here’s my script on the next scene for that:

extends Node2D

func ready():

Right? If quite empty…

Syl | 2018-04-09 01:14

What is printing if you print(new_text) in _on_LineEdit_text_entered?

Zylann | 2018-04-09 01:16

Yes that print the name entered.

Syl | 2018-04-09 01:26

If you do print("hello") in the _ready of the next scene, does it prints?
I’d like to see the project though, there is something I’m missing here.

Zylann | 2018-04-09 01:27

No that doesn’t print hello. You saw my link to the screenshot of my scene ‘Name’?

Syl | 2018-04-09 01:48

If it doesn’t print hello, that means the script where you print it is not in the next scene. _ready doesn’t run, basically.
I cannot guess more than this without seeing how the project is setup, I can’t see enough in the screenshot.

Zylann | 2018-04-09 01:54

Yet that’s the script of the parent Node2D of the next scene…
Wanna check the scene and project? It’s here, with Name and Soul scene: DL.FREE.FR

Already so heavy, can’t believe it…

Syl | 2018-04-09 02:26

Aaah you wrote ready instead of _ready, that’s why it doesn’t print…

Also your project is heavy because all sounds are WAV despite being several minutes long each. I suggest you use OGG for those that are longer than 30 seconds.

Zylann | 2018-04-09 12:49


big trouble… I can’t edit the project anymore. See my question about the big bug… My backups don’t work any better… You don’t have problems when editing it?

Syl | 2018-04-09 17:22

Ok, got a backup…but that’s weird…

Syl | 2018-04-09 18:17

I didn’t have any problems editing it.

Zylann | 2018-04-09 18:30

Ok, the weirdest, is that if I change the name in my document folder, from Onyris11 to Onyris I got the bug, otherwise, it’s fine… Crazy nops?

Syl | 2018-04-09 18:42

Hey! That works! Thanks so much for your help! :slight_smile:

Syl | 2018-04-09 23:21