How-To: React to Team Changes

Goal: update gameplay and UI the moment an actor's team or hostility changes - no polling.

Prerequisites
An actor with a team agent component (Quick Start).

1. Per-agent change delegates

Each component exposes OnTeamTagsChanged and OnHostileTagsChanged (both fire with the owning actor and the old/new tags). They fire on the server when you mutate, and on clients when the change replicates - so listeners work everywhere.

cpp
Team->OnTeamTagsChanged.AddDynamic(this, &UMyComp::HandleTeamChanged);
void UMyComp::HandleTeamChanged(UObject* ChangedActor,
const FGameplayTagContainer& OldTags, const FGameplayTagContainer& NewTags)
{
// recolor nameplate, retarget, etc.
}

2. Subsystem-wide attitude hook

After any agent changes and the system has refreshed AI perception, UCrimsonTeamSubsystem::OnTeamAttitudeChanged(ChangedActor) broadcasts once. Bind it for custom AI/UI re-evaluation that should run whenever anyone's attitude shifts.

cpp
GetWorld()->GetSubsystem<UCrimsonTeamSubsystem>()
->OnTeamAttitudeChanged.AddDynamic(this, &UMyAIService::HandleAttitudeChanged);

3. Blueprint async observers (for UI)

For widgets, the async nodes are simplest: they fire immediately with the current state, then on every change. Cancel by calling Cancel() on the returned action.

In the widget's Event Construct: Observe Team Colors (Team Agent = the PlayerState you are watching) -> On Team Changed -> Is Valid (Branch) on the Display Asset output -> Apply To Material (Material = your nameplate's dynamic material instance). The On Team Changed pin also carries Is Part Of Team and Team Tags if you want to hide the plate for an untagged actor. Keep the returned async action in a variable and call Cancel on it from Event Destruct.

Verify
Flip a team in the Dev Tools (or via a cheat) and watch the listener fire once, with the new tags.

See also

  • Concept: Dynamic Attitude & Multiplayer - what happens between the mutation and your delegate firing
  • How-To: Declared Teams & Colors - driving team-colored visuals