Write a Custom Target Scorer
Goal. A Soldier that never fires at an ally and, among the enemies in range, goes for Trucks first.
A target query has two halves. The framework runs the mechanical half the same way every time: range, firing arc, required component, required tags, line of sight and a fixed order for ties. A Target Scorer owns the policy half: which candidates count at all, and which one wins. You override two functions and attach the scorer to the query.
The built-in scorer skips only entities owned by the asking unit’s own player, and scores nearer targets higher. In a game with allied players that is not enough, and it has no idea of priority.
Before you start
Section titled “Before you start”You need an attack that uses Find Targets or Check Target, such as SA_Attack and SA_AutoAttack from Add Combat. Two players on the same team for the test: in the lobby, give two players the same team. Team 0 means no team. Teams grant each other Present As Friendly diplomacy when the match starts, which is what the scorer checks.
1. Create the scorer
Section titled “1. Create the scorer”Right-click in the Content Browser and choose Blueprint Class, then pick Target Scorer under All Classes. Name it TS_NoAlliesPreferTrucks.
Add two variables, both Instance Editable:
| Variable | Type | Default |
|---|---|---|
PreferredTag |
Gameplay Tag | SeinARTS.Unit.Truck |
PreferenceBonus |
Fixed Point | 500 |
A scorer is stateless. The framework evaluates it on its class defaults, as many times as it needs to, in any order. Read the variables, but never write them, and keep no counters. Data validation fails a scorer that writes its own variables. Both functions are pure.
2. Skip allies: Is Valid Target
Section titled “2. Skip allies: Is Valid Target”Override Is Valid Target. It receives World, Query and Candidate (an entity handle), and returns a Boolean. It runs after the mechanical gates, so the candidate is already alive, in range and in line of sight. Return false to refuse it.
- Get Entity Owner of the Candidate: this is the candidate’s player.
- Break Sein Target Query on Query, and Get Entity Owner of Instigator: this is the asking player.
- Has Diplomacy with Source Player the candidate’s player, Target Player the asking player, and Diplomacy Tag
SeinARTS.Diplomacy.Grant.PresentAsFriendly. It is true for allies, and also true for a player with itself, so it covers the player’s own units too. - Return Not of the result.
Every node that asks for a World Context Object gets the World input wired into it. The scorer is not a world object, so the nodes show that pin and the compile fails while it is empty.
The grant is directional: Source gives Target. Teams grant it in both directions. If a game grants it one way, put the players in the order your rule needs.
3. Prefer trucks: Score Target
Section titled “3. Prefer trucks: Score Target”Override Score Target. It receives World, Query and Candidate (a struct with the target’s handle, its Distance from the query origin and a Score), and returns a Fixed Point. The highest score wins, and equal scores fall back to entity order. The default returns the query’s Range minus the distance, which is positive and larger for nearer targets.
- Right-click the function entry and choose Add call to parent function. The result is the default score.
- Has Tag with Entity Handle the candidate’s Target and Tag
PreferredTag. Wire World in as above. - Select
PreferenceBonusif true, and Fixed-point Zero if false. Add it to the parent’s score and return it.
Pick the bonus against the scale of the scores. With a Range of 900, nearer-is-better scores run from 0 to 900. A bonus of 500 means a Truck beats a non-Truck unless the non-Truck is at least 500 units closer. A bonus above the Range means a Truck always wins. A bonus of 0 is the default behavior.
Compile and save.
4. Attach it to the query
Section titled “4. Attach it to the query”A query’s Scorer Class says which scorer to use. It is a soft class path. Empty means the built-in scorer.
Make the query once and keep it in a variable, so TryShot and the auto-attack share it:
- On
SA_Attack, add a variableTargetQueryof type Sein Target Query. - Compile, then in its Default Value in the Details panel, set Scorer Class to
TS_NoAlliesPreferTrucks. It is a class picker that lists only Target Scorers. Leave the other members for now. - Where the graph made the query with Make Sein Target Query, use
TargetQueryinstead and fill the per-use members with Set Members in Sein Target Query: Instigator Owner Entity, Range, and Required Component. The members you do not set keep their defaults, including Scorer Class. - Use the same variable for Check Target and Find Targets.
SA_AutoAttackinherits it fromSA_Attack.
The scorer applies to Find Targets and Check Target alike. For an ally, Check Target returns Rejected By Scorer.
5. Test
Section titled “5. Test”Set up one Soldier (the attacker, Range 900) and, from its player’s point of view:
| Entity | Owner | Distance | Default score | With the scorer |
|---|---|---|---|---|
| Soldier | An ally | 100 | 800 | Skipped |
| Soldier | An enemy | 300 | 600 | 600 |
| Truck | An enemy | 700 | 200 | 700 |
Play. The attacker should fire at the enemy Truck first, though the enemy Soldier is much closer. Kill the Truck and it turns to the Soldier. It never fires at the allied Soldier, even when that is the only thing in range.
To see the order, call Find Targets with Max Results 3 from a debug ability and print each candidate’s Target and Score. Expect Truck 700, then Soldier 600.
Troubleshooting
Section titled “Troubleshooting”- It still fires at allies. The players are not on the same team, so no Present As Friendly grant exists. Or the query’s Scorer Class is empty, so the built-in scorer ran: check the variable’s default and that every query uses it.
- It ignores the Truck preference. The Truck’s identity tag is not
SeinARTS.Unit.Truck, orPreferredTagis wrong. Has Tag also matches child tags. - A Truck wins from far away. The bonus is large compared to the Range. Lower it.
- The compile reports an empty World Context Object pin. Wire the World input into it.
- Data validation fails the scorer. It writes a variable, or uses a float, a random or a time node. A scorer must be pure and deterministic. Remove the write, and use fixed-point nodes.
- Results seem to vary with the query radius. The framework may evaluate a scorer a different number of times depending on the radius. A scorer that counts its calls or stores state sees different answers. Keep it free of state.
- Neutral entities are targeted. This scorer refuses allies and the asking player’s own units only. A neutral candidate, such as a mineral rock, still passes. Exclude it with the query’s Required Component or Required Target Tags.
Related
Section titled “Related”- Add Combat for the attack ability
- Collision and Extents for position queries
- Fog of War for the line-of-sight gate
- Target Scorer, Target Query and the Targeting Library