I am not incredibly experienced with Godot or programming in general, however I am not brand new and have been using Godot for about the past year on and off.
I am having an issue where I have created a resource that contains an array of items, but when I try and get an index for them, I get the error “Only ‘String’ or ‘StringName’ can be used as index for type ‘Inventory’, but received ‘int’”. This isn’t really a bug issue, more just about me not really getting how arrays work with resources.
I saw that there was a post here with a similar error, but I couldn’t really make heads or tails of what fixed the issue and how I could apply it to my code, so I’m hoping someone can give me a little bit more of a digestible answer about how stupid I’m being.
This is the script that is causing the errors, specifically the lines that have Playerstats.items[number]:
func item_stuff():
if sub_menu == "item":
if item_page == 1:
item_1.text = Playerstats.items[0]
if Playerstats.items.size() > 1:
item_2.text = Playerstats.items[1]
if Playerstats.items.size() > 2:
item_3.text = Playerstats.items[2]
if Playerstats.items.size() > 3:
item_4.text = Playerstats.items[3]
if item_page == 2:
if Playerstats.items.size() > 4:
item_1.text = Playerstats.items[4]
if Playerstats.items.size() > 5:
item_2.text = Playerstats.items[5]
if Playerstats.items.size() > 6:
item_3.text = Playerstats.items[6]
if Playerstats.items.size() > 7:
item_4.text = Playerstats.items[7]
This is the script for the Inventory resource:
extends Resource
class_name Inventory
@export var Items: Array[Item]
This is the global that has the items variable
extends Node
var max_hp: int = 20
var current_hp: int = 20
var level: int = 1
var experience: int = 0
var items: Inventory
This is what is in one of the resources I am trying to get
Sorry turns out that’s not actually the solution, oops
Your fix just got rid of the error in the editor, but when the game runs and the game tries to get anything from the Items array the game crashes and gives this error:
“Invalid access to property or key ‘Items’ on a base object of type ‘Nil’.”
my fault for not running the game and just assuming it worked because the error in the editor went away
I don’t have anywhere in the scene that assigns anything to the inventory resource, and the earliest occasion I try and access it is pretty much at the lines where the error was before
This is a screenshot of node that assigns things to the Inventory resource I think, it wasn’t in the scene where I am trying to access it through the player stats global script but after adding it nothing changes:
This is just an empty variable (with a type hint), but it doesn’t get any resource object here. You need to assign a resource object to the variable somewhere: Either export the variable and do it in the editor (like it seems to be the case for your player) or somewhere in the code.
Autoloads can either be scenes or scripts. If it’s a scene you can export the variable and set it there, if it’s just a script you would have to do it via code I guess.