0 votes

I'm very new to Godot and I’m trying to understand how to efficiently use Dictionary to create an Inventory. I'm probably not even doing it right so please bear with me if my code is messy.
I'm trying to pick up an object from the scene store his ID then compare it to a list of ID in a Dictionary that is usd as a database to store Icon and text description for my items.
To put it simple I want to read through my Dictionnary to find a value equal to the value of the object I picked

Here's the function to pick an object

func _pickObject():
if Input.is_action_just_pressed("Interact"):
    if CanPick == true:
        StoringID = CollectibleObject.ID
        HasCollected = true

The problem is I cannot figure how to connect the Stored ID to the ID in the dictionary.
I am currently trying to do something like that

extends Node
var DicID = { "ObjectTest" : { 
    objectID = 1,
    icon = "res://icon.png",
    description = "This is a test object",

The problem is I can't figure how to connect the Stored ID to the ID in the dictionnary.
I'm currently trying to do something like that

func _ready():
menu = get_node("Inventaire/Panel")
ObjectToLoad = get_node("Database").DicID.keys()

func _AddingObject():
ObjectToAdd = get_node("Hero/Area2D").StoringID
var IDtoCompare = ObjectToAdd
for ObjectToLoad in get_node("Database").DicID:
        if ( ObjectToLoad == IDtoCompare)
                                    #DoStuff     #ERROR

That's where I have my problem, it only return an error telling that I can't compare my Dictionary value with an Int using ==.
I figured out I wasn't doing the right the thing but I have no idea how to compare the object my character picked up to the ones stored in my Dictionary.

Sorry if I expressed myself badly, I'm looking to any kind of help, tutorial or anything.
Thanks in advance !

in Engine by (12 points)

1 Answer

0 votes

There are quite a lot of ways to do it, because it is open ended how you choose to organize your data. If your item IDs are unique, I would recommend the most efficient method, create a dictionary where the keys are the integer ID numbers.

You can't assign integer IDs as keys with the default syntax though, because like variable the names can't start with numbers. You can get around this by doing the assignment during some initialization step.

extends Node2D

var dict = {} # Intialize empty dictionary

func _ready():

    # Setup dictionary with integer keys.  Brackets create/assign allow any type.
    dict[0] = { name = "thing1", power = 100 }
    dict[1] = { name = "thing2", power = 200 }

Then you can check that the keys are indeed integers using typeof and print statements.

# Verify key is integer.
for key in dict.keys():
    print(typeof(key) == TYPE_INT) # true / true

From this point forward to know if the dictionary contains an item ID, just use has().

# Do IDs exist?
var id = 1
print(dict.has(id)) # true

id = 2
print(dict.has(id)) # false

Going with the method you have, it is less optimal, but you can create a function that scans through the sub-dictionaries for IDs, assuming that nested key exists. Here is an example function of it, but recode it as it suits your project.

func _ready():

    var d = {

        thing1 = { id = 0, power = 100 },
        thing2 = { id = 1, power = 200 }

    print(has_id(d, 2)) # false

func has_id(dict, item_id):

    # Be sure sub dictionary values have a key called "id"

    for entry in dict.values():
        if(entry.id == item_id):
            return true

    return false

Lastly, the error you're getting is from comparing incompatible types. If one field is stored as an integer and somewhere else brought in as a string, you can type cast.

int(dict_id) == obj_id


dict_id == str(obj_id)

by (5,203 points)

Thank you very much for your detailled answer !

You're welcome. ^_^

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.