Referencing multiple nodes at once

Godot Version

4.5.1

Question

So I’m currently making an inventory system, but the problem is that for my items, I simply duplicate the items in the editor (Creating Item and Item2) but the problem with this is that my code specifically references just Item, which means that Item2 is completely unaffected. My other problem is that when the mouse enters the item, a variable is set (Shown below) but because both items have the same script, if the mouse is in Item2, Item will be teleported. Help?

Inventory Slot code (Sorry for bad formatting, I dunno how to do it with the colors)

class_name InventorySlot extends Area2D
var is_in = 0
var mouse_is_in = 0
@export var Itemz: Array[Node] = [$"../Item", $"../Item2"]
@export var Item_textures: Array[Node] = [$"../Item2/item_base_texture", $"../Item/item_base_texture"]

#Detects when item enters
func _on_area_entered(area: Area2D) -> void:
	is_in += 1


# Detects when item exits
func _on_area_exited(area: Area2D) -> void:
	is_in = 0

#Moves the item into the slot
func _process(_body) -> void:
	if is_in == 1:
		if !Input.is_action_pressed("Click"):
			print("if works")
			is_in = 0
			$Item_texture.texture = $"../Item/item_base_texture".texture
			$"../Item".visible = false
	#Script to take item out of slot
	if mouse_is_in == 1:
		if Input.is_action_pressed("Click"):
#			$"../Item".visible = true
			$Item_texture.texture = null
			$"../Item".visible = true


#Does nothing rn
func _ready() -> void:
	pass


#Detects if the mouse enters
func _on_area_mouse_exited(area: Area2D) -> void:
	mouse_is_in = 0


#Detects when the mouse exits
func _on_area_mouse_entered(area: Area2D) -> void:
	mouse_is_in += 1

Item code (I dunno why this formatted and not the other, sorry)

class_name Item extends Node2D
var mouse_is_in = 0
@onready var item:= $"../Inven_Slot"


# Nothing rn
func _ready() -> void:
	print(mouse_is_in)


# Moves it to the mouse when clicked
func _process(_delta) -> void:
	if mouse_is_in == 1:
		if Input.is_action_pressed("Click"):
			$"../Item".global_position = get_global_mouse_position()


#Detects if the mouse enters
func _on_area_2d_mouse_entered() -> void:
	mouse_is_in += 1


#Detects when the mouse exits
func _on_area_2d_mouse_exited() -> void:
	mouse_is_in = 0

Seems like you could use self.global_position or just global_position = get_global_mouse_position(); that will set the global position of what ever the script is attached to. Using $"../Item" goes up to the parent and finds a node name “Item”, hoping to find itself, but you could attach this script to anything (and multiple things) named anything.


``` will make a code block (great!) it will automatically pick colors for what ever language it detects, it usually does a bad job detecting the language

```gd or ```gdscript   will color the code block as if it was gdscript (which it probably is around these parts)

Okay, so swapping that did work, but I still have issues with the slot. The way I have it with the slot is that if you release the item in the slot, it removes the item itself and copies the texture onto the slot. But for there, it’s still only refrencing Item

You should probably communicate from the item to the inventory slot via a signal

class_name Item extends Node2D
var mouse_is_in = 0
signal picked_up(item: Item) ### New signal
@onready var base_texture = $item_base_texture ### storing this variable for other scripts

func _process(_delta) -> void:
	if mouse_is_in == 1:
		if Input.is_action_pressed("Click"):
			global_position = get_global_mouse_position()
			picked_up.emit(self) ### emit signal, anyone can listen including parent nodes

You inventory slot can listen to this signal, if you want to connect it through code or the editor is up to if you spawn in item slots or if they all exist in the editor.

func _ready() -> void:
    # connecting the new signal through code
    for slot in Itemz:
        slot.picked_up.connect(_on_item_picked_up)

func _on_item_picked_up(item: Item) -> void:
    item.visible = true
    item.base_texture.texture = null