# Inspector variable dependent on other variables

Hello,

so my idea is to create a “drop table” as a scriptable object.
The scriptable objects contains multiple lists of items which can be dropped (say a list for “common”, a list for “rare” and so on) and each list has a drop chance linked to it.

In principle this is not a problem, however when setting the drop chances for each list in the inspector I would like the inspector to make sure that the sum of all chances never exceeds 1 (100%), so that I can then basically use a “staircase-style” function where I check from what list to pick without needing to worry about whether or not a particular chance can actually be reached at any point in my game.

e.g.:
Common - 75%
Rare - 20%
Legendary 5%

If I then increase the value of “Rare” to say 25% the inspector should automatically decrease the value of “Common” to 70%.

I guess there is probably a way to do that but I can’t see how, any help would be appreciated.

You can use .OnValidate() message to check if the value is correct.
Simply put your percentage checking logic there.

If you always want the probabilities to add up to 100%, then rather than specifying percentages, I find it’s usually more convenient to specify proportional weights.

e.g.:
Common - 15.0
Rare - 4.0
Legendary - 1.0

Then, in essence, you multiply all the weights by whatever number you need to to make them add up to 100%. Your code might look something like:

``````float totalWeight = allWeights.Sum();
float r = Random.Range(0, totalWeight);
for (int i = 0; i < allWeights.Count; ++i)
{
r -= allWeights[i];
if (r < 0)
{
// Use index i
break;
}
}
``````

Then if you decide you want legendaries to be a little more common, you can easily change them without altering the ratio between common and rare:

Common - 15.0
Rare - 4.0
Legendary - 1.5