The weighting framework allows you to define how relative weights between items differ over a secondary dimension such as time or distance. As an example, if you are dynamically spawning enemies you can adjust which enemies are spawned as time changes.

Each item that you are interested in is represented by a list of DistanceWeightValue’s that specify what that items weighting should be at a given distance. DistanceWeightedItems is used to control the relative weighting between items. To setup weighting, you need to:

Create an instance of the DistanceWeightedItems class, specifying as the generic the type what you want to track (this could be a prefab, or some custom class).

  1. Call AddItem() on all items you are weighting passing a list of DistanceWeightValue’s
  2. Call PrepareForUse() to calculate and sync weights for further use.

Once setup is complete, you can call GetItemForDistance() to get a random, weighted item based upon the distance (or time) that you specify.

For the following example:

Subsequent calls to distanceWeightedItems.GetItemForDistance() would then return the following values for the given distances:

0 – “Item1”
1 – “Item1”
1.5 – “Item1”
2 – “Item1” or “Item2” with a ratio of 3:7
3 – “Item1” or “Item2” with a ratio of 3:7
4 – “Item2”
5 – “Item2”

The ParallexScrolling script in the Pro Bundle contains an example of weighting where the dynamically generated landscape can be changed over time to give a more varies landscape; You can simple go from a forest to a desolate landscape by setting different weights on the prefabs used.

Weights for Gameobjects

There is a DistanceWeight component that you can add to a gameobject or prefab that will allow you to specify a list of DistanceWeightValue’s that you can easily extract and use.



See also

If you have any thoughts or suggestions on improvements or additions to the Messaging features then please let us know!