0 votes

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/Inventory.gd" 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")


Godot version 3.4
in Engine by (73 points)
edited by

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.