finepointcgi / Godot-Horror-Game-GD

28 stars 5 forks source link

Adding more items to the Inventory than its capacity. #1

Closed RobertSzaszak closed 1 year ago

RobertSzaszak commented 1 year ago

If you add more items to the inventory than its capacity by using the Add Item button you get the following error:

Attempt to call function 'UpdateItem' in base 'null instance' on a null instance.
E 0:00:07:0804   _on_button_4_button_down: Index p_index = 20 is out of bounds (data.children.size() - data.internal_children_front - data.internal_children_back = 20).
  <C++ Source>   scene/main/node.cpp:1243 @ get_child()
  <Stack Trace>  Inventory.gd:118 @ UpdateButton()
                 Inventory.gd:85 @ Add()
                 Inventory.gd:155 @ _on_button_4_button_down()
RobertSzaszak commented 1 year ago

I could prevent crashing by using the following code:

func UpdateButton(index : int):
    if gridContainer.get_child(index) == null:
        print("Error: Child at index " + str(index) + " is null")
        return
    if range(items.size()).has(index):
        gridContainer.get_child(index).UpdateItem(items[index], index)

    else:
        gridContainer.get_child(index).UpdateItem(null, index)
RobertSzaszak commented 1 year ago

I also changed the function button function:

func _on_button_button_down() -> void:
    var item = ResourceLoader.load("res://Resources/Test_Item.tres")
    if items.size() < gridContainer.get_child_count():
        Add(item)
    else:
        print("Error: Grid container is full")
    pass
finepointcgi commented 1 year ago

This issue has been fixed!