Structs

Structs

Structs are used to group values and objects together. The way the are written is similar to a class. They function very differently though. A struct cannot inherit from another struct. Structs do not create objects they create values, therefore they are saved on the stack and not the heap, which can reduce garbage collection. The tradeoff is that modifying the struct value will only work on the local scope of the code, therefore struct should in principle be immutable. If you do not know about stack, heaps, garbage collection, etc… you may research about it but it is not required to understand them to create game prototypes or simple games in general.

Example of a struct:

/// <summary>
/// An item amount.
/// </summary>
[Serializable]
public struct ItemAmount
{
    [Tooltip("The item definition.")]
    [SerializeField] private ItemDefinition m_ItemDefinition;
    [Tooltip("The item.")]
    [SerializeField] private Item m_ItemComponent;
    [Tooltip("The amount.")]
    [SerializeField] private int m_Amount;

    private IItem m_Item;
    
    public int Amount => m_Amount;
    public IItem Item
    {
        get
        {
            if (m_Item != null) { return m_Item;}

            if (m_ItemComponent != null) {
                m_Item = m_ItemComponent;
                return m_Item;
            }
            
            if (m_ItemDefinition == null) { return null;}
            
            return m_ItemDefinition.DefaultItem;
        }
    }

    /// <summary>
    /// Constructor.
    /// </summary>
    /// <param name="item">The item.</param>
    /// <param name="amount">The amount.</param>
    public ItemAmount(IItem item, int amount)
    {
        m_Amount = amount;
        m_Item = item;
        m_ItemComponent = item as Item;
        m_ItemDefinition = item?.ItemDefinition;
    }

    public static implicit operator ItemAmount( (int,IItem) x) 
        => new ItemAmount(x.Item2,x.Item1);
    public static implicit operator ItemAmount( (IItem,int) x) 
        => new ItemAmount(x.Item1,x.Item2);
}