Hi, i am trying to make and inventory system, i get the item data from a Dictionary composed of other Dictionaries, like this:
var item_list : Dictionary = {
"0": {"name": "sword", "attack": 2, "defense": 0},
"1": {"name": "shield", "attack": 0, "defense": 1}
}

Then i need to pass those values to the item instance, like this:
var item_id : String = "0"
var item_info : Dictionary = item_list[item_id]

This isn't working, i get the error: Invalid access to property or key '0' on a base object of type 'Dictionary'

When i print(item_list[item_id]) it works fine, i just can't define the value of item_info

  • xyz replied to this.
  • Hey, @DaveTheCoder and @xyz thanks for your time, i was checking the indentation and the variable types to make sure it wasnt that and found a solution, in my original code i was declaring item_id and item_info before func _ready. Now i tried declaring item_info inside func _ready and it worked!

    i failed to mention that item_list is declared in the ready function of a global script, but item_id and item_info are declared in the item script before func ready --' my bad. i will make sure in future questions in the forum i post the full script.

    `
    class_name Item extends Node2D

    @export var item_id : String = "0"

    func _ready() -> void:
    var item_info : Dictionary = Data.item_list[item_id]
    print(item_info)
    `

    I don't see anything wrong with the code.

    I just tested it in Godot 4.5-dev2, and it ran without errors. I printed the values of all three variables, and they were correct.

    Is the indentation in your post accurate? Do you get any other errors or warnings?

    JVStorck Are you sure that zero is indeed a string and not and integer in your original code?

    Hey, @DaveTheCoder and @xyz thanks for your time, i was checking the indentation and the variable types to make sure it wasnt that and found a solution, in my original code i was declaring item_id and item_info before func _ready. Now i tried declaring item_info inside func _ready and it worked!

    i failed to mention that item_list is declared in the ready function of a global script, but item_id and item_info are declared in the item script before func ready --' my bad. i will make sure in future questions in the forum i post the full script.

    `
    class_name Item extends Node2D

    @export var item_id : String = "0"

    func _ready() -> void:
    var item_info : Dictionary = Data.item_list[item_id]
    print(item_info)
    `

    @JVStorck actually it's great to present a simplified version of your problem, it makes things easier for everyone and you're more likely to get an answer.

    However it's a good idea to try the simplified version yourself first, just in case it behaves differently!