"### Inventory_UI.gd
extends Control
Scene-Tree Node references
@onready var grid_container = $GridContainer
Drag/Drop
var dragged_slot = null
func _ready():
# Connect function to signal to update inventory UI
grid_container.columns = 5
get_viewport().connect(“size_changed”, Callable(self, “_on_viewport_size_changed”))
Global.inventory_updated.connect(_on_inventory_updated)
call_deferred(“_on_inventory_updated”)
Update inventory UI
func _on_inventory_updated():
print(“_on_inventory_updated called in Inventory_UI”)
# Clear existing slots
clear_grid_container()
print(“Grid container cleared”)
# Add slots for each inventory position
for item in Global.inventory:
print("Processing item: ", item)
var slot = Global.inventory_slot_scene.instantiate()
if slot == null:
print("Error: Failed to instantiate slot")
continue
slot.drag_start.connect(_on_drag_start)
slot.drag_end.connect(_on_drag_end)
grid_container.add_child(slot)
print("Slot added to grid container")
if item != null:
slot.set_item(item)
print("Setting item in slot: ", item)
else:
print("Setting slot to empty")
slot.set_empty()
Clear inventory UI grid
func clear_grid_container():
while grid_container.get_child_count() > 0:
var child = grid_container.get_child(0)
grid_container.remove_child(child)
child.queue_free()
Store dragged slot reference
func _on_drag_start(slot_control : Control):
dragged_slot = slot_control
print("Drag started fro slot: ", dragged_slot)
func _on_drag_end():
var target_slot = get_slot_under_mouse()
if target_slot and dragged_slot != target_slot:
drop_slot(dragged_slot, target_slot)
dragged_slot = null
Get the current mouse position in the grid_container’s coordinate system
func get_slot_under_mouse() → Control:
var mouse_position = get_global_mouse_position()
for slot in grid_container.get_children():
var slot_rect = Rect2(slot.global_position, slot.size)
if slot_rect.has_point(mouse_position):
return slot
return null
Find the index of a slot
func get_slot_index(slot: Control) → int:
for i in range(grid_container.get_child_count()):
if grid_container.get_child(i) == slot:
# Valid slot found
return i
# Invalid slot
return -1
Drop slots
func drop_slot(slot1: Control, slot2: Control):
var slot1_index = get_slot_index(slot1)
var slot2_index = get_slot_index(slot2)
if slot1_index == -1 or slot2_index == -1:
print(“Invalid slots found”)
return
else:
if Global.swap_inventory_items(slot1_index, slot2_index):
print("Drpping slot items: ", slot1, slot2_index)
_on_inventory_updated()
"
when i delete all script that fault is dissapeared,
print("Inventory_Slot object name : " + name)
Inventory_Slot object name : InventorySlot
Item ready: RigidBody2D
Player ready
Success: inventory_ui instantiated
_on_inventory_updated called in Inventory_UI
Grid container cleared
Processing item:
Inventory_Slot object name : Inventory_Slot
Slot added to grid container
Setting slot to empty
Processing item:
Inventory_Slot object name : @Control@11
Slot added to grid container
Setting slot to empty
Processing item:
Inventory_Slot object name : @Control@12
Slot added to grid container
Setting slot to empty
Processing item: