Unable to fetch resources that are inside an array in another resource

Godot Version

4.3

Question

`
Hey everyone :slight_smile:,

I am currently trying to create a supermarket shelf, which contains a specific item.
E.g. Apple, Bread and so so on.

For that I created resources for each of the items.
To randomly select one of those resources to spawn on my shelf, I created another resource that holds an array with all of my item resources.

To test if I can fetch the contents of my Itemlist resource, I tried to print out them out.
This results in following output: Resource#-9223372007209433817

Below you can find the scripts for my resources aswell as the script for the shelf in which I am trying to get their contents.

My question is, how would I store my Items so that I can dynamically expand them and call them in every script that I need them in?

I hope that someone can assist me in solving my issue.
In case you need more of the code or more insights, just let me know.

Thanks in advance :slight_smile:

Items resource:

extends Resource

class_name Items

@export var itemname: String
@export var itemprice: float
@export var itemquantity: int
@export_file("*.obj") var itemobject

Itemlist resource:

extends Resource

class_name Itemlist

@export var itemlist: Array[Items] = []

Shelf script: (only the relevant snippet of the code)

class_name Interactable
extends StaticBody3D

@export var itemattributes : Itemlist

func _ready():
	print(itemattributes.itemlist)

`

You almost got it. Change

print(itemattributes.itemlist)

into

print(itemattributes.itemlist[0].itemname)

This will print the name of the first item in the itemlist

Alternatively:

for item in itemattributes.itemlist:
  print(item.itemname)
# prints:
# Apple
# Bread

I’m not sure what you mean by dynamically expand them? You mean the itemlist?
If you want to use the items in every script, there are multiple ways to do it. For example:

var myitemlist: ItemList = load(β€œres://resources/itemlist.tres”)

This loads the itemlist as a resource every time. This can be useful but it is preferred to load them only once.

Alternatively, you can use a singleton: Singletons (Autoload) β€” Godot Engine (stable) documentation in English
Create a singleton to load the itemlist and you will be able to access them from anywhere.

Hey @snipercup :slight_smile:
Thanks for your quick answer.

Shortly after posting here, I was able to solve my issue with the exact same approach you posted :smiley:

Thanks alot :slight_smile:

1 Like