0 votes

Hey, I'm trying to access sub-values in my dictionnaries using the name of the node trying to access those said values.

I'm storing a name on a Singleton when pressing a button like so :

func _on_Confirm_pressed():
GameData.characters["Player"]["Character_name"] = Character_name

Dictionnary looks like that :

var characters = {
"Player": {
    "Character_name": "",
    "Username": "",
}

}

And when the node is intanced, I try to access it like so :

func _ready():
print(GameData.characters[str(self.name)]["Character_name"])

I tried both with and without the str(), but I can't seem to make this work.

How can I access my dictionnary using the name of the node calling it ?

Godot version 3.3.3 stable
in Engine by (86 points)

1 Answer

0 votes

You need to add a key to your dictionary with the name of your player before accessing it. Also, using str() is unneeded since Node.name returns a string by default.

func _ready():
    GameData.characters[self.name] = {
        "Character_name": "",
        "Username": "" }
    print(GameData.characters[self.name]["Character_name"]) 
by (123 points)

So, doing this, I'll create an entirely new dictionnary within the singleton ?

That may cause an issue since I need to change the character name before instancing it (using a lobby, the player will use buttons to choose a character, and then it will get launched in the game)

Or am I not understanding something about dictionnaries ?

You will create a new dictionary, but it will go inside the GameData.characters dictionary, not directly inside the singleton. I'm sorry if I don't fully understand your question. Do you need the GameData.characters dictionary to only have the "Player" key in it?

Well, I'm trying to make a multiplayer game

So, I'm using a Lobby to let every player choose their characters.

I was thinking about using a dictionnary to store the name of the character each player used, and use that name to instance the correct character, with the correct sheet / animation and correct skills

So, I'll need the Gamedata.characters dictionnary to store data for "Player", but also "Player2", "Player3" and "Player4"

I have a lot more questions than this one, but it's not like I can use anything but here to ask about all the things I don't really understand.

I'm still a newbie at game development haha.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.