This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote


I'd like to store the informations of my items in autoloaded inventory scene, and so my items will have multiple stats like weight, durability, protection... Added to a sprite to display them.

How could i store all that information on a single var for each item?

in Engine by (326 points)

2 Answers

+1 vote
Best answer

You probably want a Dictionary. See here:


var item1 = {"weight": 50, "durability": 75, "protection": 100}
by (22,704 points)
selected by

You can reference the individual elements via either of the below:


Thxs but how do you get access to the coresponding sprite of each items? No way to include the path in the dictionary, right?

And i cannot write the brace bracket '{' in godot editor with my usual ctrl+alt+4, what's wrong please? Nothing is typed.

Eh? How could i coment to two diferent answers in the thread?

Dictionaries are fairly flexible. For example, you could include the sprite's path inside the dictionary as a property. Or, you could include it as the key in a dictionary of dictionaries.

The first would look something like this:

var item1 = {"path": $Sprite.get_path(), "weight": 50, "durability": 75, "protection": 100}

The second would look something like this:

var items = {
   $Sprite.get_path() : {"weight": 50, "durability": 75, protection": 100},
   $Sprite2.get_path() : {"weight": 40, "durability": 55, protection": 80}

For that second item, you could retrieve the info for a give sprite via:

items[$Sprite.get_path()].weight  # for example

Or, depending on your needs, you could use the Sprite itself as the key to a dictionary of values (instead of the sprite's path). That'd look like this:

var items = {
    $Sprite: {"power": 1, "health": 2},
    $Sprite2: {"power": 3, "health": 5}


Thxs, but how could i write that '{', what wrong with my keyboard and godot ?

No idea on the keyboard issue... Can you type a { in other applications, like NotePad? If so, maybe restart Godot? If not, I'd guess you have a keyboard problem...

I notice you mention Ctrl+Alt+4 for the open-brace character. Why would you do that? Your keyboard should have a key for both brace characters I assume, right?

No problem typing { with notepad, only in godot, and restart doesn't change anything... What you mean both brace character? I got one key, with 4, ' and {.

Hmmm... Not sure then. I assume this is a non-US keyboard? On a typical US-keyboard the brace characters ({ and }) are on the same keys with the bracket characters ([ and ]).

Guess you could always cut/paste it into Godot, but that's far from ideal...

It could be linked to my shortcuts, since ctrl+alt make an angle with green and red x/y appears... But dunno wich shortcut...

Ok, managed it with ctrl alt on the right... To test those dictionaries now.

Ok, first blocking:

var items = {
    $ClothesArmor/PurClo: {"weight": 3, "durability": 2}, {"protection": 1},
    $ClothesArmor/LeaBelPoc: {"weight": 0.5, "durability": 1 

':' is expected on second line. Parser error.

Ummmm... That has several syntax errors... If you follow the example, it should work. Here's your code, adjusted to be syntactically correct...

var items = {
    $ClothesArmor/PurClo: {"weight": 3, "durability": 2, "protection": 1},
    $ClothesArmor/LeaBelPoc: {"weight": 0.5, "durability": 1 , "protection": 2}

Sry, the items nodes aren't found. That's my tree:


Tried getnode(), getchild(), no avail.

What node is the script attached to? If it's on the Inventory node, those references look OK to me. If it's somewhere else, they're probably wrong.

Yes, the script is attached to inventory...

I don't think those node references are the problem, but it's really difficult to determine what the problem is. Can you provide a screenshot that includes the scene tree, the error, and the script (showing the line with the error)?

OK, so I think you're issue is that you're trying to reference those nodes before they've been added to the scene tree (during scene load).

Change this:

var items = {
    $ClothesArmor/PurClo: {"weight": 3, "durability": 2, "protection": 1},
    $ClothesArmor/LeaBelPoc: {"weight": 0.5, "durability": 1 , "protection": 2}

to this:

onready var items = {
    $ClothesArmor/PurClo: {"weight": 3, "durability": 2, "protection": 1},
    $ClothesArmor/LeaBelPoc: {"weight": 0.5, "durability": 1 , "protection": 2}

Note, the only differences is the addition of the onready at the beginning. That'll cause that code to not execute unit all of the nodes have been added to the scene (when the scene loads). I think that'll fix the reference problem...

That's it, clear of errors. :D

So many thxs for it all.

+1 vote

Yep! You can use either a dictionary or array. Dictionaries are easier for stuff like what you want to do, as you can assign a key to each piece of information and access it via said key.

For example, a dictionary can look like:

var big_boy_sword = {
    "name" : "Big Boy Sword",
    "weight" : 10,
    "durability" : 50,
    "damage" : 100

And you can reference the information like:

#This'll print 100

More information can be found here:

by (449 points)

Thxs, but how do you get access to the coresponding sprite of each items? No way to include the path in the dictionary, right?

And i cannot write the brace bracket '{' in godot editor with my usual ctrl+alt+4, what's wrong please? Nothing is typed.

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.