Godot Version 4.7.1
I’m making an inventory based of this https://www.youtube.com/watch?v=2ZauOcB_jBw&t=1109s tutorial, but I can’t add new property’s to my item class.
My resource file looks like this:
extends Resource
class_name Item
@export var name: String
enum ItemType { GENERIC, CONSUMABLE, QUEST, EQUIPMENT }
@export var type: ItemType
@export var inv_icon: Texture2D
@export var stack_amt: int
@export var item_path: String
@export var description: String
@export var value: int
When I started this file it only had name, type, icon and path, now I’d like to add things like description and value, but even thou the property’s are visible in the editor, I can’t access the new property’s in code, if i print my item I only get the initial first four values: { “name”: “Stick”, “inv_icon”: (res://inventory/items/item_icons/stick.png):<CompressedTexture2D#-9223371985264834770>, “item_path”: “res://inventory/items/item_scenes/stick.tscn”, “stack_amt”: 3 }
Do I need to reload them somehow?
Hi there!
Make sure the code you try to access really uses your new version of that resource. For example by following the reference to Item by control-clicking (control + left click) it in the code editor.
Also keep in mind that printing a class is not the same as accessing its members. Printing is defined by the _to_string() → String method which you might have never updated after extending your Resource. Maybe the members exist, but you are not testing them.
All my collectible items have this script attached, and I can see and edit all the fields in the editor:
extends StaticBody3D
@export var item_res: Item
func _ready() → void:
add_to_group(“interactable”)
I tried accessing the members directly but I get this error:
Invalid access to property or key ‘description’ on a base object of type ‘Dictionary’.
I don’t get the same error if I try accessing any of the original members, just the new ones.
That hints at the variable you are trying to access being of type Dictionary, but the description member was defined on the type Item which extends Resource. Can you show the code that causes this error?
extends Node2D
@onready var item_icon = $ItemIcon
@onready var item_count_label = $ItemCountLabel
@onready var inspector_label = $/root/World/Player/CanvasLayer/PlayerUI/MainPanel/InspectorPanel/InspectorLabel
var slot_num: Vector2i
var item: Dictionary
var item_count = 0
var item_txt = ""
func add_item(new_item):
if (item_count != 0 and (item['name'] == new_item['name']) and item_count < item['stack_amt']) or item == {}:
item_count += 1
item = new_item
item_icon.texture = item["inv_icon"]
#item_txt = item[‘description'] # This doesn't work
item_txt = item['name'] # but this does work ???
print(item_txt)
refresh_label()
return true
return false
The print is just me trying to understand why this doesn’t work.
If I use “description” it crashes, but “name” works fine
What about item.description? Does that work?
Why is your item a dictionary? I thought you were trying to access Item?
var item: Dictionary
This would try to access Item:
var item: Item
Using item.description also crashes the program with the same error.
The reason I’m using a dict is to transfer item-info before destroying(picking up) the item.
All that is not necessary. Implement the function like so:
# Add the explicit type you expect to get feedback early if you put the right thing into add_item()
func add_item(item : Item):
print(item)
You can also add an explicit to_string function to the Item resource to see the state of your Item:
func _to_string() -> String:
var NL: String = '\n'
var result: String = '=== ITEM ===' + NL + \
' description: %s' % description + NL + \
' stack amt: %d' % stack_amt + NL + \ # add more variables you want to debug.
' item path: %s' % item_path
Perhaps not, but I still don’t understand why my approach doesn’t work?
Picking up an item in my game is removing that item from the game and spawning an icon in my inventory, I need some way to keep that information for placing/staking in my inventory.
Sorry… my bad 
I forgot to convert that part in my item-prep (in player-script):
func prep_item(new_item):
var item = {}
item[‘name’] = new_item.item_res.name
item[‘inv_icon’] = new_item.item_res.inv_icon
item[‘item_path’] = new_item.item_res.item_path
item[‘stack_amt’] = new_item.item_res.stack_amt
item[‘description’] = new_item.item_res.description
return item
But thanks for guiding me along, learned something new 