#unreal

Used for detection by affiliation in AI Perception System

The AIController class implements this interface, but the implementation is mostly nonfunctional unless you override GetTeamAttitudeTowards in your own AI controller (see below for details)

Additionally an FAttitudeSolverFunction is useful for advanced logic, such as teams that may be friendly or neutral to each other.

AI Controller -only implementation

Create a custom AI Controller class, and override GetTeamAttitudeTowards. The actor passed into it will typically be a pawn, so you can simply get its AI Controller, and compare its team id.

virtual ETeamAttitude::Type AMyAIController::GetTeamAttitudeTowards(const AActor& Other) const
{ 
	const APawn* OtherPawn = Cast<const APawn>(&Other);
	if(OtherPawn)
	{
		return FGenericTeamId::GetAttitude(
			GetGenericTeamId(),
			FGenericTeamId::GetTeamIdentifier(OtherPawn->GetController())
		);
	}
 
	return ETeamAttitude::Neutral;
}

This makes the attitude depend on what the team ID of each controller is. You can give each controller a team ID using FGenericTeamId. By default, the attitude will be hostile unless the team ID is equal.

If you want non-pawn actors (f.ex. buildings or such) to be handled, you’ll need to take this into account and provide a way to get the team ID of the actor. The easiest way to do this is to implement the IGenericTeamAgentInterface on the actor, and check for it in your AI controller’s GetTeamAttitudeTowards function.

A more elaborate implementation

You can also implement this interface on any actor such as your character. In that case, you can for example implement the functions of the interface, and forward the calls to the functions on the AI controller currently controlling the pawn. You can also do more advanced logic via overriding the functions with other implementations.

Switching teams/attitudes

If you need to switch to another team, or trigger a different attitude as a result of an action, you need to have the actor switching the team call PerceptionComp->RequestStimuliListenerUpdate().

This triggers the perception system to recreate cached data, and update team affiliation info on perceived actors.

Another alternative is to unregister and re-register the team-changing actor from the perception system, but this may have undesirable effects if other actors are currently perceiving it.

Implementation notes

GetTeamAttitudeTowards

GetTeamAttitudeTowards can be overridden to change logic relating to what attitudes this actor has towards other teams. By default this function simply calls the current FAttitudeSolverFunction.

It may be simpler to override the attitude solver function - The reason is overriding GetTeamAttitudeTowards only affects the particular actor’s own logic - eg. only that actor uses the overridden function to determine team attitudes. Other actors will need their own implementation.

If you need multiple actor types to follow the same team attitude logic, it’s much simpler to create your own FAttitudeSolverFunction. That way you only need to define your team attitude logic in one location.

There may be other reasons to use GetTeamAttitudeTowards, but it’s at least good to keep this in mind.

GetGenericTeamId and SetGenericTeamId

This is used to determine the affiliation of this actor when the perception system is set to perceive different attitudes. You may want to define a Custom TeamID Enum to make this process simpler.

If the actor’s team will always be the same, you can hardcode this to always return the same value. Alternatively, you can have a property in your class to store the team ID, and also override SetGenericTeamId to allow setting it. This is how it’s set up in AI controllers.

See also

  • IAISightTargetInterface - used to customize how sight perception works, such as allowing you to perceive the head of a character instead of its torso.