I want to make a "Random Box Item"

difqk3

New member
Unlike "Surprise Box", if you use "Random Box Item", I want to create a Box that generates items according to probability
For example, there is a "normal item = 50% / magic item = 30% / epic item = 20%" probability.
Is there a function that an item can generate based on probability?
 
There is no function like this built-in but you could create one fairly easily using a custom Item Action. Learn more about them here.
Check the existing Item Actions to have a better idea of how they work.

You can create a ItemInfoProbabilityTable from a list of item amounts. Where the higher the amount, the higher the probability to get that item.
You can get all the itemDefinitions from the InventorySystemManager and filter by category (normal, magic, epic), or you could get all itemDefinitions in a category.

From there you could create the probability table, create it once the first time the action is called. Once you have the probability table you can call it whenever you want to get a list of random items.

Here is some pseudo code to give you some pointers:

C#:
var category = InventorySystemManager.GetItemCategory("MyCategory");

// Get all ItemDefinition from the category. CAREFUL when using this solution the array size can be bigger than the numberOfItems!
var itemDefinitionsForCategory = new ItemDefinition[0];
var numberOfItems = category.GetAllChildrenElements(ref itemDefinitionsForCategory);

// Or you could get all item Definitions and filter by category.
var itemDefinitionsForCategory2 = new List<ItemDefinition>();
var allItemDefinitions = InventorySystemManager.ItemDefinitionRegister.GetAll();
foreach (var itemDefinition in allItemDefinitions) {
    if (category.InherentlyContains(itemDefinition)) {
        itemDefinitionsForCategory2.Add(itemDefinition);
    }
}

//From the definition create a list of ItemInfo, where the amount of Item defines the probabily
var itemInfos = new List<ItemInfo>();
for (int i = 0; i < itemDefinitionsForCategory2.Count; i++) {
    var probability = 1; // Choose the probability here. The higher the number the higher the probability to get it.
  
    var itemInfo = (ItemInfo) (probability, itemDefinitionsForCategory2[i].DefaultItem);
  
    itemInfos.Add( itemInfo );
}

//Create a probability table
var probabilityTable = new ItemInfoProbabilityTable(itemInfos);

//ALL the above should be done once in an intialization phase. Then you can call the function below whenever.

//Get a random item with probability
var minAmount = 1;
var maxAmount = 3;
var listOfRandomItems = probabilityTable.GetRandomItemInfos(minAmount, maxAmount);

I hope this helps
 
Top