class_name HotbarComponent extends Node var selected_item: int = 0 var prev_selected_item: int = 0 var inventory: Array[ItemData] = [null, null] @export var equipped_item_mesh: MeshInstance3D @export var hold_position: Marker3D @export var cursor_raycast: RayCast3D signal added_inventory_slot(successful: bool) signal removed_inventory_slot(successful: bool) signal select_item(index: int) # For UI elements signal add_item(index: int, item: ItemData) signal remove_item(index: int) func add_inventory_slot() -> bool: if len(inventory) < 5: inventory.append(null) added_inventory_slot.emit(true) return true added_inventory_slot.emit(false) return false func remove_inventory_slot() -> bool: if len(inventory) > 2: inventory.remove_at(len(inventory) - 1) removed_inventory_slot.emit(true) return true removed_inventory_slot.emit(false) return false func next_item(): prev_selected_item = selected_item if selected_item + 1 > len(inventory) - 1: selected_item = 0 else: selected_item += 1 _select_item() func prev_item(): prev_selected_item = selected_item if selected_item - 1 < 0: selected_item = len(inventory) - 1 else: selected_item -= 1 _select_item() func _select_item(index: int = selected_item): selected_item = index if index >= 0 and index < len(inventory): select_item.emit(index) # Update the hand visual if inventory[index] == null: equipped_item_mesh.mesh = PlaceholderMesh.new() else: # Apply the mesh directly from the ItemData! equipped_item_mesh.mesh = inventory[index].hand_mesh func _add_item(item: EquippableItem): var target_index = -1 # 1. Decide which slot to use (current slot first, then first empty) if inventory[selected_item] == null: target_index = selected_item else: for i in range(len(inventory)): if inventory[i] == null: target_index = i break # If inventory is full, do nothing if target_index == -1: print("Inventory Full!") return # 2. Extract Data from the physical item var new_item_data: ItemData = ItemData.new() new_item_data.name = item.name new_item_data.weight = item.weight new_item_data.scene_path = item.scene_file_path if item is Backpack: new_item_data.item_type = ItemData.ItemType.BACKPACK new_item_data.provided_slots = item.inventory_size elif item is Pocket: new_item_data.item_type = ItemData.ItemType.POCKET else: new_item_data.item_type = ItemData.ItemType.NORMAL # Save the mesh so we can display it in the player's hand later if item.mesh: new_item_data.hand_mesh = item.mesh.mesh # Await your custom icon generator (if it requires awaiting) new_item_data.ui_icon = await GrabbableBase.create_icon_from_mesh(item.mesh.mesh) # 3. Add to inventory array and UI inventory[target_index] = new_item_data add_item.emit(target_index, new_item_data) # 4. CRITICAL: Delete the physical item from the world! item.queue_free() # Update the hand model if we added it to our currently selected slot _select_item(selected_item) func update_slot(index: int, new_item: ItemData) -> void: inventory[index] = new_item # Tell the in-game HUD that the slot changed if new_item != null: add_item.emit(index, new_item) else: remove_item.emit(index) # If this is the slot the player is currently holding, update the 3D hand mesh! if index == selected_item: _select_item(selected_item) func _drop_item(index: int = selected_item): var item_data = inventory[index] if item_data != null and item_data.scene_path != "": # 1. Load the original scene file for the item var packed_scene = load(item_data.scene_path) as PackedScene if not packed_scene: push_error("Could not load scene path: " + item_data.scene_path) return # 2. Instantiate a brand new physical object var new_object = packed_scene.instantiate() as RigidBody3D # 3. Add it to the world (Parent it to the main scene, not the player!) get_tree().current_scene.add_child(new_object) # 4. Place it in front of the player new_object.global_position = hold_position.global_position new_object.global_rotation = Vector3.ZERO # 5. Clear the inventory slot _remove_item(index) func _remove_item(index: int): remove_item.emit(index) inventory[index] = null _select_item(selected_item) # Refresh the hand model func get_selected_item() -> ItemData: return inventory[selected_item] func _ready() -> void: get_tree().create_timer(0.1).timeout.connect(func(): _select_item(0)) func _process(_delta: float) -> void: if Console.toggled or UiManager.in_game_menu.visible: return if Input.is_action_just_pressed("next_item"): next_item() if Input.is_action_just_pressed("prev_item"): prev_item() if Input.is_action_just_pressed("select_item_1"): _select_item(0) if Input.is_action_just_pressed("select_item_2"): _select_item(1) if Input.is_action_just_pressed("select_item_3"): _select_item(2) if Input.is_action_just_pressed("select_item_4"): _select_item(3) if Input.is_action_just_pressed("select_item_5"): _select_item(4) if Input.is_action_just_pressed("equip"): if cursor_raycast.is_colliding(): var hit_object = cursor_raycast.get_collider() if hit_object is EquippableItem: _add_item(hit_object) if Input.is_action_just_pressed("drop"): _drop_item() #class_name HotbarComponent extends Node # #var selected_item: int = 0 #var prev_selected_item: int = 0 #var selected_item_object: EquippableItem = null #var inventory: Array[ItemData] = [null,null] # #@export var equipped_item_mesh: MeshInstance3D #@export var hold_position: Marker3D #@export var cursor_raycast: RayCast3D # #signal added_inventory_slot(successful:bool) #signal removed_inventory_slot(successful:bool) #signal select_item(index: int) # ##for ui elements #signal add_item(index: int, item: ItemData) #signal remove_item(index: int) # #func add_inventory_slot() -> bool: #if len(inventory) < 5: #inventory.append(null) #added_inventory_slot.emit(true) #return true #added_inventory_slot.emit(false) #return false # #func remove_inventory_slot() -> bool: #if len(inventory) > 2: #inventory.remove_at(len(inventory) - 1) #removed_inventory_slot.emit(true) #return true #added_inventory_slot.emit(false) #return false # #func next_item(): #prev_selected_item = selected_item #if selected_item + 1 > len(inventory) - 1: #selected_item = 0 #else: #selected_item += 1 #_select_item() # #func prev_item(): #prev_selected_item = selected_item #if selected_item - 1 < 0: #selected_item = len(inventory) - 1 #else: #selected_item -= 1 #_select_item() # #func _select_item(index:int = selected_item): #if index >= 0 and index < len(inventory): #select_item.emit(index) # #if inventory[index] == null: ##if current index is empty in the inventory, replace held object with placeholder (empty mesh) #equipped_item_mesh.mesh = PlaceholderMesh.new() #else: #var mesh = null #for i in inventory[index].get_children(): #if i is MeshInstance3D: #equipped_item_mesh.mesh = i.mesh # #func _add_item(item: EquippableItem): ##add item to current equipped slot if empty # #var new_item_data = ItemData.new() #new_item_data.name = item.name #new_item_data.weight = item.weight #new_item_data.scene_path = item.scene_file_path #new_item_data.ui_icon = await GrabbableBase.create_icon_from_mesh(item.mesh.mesh) #if inventory[selected_item] == null: #inventory[selected_item] = new_item_data # #add_item.emit(selected_item, new_item_data) # ##ui.add_item(selected_item, item) #item.reparent(get_parent().get_parent()) #item.collision_layer = 0 #item.visible = false #_select_item(selected_item) #return # ##add item to first empty slot if currently equipped slot is full #for i in range(0, len(inventory)): #if inventory[i] == null: #inventory[i] = new_item_data # #add_item.emit(i, new_item_data) ##ui.add_item(i, item) #item.reparent(get_parent().get_parent()) #item.collision_layer = 0 #item.visible = false #return # ##dont equip item if inventory full # #func _drop_item(index: int = selected_item): #if inventory[index] != null: #var object = inventory[index] # #object.scale = Vector3.ONE # #if object.is_inside_tree(): #object.reparent(get_tree().current_scene, true) # #object.collision_layer = 17 #object.global_position = hold_position.global_position #object.visible = true #object.global_rotation = Vector3.ZERO # #_remove_item(index) # #func _remove_item(index: int): ##ui.remove_item(index) #remove_item.emit(index) #inventory[index] = null #_select_item(index) # #func get_selected_item() -> ItemData: #return inventory[selected_item] # #func _ready() -> void: #get_tree().create_timer(0.1).timeout.connect(func (): _select_item(0)) # #func _process(_delta: float) -> void: #if not Console.toggled and not UiManager.in_game_menu.visible: #if Input.is_action_just_pressed("next_item"): #next_item() # #if Input.is_action_just_pressed("prev_item"): #prev_item() # #if Input.is_action_just_pressed("select_item_1"): #_select_item(0) # #if Input.is_action_just_pressed("select_item_2"): #_select_item(1) # #if Input.is_action_just_pressed("select_item_3"): #_select_item(2) # #if Input.is_action_just_pressed("select_item_4"): #_select_item(3) # #if Input.is_action_just_pressed("select_item_5"): #_select_item(4) # #if Input.is_action_just_pressed("equip"): #if cursor_raycast.is_colliding(): #var hit_object = cursor_raycast.get_collider() # #if hit_object is EquippableItem: #_add_item(hit_object) # #if Input.is_action_just_pressed("drop"): #_drop_item()