How can i access item via script?

Godot Version

4.3

Question

I have an inventory, there are a few slots inside it, and inside every slot an item can be assigned. For example when i click on item, it adds correctly.

	var itemslots = slots.filter(func(slot): return slot.item == item)
	if !itemslots.is_empty():
		pass
	else:
		var emptyslots = slots.filter(func(slot): return slot.item == null)
		if !emptyslots.is_empty():
			emptyslots[0].item = item
	update.emit()

But when i want to access it i don’t know how to. I need that for multiple things (Drag and Drop system, puzzles and interactions)
Items are *.tres files, Inventory is Array, also *.tres. InvSlot is like a holder for InvItem, you assign an item to slot ant the slot to the inventory.
So my question is how to access it? Also it would be great if anyone helped me with drag’n’drop, not only to place an items, but to remove them as well. Thx!

You can find some tutorials on youtube about it: https://www.youtube.com/results?search_query=godot+drag+drop+inventory

Your post raises some questions due to a lack of information. What do you mean when you say you want to access it? From where? How is your scene layed out? The inventory can work fine with signals. You have the inventory emit signals when an item is added or removed and have it respond to signals like an item pickup.

If you want the player to interact with the inventory, you’d probably need to have the InvSlot respond to user input.

1 Like

let me take a wild stab in the dark op has a script that extends resource that has a

extends Resource
class_name ItemsResource

@export
var items: Array[ItemClass]

they have made a few resource files where they have hand entered many items in.

now they have another script they want to drag and drop those saved resource files with their inventory items by doing the drag and drop.

something like
extends Node
class_name ItemHandler

@export
var WeaponItems : ItemsResource
@export
var ArmorItems : ItemsResource

func IsWeapon(Weapon:ItemClass)->bool:
for i:ItemClass in WeaponItems.items:
if i == Weapon:
return true
return false

1 Like

InvItem:

extends Resource
class_name InvItem

@export var name: String = ""
@export var texture: Texture2D

Inventory:

extends Resource
class_name Inv

signal update

@export var slots: Array[InvSlot]

func insert(item: InvItem):
	var itemslots = slots.filter(func(slot): return slot.item == item)
	if !itemslots.is_empty():
		pass
	else:
		var emptyslots = slots.filter(func(slot): return slot.item == null)
		if !emptyslots.is_empty():
			emptyslots[0].item = item
	update.emit()

InvSlot:

extends Resource
class_name InvSlot

@export var item: InvItem

And in the character script there is that line:

@export var inv: Inv

So basically what i mean by “Access it” is to know how to call an InvItem itself and its properties to change in which slot that item is for example

That is what layout for inventory looks like:
image

For inventory container there is that code:

extends Area2D
@onready var inv: Inv = preload("res://inventory/storage/item_inv.tres")
@onready var slots: Array = $ScrollContainer/HBoxContainer.get_children()

func _ready() -> void:
	inv.update.connect(update_slots)
	update_slots()

func update_slots():
	for i in range(min(inv.slots.size(), slots.size())):
		slots[i].update(inv.slots[i])

so in Inv class

func FindIndexInvItemByName(InvItemName: String)->int:
	var index:int=0
	for IS:InvSlot in slots:
		if IS.item.name == InvItemName:
			return index
		index+=1
	return -1

and

func AccessChangeName	(
		CurrentNameOfInvItem:String,
		NewName:String,
						)->bool:
	var index:int=FindIndexInvItemByName(CurrentNameOfInvItem)
	if index==-1:
		return false
	slots[index].name=NewName
	return true

would something like this work.

1 Like

That’s exactly what i needed! Thx a lot

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.