0 votes

Hello,

I have an external dictionary imported as a singleton DataImport.inven_data with the following contents:

print(DataImport.inven_data)
{101:[LeatherBoots, 1], 202:[Sword, 1]}

101 and 202 are keys and [LeatherBoots, 1] [Sword, 1] are values with the item name and item quantity. I want to get a true false boolean if a value Sword is present in a dictionary and have problems setting this up.

Any help appreciated.

in Engine by (44 points)

4 Answers

+1 vote
Best answer

If you want to look something up in a dictionary, you should use that as key, instead of a number like 202 or 101. If you know sword is always going to be 202, you could ask

if 202 in DataImport.inven_data:
    print(DataImport.inven_data[202]) #replace this by your actual code

That will print [Sword, 1]
If you really need to have the key 202, but need to look if some of the values is Sword, it dependes what is sword... if you have on the sword script something like class_name Sword maybe you could do a for loop like this:

for item in DataImport.inven_data:
   if DataImport.inven_data[item][0] == "Sword":
        print(dict[item]) #replace this by your actual code
        break

That would also only print [Sword, 1]
Is something like that what you are looking for?

by (3,388 points)
selected by

201 -210 are keys that get populated randomly by the order of how the player picks up random loot.

So 202 will contain different things all the time. Should have mentioned this now that I think about it.

I think I will try setting up if statements checking if any item from 201 - 210 has combo "key number, Sword", but that will be a lot of text.

And the loop i wrote in the answer doesnt work for you?

I get Invalid operands 'String' and 'Array' in operator '=='.

I tried with:

for i in DataImport.inven_data.values():
    if DataImport.inven_data[i][0] == [Sword]:
        print(i)

which returns first item in the dictionary which is not what I am looking for. The code below returns the values of the dictionary, I just cant figure it out how to approach single line returning boolean if the item value is there so I can do some further code.

for i in DataImport.inven_data.values():
    print(i)

Sorry, that was why i asked if you had a class_name Sword or something
What is sword? an array?

Sword and other loot dictionary & inventory items are defined within json and most don't have their own class, but some do. Sorry, this is the best as I can explain myself as this is my first godot project.

+1 vote

Hi,
this does not seem a very efficent setup (at least when the keys have no other use). You have to step through the dict and search a match.

func search( what):
   for item in dict:
      if item[0] == what:
         return item
by (3,922 points)

I think item in dict will give you the key, not the value, so item[0] wont contain Sword.. at least i tried that and that was the result.

0 votes

I would use item name as a property name if it's unique. Your records will look like this:

inven_data = {
    "LeatherBoots": {
        "num": 1,                 # number of items
        "inven_loc": 101},   # id of inventory place
    "Sword": {
        "num": 2,
        "inven_loc": 202}
}

and check for item with simple if inven_data.has("Sword")

or an Array for inventory where each member is a Dictionary:

inven_data = []
inven_data.resize(INVENTORY_SIZE)
inven_data[inv_index] = {"name": "Sword", "quantity": 1}

looking for records a bit more complicated then:

for each in inven_data:
    if !each.empty():   # check if inventory place has any item
        if each.name == "Sword":
            return true
by (126 points)
+1 vote
if value in dict.values():
   ...

Dictionary.values()
Array.has()

by (20 points)
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.