0 votes

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?

in Engine by (321 points)

2 Answers

0 votes
Best answer

You probably want a Dictionary. See here:

https://docs.godotengine.org/en/3.2/classes/class_dictionary.html

Example:

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

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.

0 votes

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: https://docs.godotengine.org/en/latest/classes/class_dictionary.html

by (440 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 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 webmaster@godotengine.org with your username.