Array of custom resources

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By george12teodor

I have a custom resource called Item.How can i make an array of these items that i can edit in the inspector as well as through script?

:bust_in_silhouette: Reply From: AndyCampbell

I think if you are using GDscript, you will miss the ability to filter the type of resources in the inspector so I do not think you can say “only show Item resources”, however you can at least make the structure you need work in the editor…

I assume you have your Item resource class defined in a .gd script, and I assume you have saved some instances of the resource (.tres extension) . I am assuming your resources are working correctly so you can open a resource in the editor and see it’s properties in the Inspector panel.

In the scene that will hold the array, ensure you have the tool command as the first line of the script, then export an Array of Resources something like this

tool
extends Node # or whatever your scene extends 

export(Array, Resource) var test_array

When you have saved that and then re-select the scene in the editor (you may need to reload it), the tool command should cause the editor to display the test_array in the Inspector panel.

You can then set the size of the array which add rows in the inspector. For each row, you can then drag and drop your resource tres files from the FileSystem view in the editor into the “[empty]” resource field for each item on the array. When you click on a resource in the array the Inspector should show a panel with the properties of the resource, which you can edit.

It would be great if you could automatically set the array to expect each element to be an Item resource, but I am not aware of a way to do that in GDscript. If there is a way, hopefully somebody else can join in.

Since test_array just a regular Array, you can manage that in your scripts, like any other another array