Godot Version
Godot 4.3 Stable
Question
I’ve been building an inventory system based off this tutorial:
and I’m using a similar method to automatically add items from here:
I’ve been running into a weird bug when first loading in the items, where the item icon will snap to the top left of the grid container. The actual item data is in the right slots and whenever you pickup/place the item after the initial load they function as expected. I found some other people who were having similar issues, but unfortunately none of their solutions have worked so far.
Here’s most of the code used to load items
func create_slots(inv_data: InventoryData):
for slots in inv_data.slots:
var new_slot = slot_scene.instantiate()
new_slot.slot_ID = grid_array.size()
grid_container.add_child(new_slot)
grid_array.push_back(new_slot)
new_slot.slot_entered.connect(_on_slot_mouse_entered)
new_slot.slot_exited.connect(_on_slot_mouse_exited)
pass
func populate_grid(inv_data: InventoryData) -> void:
for item_stack in inv_data.item_stacks:
var item = item_scene.instantiate()
add_child(item)
item.update_stack(item_stack)
item_held = item
_add_item()
and for placing them in the inventory
func _add_item():
for i in grid_array.size():
if find_free_slot(grid_container.get_child(i)) == true:
place_added_item(grid_container.get_child(i))
return
else:
continue
func find_free_slot(a_Slot):
for grid in item_held.item_grids:
var grid_to_check = a_Slot.slot_ID + grid[0] + grid[1] * col_count
var line_switch_check = a_Slot.slot_ID % col_count + grid[0]
if line_switch_check < 0 or line_switch_check >= col_count:
return false
if grid_to_check < 0 or grid_to_check >= grid_array.size():
return false
if grid_array[grid_to_check].state == grid_array[grid_to_check].States.TAKEN:
return false
return true
func place_added_item(a_Slot):
icon_anchor = Vector2(10000,100000)
item_held.get_parent().remove_child(item_held)
grid_container.add_child(item_held)
item_held.grid_anchor = a_Slot
for grid in item_held.item_grids:
var grid_to_check = a_Slot.slot_ID + grid[0] + grid[1] * col_count
grid_array[grid_to_check].state = grid_array[grid_to_check].States.TAKEN
grid_array[grid_to_check].item_stored = item_held
if grid[1] < icon_anchor.x: icon_anchor.x = grid[1]
if grid[0] < icon_anchor.y: icon_anchor.y = grid[0]
var calculated_grid_id = a_Slot.slot_ID + icon_anchor.x * col_count + icon_anchor.y
item_held._snap_to(grid_array[calculated_grid_id].global_position)
print(calculated_grid_id)
print(grid_array[calculated_grid_id].global_position)
item_held = null
clear_grid()
I think the problem might be at grid_array[calculated_grid_id].global_position, as it prints the coordinates at 0,0 on the inventory. Also sorry if this is a lot of text, this is my first time asking for coding advice and I wanted to be thorough.
Thanks!