0 votes

I have one dictionary for inventory items:

print(DataImport.inven_data)

{101:[ShieldBeastmaster, 1], 102:[HelmetFootman, 1], 103:[Shield, 1], 104:[HelmetBeastmaster, 1], 105:[ShieldBeastmaster, 1], 106:[Shield, 1], 201:[Mace, 1], ....other items.........., 209:[Bow, 1], ..other items...... 501:[IronIngot, 1], 502:[IronIngot, 1], .........other items............, Gold:719, HealthPack:5 }

And other dictionary for true false booleans checking it item is in the dictionary:

print(SaveData.wearables_dic)

{HelmetBeastmaster:True, HelmetFootman:True, HelmetKnight:False, Shield:True, ShieldBeastmaster:True, ShieldFootman:False, ShieldKnight:False}

I am updating true false dictionary through the series of if checks on new loot. I know it can probably be done much more efficient but I am new to Godot so I like to have code that I am able to understand. The problem arises when I delete item from the dictionary where I tried with:

if InvenData["101"][0] != "Shield" and InvenData["102"][0] != "Shield" and InvenData["103"][0] != "Shield":
    SaveData.wearables_dic["Shield"] = false

And got: Invalid get index '103' (on base: 'Dictionary'), as 103 is at that moment already deleted from the dictionary.

Ok, so I tried with:

if InvenData.has(["103"][0]):
    if InvenData["101"][0] != "Shield" and InvenData["102"][0] != "Shield" and InvenData["103"][0] != "Shield"":
    SaveData.wearables_dic["Shield"] = false

Error is now handled, but true false boolean still does not get updated, because that item just got deleted, so it never runs through the nested if. Any idea how to handle this ? Any help appreciated.

in Engine by (44 points)

turned comment into the answer below

1 Answer

+1 vote
Best answer

I know it can probably be done much more efficient but I am new to Godot so I like to have code that I am able to understand.

Code you can understand >>>>>>>>>>> efficient code. Regardless of whether you are new or not! Only if you start having noticeable frame drops should you start to worry about efficiency.

Try replacing your last snippet of code with this:

var has_shield = false
for key in ["101", "102", "103"]:
    if InvenData.has(key) and InvenData[key][0] == "Shield":
        has_shield = true
SaveData.wearables_dic["Shield"] = has_shield 
by (1,086 points)
selected by

Thank you a lot.

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.