Invalid get index ''Item' (on base: 'String'). (conflicts with null to my mind)

Godot Version

Code:
func _ready():
for i in PlayerData.inv_data.keys():
var inv_slot = $InventoryBack.get_node(i)
print(PlayerData.inv_data)
if PlayerData.inv_data[str(i)][“Item”] != null: //error line
var item_name = GameData.item_data[str(PlayerData.inv_data[i][“Item”])][“ItemName”]
var icon_texture = load(“res://Assets/Items/ID” + str(PlayerData.inv_data[i][“Item”]) + “.png”)
inv_slot.get_node(“Icon”).set_texture(icon_texture)

Console:
//{ “Inv1”: { “Item”: “1” }, “Inv2”: { “Item”: }, “Inv3”: “0”, “Inv4”: “3”, “Inv5”: { “Item”: }, “Inv6”: { “Item”: }, “Inv7”: { “Item”: }, “Inv8”: { “Item”: } }

JSON:
{“Inv1”:{“Item”:“1”},“Inv2”:{“Item”:null},“Inv3”:“0”,“Inv4”:“3”,“Inv5”:{“Item”:null},“Inv6”:{“Item”:null},“Inv7”:{“Item”:null},“Inv8”:{“Item”:null}}

Question

Invalid get index '‘Item’ (on base: ‘String’).

Dont undertstand the error. Tried to fix with stringify, but it didnt help a lot.

in which line there is this error ?
this could be your Player.inv_data[index] value is a String type, instead of what you wanted it to be a Dictionary type

I edited

yes, so try add this before the error line

print(PlayerData.inv_data)

this should confirm whether or not your inv_data dictionary’s value is String type or Dictionary type

Wrote it. It repeats three times btw, so logic (for loop) is passing.

this what made the error
in inv3’s value there is no Dictionary key of Item

show how you organize the inv_data of the player

1 Like

Ah, I see the problem

yes, your issue is there are these inv values that has Dictionary with Item as key, some just pure string of “0”
the way you organize the inventory’s inv data may be wrong

if you intend to keep the pure string of “0” for inv3, then the condition for checking will need to change

This problem is created by my resaving function…
Im saving badly json file

TY so much! I fixed saving process and all worked!

1 Like

PlayerData.inv_data[target_inv_slot] = data[“origin_item_id”] → PlayerData.inv_data[target_inv_slot][“Item”] = data[“origin_item_id”]

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.