Overview
.....
Perception Scorer System gives a score to every actor your AI knows about through AI Perception, and tells you which one has the highest score.
Instead of "attack the closest enemy", your AI can weigh many things together: how close an actor is, how much damage it dealt, whether it is winding up an attack, and anything else you can describe as a number.
⭐ Key Benefits#
- Built on AI Perception: actors are added and removed as the AI perceives and forgets them.
- Modular scoring: every reason to care about an actor is its own Score Type asset.
- Weights: decide how much each score type counts towards the total score.
- Negative scores: make an actor less interesting, for example while it is blocking.
- React to events: Score Listeners turn things that happen in the world, like damage or an attack windup, into scores.
- Highest scored actor: events fire when another actor takes the lead, overall and for each team attitude.
- No flickering targets: a switch margin keeps the current target until another one is clearly better.
- Extend in Blueprint: write your own score deciders, weights, filters, attitude deciders and listeners.
- Debug view: one console command shows every AI's scores above its pawn.
Key Concepts#
| Concept | What it is |
|---|---|
| Perception Scorer System | The component (UPerceptionScorerSystem). Add it to an AI controller that has an AI Perception component. |
| Scored Entity | One perceived actor with its team attitude, its score per score type and its total score. |
| Score Type | A data asset for one reason to score an actor, for example Distance or Damage Taken. |
| Score Decider | The rule inside a score type that updates its score over time. |
| Score Weight | The multiplier of a score type's score in the total score. |
| Entity Filter | Decides which perceived actors are scored at all, for example to leave out friendly ones. |
| Team Attitude Decider | Decides whether an actor is Friendly, Neutral or Hostile. |
| Score Listener | Listens to events in the world and turns them into scores. Every AI has its own copy. |
The total score of an actor is the sum of its scores, each multiplied by the weight of its score type:
Total Score = Score A × Weight A + Score B × Weight B + ...Scores and totals can be negative.
Setup#
- Enable the Perception Scorer System plugin. It needs the Modular Gameplay plugin, which is enabled with it.
- Open your AI controller and make sure it has an AI Perception component.
- Add the Perception Scorer System component to the same AI controller.
- Create your score types: right click in the Content Browser, Miscellaneous → Data Asset, and pick Perception Score Type.
- Add the score types every actor should have to Always Granted Perception Score Types on the component.
The component finds the AI Perception component on begin play and starts updating the scores. It runs on the server only, like the AI controller.
Component Settings#
| Setting | What it does |
|---|---|
| Perception Score Update Interval | Seconds between score updates. Default 0.5. |
| Pending Score Lifetime | How long a score added for a not yet perceived actor is kept. 0 ignores such scores. |
| Always Granted Perception Score Types | Score types every scored entity has. They cannot be removed from an entity, but the list can be changed at runtime. |
| Entity Filter | Optional. When empty, every perceived actor is scored. |
| Team Attitude Decider | Optional. When empty, the AI controller's Get Team Attitude Towards is used. |
| Score Listeners | Optional. Turn events in the world into scores. See Score Listeners. |
| Highest Score Switch Margin | How much higher another score must be to take the lead, overall and within each team attitude. 0.1 means 10% higher. |
Score Types#
A score type has a Name, an optional Score Decider and an optional Weight.
- Without a score decider, the score keeps whatever value was added.
- Without a weight, the weight is 1.
Ready-made Score Deciders#
| Decider | What it does |
|---|---|
| Distance Score Decider | Scores an actor by its distance to the AI's pawn, blending from Score At Min Distance to Score At Max Distance. Replaces the score on every update. |
| Decay Score Decider | Moves the score towards zero by Decay Per Second, from above or from below. Can remove the score type once it reaches zero. Good for damage that is forgotten slowly. |
| Sense Score Decider | Scores an actor by the senses that sense it right now, using Sense Scores (by default Sight 3, Damage 2, Hearing 1). The highest one wins; Score When Not Sensed is used otherwise. Replaces the score on every update. |
| Stimulus Age Score Decider | Scores an actor by how long ago it was last sensed, blending from Score At Zero Age to Score At Max Age over Max Age seconds. Senses limits which senses count. Replaces the score on every update. |
Ready-made Weights#
| Weight | What it does |
|---|---|
| Static Weight | The same fixed weight for every AI. |
| Multiple Weights Multiplier | Multiplies several weights with each other. |
Ready-made Entity Filters#
| Filter | What it does |
|---|---|
| Team Attitude Entity Filter | Only scores actors of the chosen attitudes: Score Friendly, Score Neutral, Score Hostile (only Hostile by default). Uses the component's Team Attitude Decider, or else the AI controller, so it matches the attitude of the scored entities. |
| Multiple Entity Filters | An actor is only scored when it passes every filter in the list. |
Using It in Blueprint#
Adding Scores#
Call Add Score with an actor, a score type and a value, for example when the AI's pawn takes damage:
- The value is added to the actor's current score of that type. It can be negative.
- If the actor is not perceived yet, the score is kept for Pending Score Lifetime and applied once it is.
- Remove Score removes a score type from an actor. Always granted types cannot be removed.
Always Granted Types at Runtime#
These run on the server only.
| Function | What it does |
|---|---|
| Add Always Granted Score Type | Gives the type to every scored entity, the current ones included, starting at 0. An entity that already has a score of this type keeps it. |
| Remove Always Granted Score Type | Stops granting the type. With Remove From Entities (on by default) the current entities lose it; otherwise they keep it as a normal score. |
| Get Always Granted Score Types | The current list. |
Reading Scores#
| Function | What it returns |
|---|---|
| Get Highest Scored Entity | The entity with the highest total score, optionally filtered by attitude. With a filter, the switch margin is measured against that attitude's own leader. |
| Get Scorable Entities | Every scored entity, optionally filtered by attitude. |
| Is Actor Scored | Whether the actor has a scored entry. |
| Get Team Attitude Towards Actor | The attitude towards the actor, from the Team Attitude Decider or else the AI controller. |
Events#
| Event | When it fires |
|---|---|
| On Highest Scored Entity Changed | Another actor took the lead among all entities. Either actor can be empty. |
| On Highest Scored Entity By Attitude Changed | Another actor took the lead among the entities of one attitude. Fires separately for Friendly, Neutral and Hostile, with the attitude, the new and the previous actor. |
| On Perception Scores Updated | After every update, once all scores are calculated. |
| On Scored Entity Added | An actor got an entry. |
| On Scored Entity Removed | An actor lost its entry. The actor is empty when it was destroyed. |
A typical AI binds On Highest Scored Entity By Attitude Changed and sets its target when the Hostile leader changes, for example in a Blackboard key.
Score Listeners#
Score deciders update scores on every timer update. Score listeners react the moment something happens instead: damage taken, an enemy winding up an attack, an enemy starting to block.
The plugin doesn't know how your game reports these events. A listener is a class you derive from Perception Scorer Score Listener and bind to whatever your game uses. Add your listeners to Score Listeners on the component. Every AI gets its own copy, so a listener can keep its bindings.
There are two ways to listen:
- On the AI itself: bind to the controlled pawn in On Pawn Changed, for example to its damage events.
- On each scored entity: bind in On Entity Added and unbind in On Entity Removed, for example to an enemy's attack windup.
Events#
| Event | When it is called |
|---|---|
| On Initialize | The component starts, before the first entities are added. |
| On Deinitialize | The component stops. Unbind everything here; On Entity Removed is not called for the remaining entities. |
| On Pawn Changed | The AI controller possesses another pawn, and once at start if it already has one. Either pawn can be empty. |
| On Entity Added | An actor got a scored entry. |
| On Entity Removed | An actor lost its entry. The actor is empty when it was destroyed. |
Helpers#
| Function | What it does |
|---|---|
| Get Scorer, Get Owner AI Controller, Get Controlled Pawn | Quick access to the AI. |
| Resolve Scored Actor | Finds the actor a score belongs to: the actor itself when it is scored, a controller's pawn, or the instigator or owner of a weapon or projectile. |
| Add Score, Remove Score | Like the component's, but resolve the actor first, so you can pass a damage instigator or causer directly. |
| Report Damage To Perception | Reports the damage as an AI Perception damage stimulus, so an attacker the AI didn't perceive yet becomes perceived. Needs a Damage sense on the AI Perception component. |
For example, a damage listener binds to the pawn's damage event in On Pawn Changed. When the pawn is hit, it calls Add Score with the attacker, a Damage Taken score type with a slow Decay Score Decider, and the damage amount, then Report Damage To Perception so an attacker from behind is noticed too.
Extending in Blueprint#
Create a Blueprint class from any of these and override its function:
| Base class | Function to override |
|---|---|
| Perception Scorer Score Decider | Set Score: return the new score. Set Remove Score to drop the score type. |
| Perception Scorer Score Weight | Get Weight: return the multiplier. |
| Perception Scorer Entity Filter | Can Score Actor: return false to leave the actor out. |
| Perception Scorer Team Attitude Decider | Get Team Attitude: return Friendly, Neutral or Hostile. |
| Perception Scorer Score Listener | Its events: bind to your game's events and call Add Score. |
Debugging#
Run this console command in Play In Editor:
PerceptionScorer.Debug 1Every AI with a Perception Scorer System component shows a box above its pawn with its scored entities, their attitude, each score with its weight, and the total. The highest scored actor is marked with >.
Turn it off with PerceptionScorer.Debug 0.
Unreal's Gameplay Debugger is separate: open it with EnableGDT or the ' key. It shows one AI in detail, including what its AI Perception senses. The two work well together: the Gameplay Debugger shows what the AI perceives, and the debug view shows how it scores it.