Why do we need 2 SetItem() functions?

Out of curiosity, why do we need 2 SetItem() functions in InventoryItemIcon.cs outside of it being for convenience sake (my assumption)?

    public void SetItem(InventoryItem item)
        {
            SetItem(item, 0);
        }

        public void SetItem(InventoryItem item, int quantity)
        {
            var iconImage = GetComponent<Image>();
            if (item == null)
            {
                iconImage.enabled = false;
            }
            else
            {
                iconImage.enabled = true;
                iconImage.sprite = item.GetIcon();
            }

            if (itemQuantity)
            {
                if (quantity <= 1)
                {
                    textContainer.SetActive(false);
                }
                else
                {
                    textContainer.SetActive(true);
                    itemQuantity.text = quantity.ToString();
                }
            }
        }

In this case, it is for convenience, you are right. One takes quantity and one doesn’t. We may be setting item with a quantity of 0 a lot more than with an actual quantity. This ‘overload’ just sets a default quantity for use. We could also have set the default in a different way; with optional parameters (introduced a bit later in C#'s lifetime)

public void SetItem(InventoryItem item, int quantity = 0)
{
    ...
}
1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms