Is it possible to store different values on a single variable?

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

Greets!

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?

:bust_in_silhouette: Reply From: jgodfrey

You probably want a Dictionary. See here:

Example:

var item1 = {"weight": 50, "durability": 75, "protection": 100}

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

print(item1["weight"])
print(item1.weight)

jgodfrey | 2020-02-16 18:06

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.

Syl | 2020-02-16 18:18

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

Syl | 2020-02-16 18:21

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

jgodfrey | 2020-02-16 19:08

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}
}

print(items[$Sprite].power)

jgodfrey | 2020-02-16 19:13

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

Syl | 2020-02-16 19:31

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…

jgodfrey | 2020-02-16 19:33

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?

jgodfrey | 2020-02-16 19:36

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 {.

Syl | 2020-02-16 19:42

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…

jgodfrey | 2020-02-16 19:45

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

Syl | 2020-02-16 19:51

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

Syl | 2020-02-16 19:59

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.

Syl | 2020-02-16 20:32

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}
}	

jgodfrey | 2020-02-16 20:40

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

Inventory(node2D)
…ClothesArmor(node)
…PurClo(sprite)
…LeaBelPoc(prite)

Tried get_node(), get_child(), no avail.

Syl | 2020-02-16 21:18

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.

jgodfrey | 2020-02-16 21:58

Yes, the script is attached to inventory…

Syl | 2020-02-16 22:05

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)?

jgodfrey | 2020-02-16 22:12

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…

jgodfrey | 2020-02-16 22:55

That’s it, clear of errors. :smiley:

So many thxs for it all.

Syl | 2020-02-16 23:03

:bust_in_silhouette: Reply From: denxi

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:

print(big_boy_sword["damage"])
#This'll print 100

More information can be found here: Dictionary — Godot Engine (latest) documentation in English

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.

Syl | 2020-02-16 18:18