Loot table problems
i am currently working on a loot table and it works sometimes, but sometimes it does nothing and im not sure why and am looking for help to fix the problem. for testing purposes i just have a light that turns on when that "loot" was hit and a debug.log to tell what loot was won, but sometimes it doesnt output anything and no lights turn on.
Answer by Larry-Dietz · Dec 28, 2019 at 02:47 PM
Your Total variable is going to be 100, the sum of the numbers in your table.
So, you are choosing a random number up to total, and checking to see if it is less then or equal to each value in the table.
That being said, any number select from 60 through total will never show as true in your if statement, and wont turn on the light/select the item.
Try reversing your logic. Make the if statement >=
instead of <=
As long at your numbers are in the table from greatest to least, (which they are) then this should get you the results you are looking for.
Here is a link to a discussion on using a weighted algorithm to select a random drop item.
It is only a minor change from what you are doing right now.
Hope this helps, -Larry
hey larry, i tried your suggestion and nothing changed. i tried over the week to figure it out through videos/forums and i wasnt able to find a solution. i am still getting the same problem with it only working sometimes. any more help would be great. thanks
Here is an implementation of weighted selection I did for a PrizeWheel in a game I did several years ago.
It works as expected, so should give you a good idea on how to implement it in your game. If you continue to have problems, let me know, and I will adapt it for the code you have shown.
PositionWeights = new List<float> { 18, 2.5f, 5, 6, 10, 5, 5, 18, 1.5f, 5, 6, 10, 3, 5 };
public int SelectRandomElement()
{
double diceRoll = UnityEngine.Random.Range(1f, 100f);
double cumulative = 0.0;
for (int i = 0; i < PositionWeights.Count; i++)
{
cumulative += PositionWeights[i];
if (diceRoll < cumulative)
{
return i;
}
}
return -1;
}
Your answer
Follow this Question
Related Questions
Black Scrreen after Splash Screen after deploy on HoloLens 1 Answer
trying to use a public int in a separate script, 1 Answer
Game crashes on iPhone 11 0 Answers
Need help with Mute Button 1 Answer
Frustrating UnityAds Please help 0 Answers