How to load a resource on runtime without referencing the filepath?

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

I’ve followed a youtube tutorial to create various items for a game as *.tres - files.
The directory containing the files gets scanned in order to create a global Item Database, which is then used to import items from the database into the actual inventory.

It works, as does loading and saving the inventory, but how can I attach one of these resources to the player character DURING runtime?

This is what my saved inventory looks like:

[gd_resource type="Resource" load_steps=5 format=2]

[ext_resource path="res://src/InventorySystem/" type="Script" id=1]
[ext_resource path="res://src/InventorySystem/Items/HealthPotionLarge.tres" type="Resource" id=2]
[ext_resource path="res://src/InventorySystem/Items/HealthPotionSmall.tres" type="Resource" id=3]
[ext_resource path="res://src/InventorySystem/Items/Blades/Weak Blades/Decrepit Blade.tres" type="Resource" id=4]

script = ExtResource( 1 )
items = [ ExtResource( 3 ), ExtResource( 2 ), ExtResource( 4 ) ]
item_list = {
"Decrepit Blade": 1,
"Health Potion Large": 5,
"Health Potion Small": 74

My player character has an export resource variable, which can be used to drag a weapon resource file into it. However, I want to be able to attach the “Decrepit Blade” from the inventory to the player during runtime. I can access the variables contained in the health potions, but it is not possible to load() the “Decrepit Blade” without the filepath. Is there a way to “get” the resource path from this file, or any other way to achieve this?


So, I figured out that once I get a specific item from the array and assign it to a new variable, I can attach the corresponding resource file to a node like this:

var item_to_use = Inventory.find_item("Decrepit Blade")