Skip to content
Aprius edited this page Aug 28, 2024 · 2 revisions

Bag

Provide get item in array with weighted probability.

image

  • Sample 1
var bag = new Bag<string>(
            new BagItem<string> {item = "a", weight = 0.3f},
            new BagItem<string> {item = "b", weight = 0.6f},
            new BagItem<string> {item = "c", weight = 0.14f});

bag.Pick(); // take random item form bag
bag.Pop(); // take random item form bag and remove it
  • Sample 2
[SerializeField] private Bag<string> bag; // initialize bag outside inspector

private void Start()
{
    bag.Initialize(); // you need to call Initialize when using inspector to initialize the bag, not new

    bag.Pick(); // take random item form bag
    bag.Pop(); // take random item form bag and remove it
}
  • Sample 3
var bag = new Bag<string>(ENumberRoll.Two,
    ERollResultType.Min,
    new BagItem<string> {item = "a", weight = 0.3f},
    new BagItem<string> {item = "b", weight = 0.6f},
    new BagItem<string> {item = "c", weight = 0.14f});

bag.Pick(); // take random item form bag
bag.Pop(); // take random item form bag and remove it
  • ENumberRoll : An enum that specifies how many times to roll a random value and then compare it against the ERollResultType to pick the final value.

    • One
    • Two
    • Three
  • ERollResultType : Strategy to pick the final value if the number of rolls is greater than one

    • None : Using this option implies that the number roll is one time.
    • Min : Select the smallest value in the rolls
    • Max : Select the largest value in the rolls
    • Sum : The final value will be equal to the sum of the values ​​of all the rolls.
  • Graph showing the probability of selection when using None

image

  • Graph showing the probability of selection when using Min

image

  • Graph showing the probability of selection when using Max

image

  • Graph showing the probability of selection when using Sum

image

Random Float

    public RandomFloat rand;

    private void Start()
    {
        float value = rand.Value();
    }

image

Random Int

    public RandomInt rand;

    private void Start()
    {
        int value = rand.Value();
    }

image

Clone this wiki locally