How can I achieve my weighted selection?
Hello, sorry if the title is a little ambiguous but I was unsure how else to word it. I'll show my code and then explain further:
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Loot Pool", menuName = "Scriptable Object/Loot/Loot Pool", order = 1)]
public class LootPool : ScriptableObject
{
public List<Item> pool;
}
using System.Collections.Generic;
using UnityEngine;
public class LootTable : MonoBehaviour
{
public List<LootPool> pools;
}
As you can see I am creating Loot Pools as scriptable objects. My enemies will each have a LootTable script attached to them that contains a List of LootPools. I would like to add a weight (chance) to these loot pools, so that when an item is dropped it has different chances to choose from different lists. I know how to code the logic in for this but I can;t figure out where to add the "weight" variable. If I added it to the LootPool class then I would not be able to have different weights for different enemies. I hope someone can please advise? I did already think to make another class that had LootPool and weight as its variables, but this seems like more of a work around than a legit solution. I feel the answer may be a simple one but my brain can't see it right now...
Answer by Hellium · Jun 11, 2020 at 10:47 PM
[System.Serializable]
public struct LootPoolProbability
{
public LootPool Pool;
public int Weight;
}
public class LootTable : MonoBehaviour
{
[SerializeField]
private List<LootPoolProbability> pools;
public LootPool PickPool()
{
// Weighted random algorithm
}
}