Thanks!
Weighted selections aren’t exactly purely random. In fact, the reason weights are used are to control just how random a choice is. Selection weighting is actually a pretty core component of many AI and machine learning approaches. I’ll give a really quick Unity-flavored example:
An enemy mob in our RPG might have a few behaviors it could engage in, let’s say “Attack”, “Heal” and “Escape”. We want to generally pursue a best course of action, something like “Attack until you’re low on health, then heal - or run if you’re really outclassed”.
If we try to code this as a behavior tree it’s either really predictable (it’ll attack until 20% HP, then it will heal, then etc.), or really time consuming to write and frustrating to test ( if health < 20% and rng.val > 50% then X else Y etc etc…)
Weighted selections, on the other hand, allow for as much randomness as we’d like, but still preserve the general ideas we’re going for, and give us interesting or complex behavior for a minimum of development time. Instead of determining exactly what we want the mob to do and when, we can model behavior based on “wants”:
If we’re at full health, we just want to attack the player, so let’s start with that:
Behavior : Weight
ATTACK : 100
HEAL : 0
ESCAPE : 0
But we want to heal at low health, so maybe something like:
ATTACK : 100
HEAL : HP_MAX - HP_CURRENT
ESCAPE : 0
This way, the chance we’ll heal gets higher as our HP decreases.
But, if we’re close to death, we need to get out:
ATTACK : 100
HEAL : HP_MAX - HP_CURRENT
ESCAPE : if (HP_CURRENT < 20) HEAL_WEIGHT * 2
(keep in mind these values are pulled from thin air, for the sake of example)
Every tick/frame/whatever the monster would re-weight his options, and when it’s appropriate to make a decision, Selectionator would return an action based on weight.
So if he’s low on health, our monster starts to really want to bail - but still has a chance to take a parting shot or drop a heal, which would change his mind about running, if he got enough health back.
We can keep adding on like this to generate really complex behavior. Maybe the monster has a defense ability that negates a round of damage - we can have him watch the player’s SPECIAL_ENERGY value, and now it’s like he’s anticipating the player using a special attack. We could also reference the player’s current HP when considering escape and weight it high if the player is at high health when we’re at low health, but keep it low or neutral if our monster has the HP advantage.
As a game developer this is a really nice approach because it is easier (in many cases) to write, but way, way easier to test and adjust than most AI solutions. Decision trees and hardcoded behavior require lots of trial and error (or really intense debug modes and editors).
Weights, on the other hand, can just be exposed during development, and even at runtime. Bind a key to increase enemy aggression or defensiveness or passivity and you can tweak in game mode.
I hope that gave you a few ideas. If you have any other questions, please let me know!