How can I use RandomItemDropper to drop items according to their rarity?

The way Random Item Dropper works is by using the Item Amount of each item in the Item Collection to build a probability table.
1606983744454.png
For example here you'll be 50 times more likely to get a pistol bullet than a health pack.

Here is the code for the RandomItemDropper:
Code:
/// <summary>
    /// Random Item dropper. Use the Inventory as a probability table.
    /// </summary>
    public class RandomItemDropper : ItemDropper
    {
        [Tooltip("The minimum amount of item that can be dropped.")]
        [SerializeField] protected int m_MinAmount = 1;
        [Tooltip("The maximum amount of item that can be dropped.")]
        [SerializeField] protected int m_MaxAmount = 2;

        protected ItemInfoProbabilityTable m_ItemAmountProbabilityTable;

        /// <summary>
        /// Initialize the probability table.
        /// </summary>
        protected override void Awake()
        {
            base.Awake();
            if (m_Inventory == null) { return; }

            var itemCollection = m_Inventory.GetItemCollection(m_ItemCollectionID);
            if (itemCollection == null) { return; }

            m_ItemAmountProbabilityTable = new ItemInfoProbabilityTable(
                itemCollection.GetAllItemStacks().ToListSlice());
        }

        /// <summary>
        /// Drop a random set of item amounts.
        /// </summary>
        public override void Drop()
        {
            var randomItemAmounts =
                m_ItemAmountProbabilityTable.GetRandomItemInfos(m_MinAmount, m_MaxAmount);

            DropItemsInternal(randomItemAmounts.ToListSlice());
        }
    }

If you wish to use a rarity attribute to decide the probablity of getting an item you could override this class and intialize the ItemAmountProbabilityTable using a list of item amounts computed using the rarity attribute.
The higher that amount, the higher to probability to drop the item.
 
Top