AIPerception - Affiliation implementation in C++

Hi everyone,

I’m trying to implement AIPerception by Affiliation (Enemies, Neutrals, Friendlies), but I’m struggling to find the correct way for the implementation in C++

I’m very unexperienced with C++ in Unreal 4 and hopefully someone can point me into the right direction for this. Any help is really appreciated.

The main question is where and how I could set the affiliation. I’m also not sure if it has to be on the character, the controller or both…

Thank you in advance.



You’'ll want to create enums that store different factions or whatever you’re looking for, ie

enum class EFactionType : uint8

And then give each monster or player a Faction. Also add an TArray that the monster is aggressive against. When the enemy sees you, check that your faction is an enemy faction, and if it is, then begin attacking the player. I won’t go into more detail about the AI stuff, because it would take too long, but you’re welcome to view my tutorials if you want some info on AI. You can search Reuben Ward on youtube.

Hope this helps!

Hey Denzel (or Reuben) :slight_smile:

Thx for your quick reply. I had a similar solution in BB using actor tags like friendly, neutral, enemy, … however, this does not feel like the correct/native way to do it and was way too much overhead (in my personal opinion).

We already have a native UE4 solution that is build in and I can’t get my head around how to use it:


The detection by affiliation must be there for a reason? I’ve seen AI tutorials that are more than one year old but everyone just suggests to tick all three detection types when it comes to use it with the AI perception system.

I’ll dig into your videos, maybe I’ll find what I’m looking for.

Thank you & Cheers

Hi Terry,

Take a look inside AIModule/Classes/GenericTeamAgentInterface.h. Basically you need to implement that interface in you characters or PlayerController (as you prefer), AIController already implement that. Override the following function from that interface to get and set the Team of that entity

virtual void SetGenericTeamId(const FGenericTeamId& NewTeamID);
virtual FGenericTeamId GetGenericTeamId() const

Bind a function to OnTargetPerceptionUpdated in you AIController to be notified each time the perception system is updated

UAIPerceptionComponent* PerceptionComp = GetAIPerceptionComponent();
if (PerceptionComp) 
	PerceptionComp->OnTargetPerceptionUpdated.AddDynamic(this, &ABaseAIController::OnTargetPerceptionUpdated); 

Now, in your OnTargetPerceptionUpdated you can get the “Team Attitude” of you AIController toward the senced actor

void AMyAIController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
	IGenericTeamAgentInterface* TeamAgent = Cast<IGenericTeamAgentInterface>(Actor);
	if (TeamAgent)
		ETeamAttitude::Type AttitudeTowards = GetTeamAttitudeTowards(*Actor);

		//Now in AttitudeTowards you have the attitude (Friendly, Neutral, Hostile,) at this point you only need to check and do wetherever you want based on that attitude. if is Hostile, attack, if is Neutral, ignore etc ... For example:
		// Don't do anything if the attitude towards sensed actor is neutral. 
		if (AttitudeTowards == ETeamAttitude::Neutral)

also you can override GetTeamAttitudeTowards if you want to change how retrieve owner attitude toward the given actor

Hope this help you

Best regards

Remember that both entities AIController and the sensed actor (the entities that implement the interface) should be in some team, not just the sensed actor also you need to define the AIController’s team, based on your gameplay teams logic.

Thank you, that’s what I was looking for.