I am trying to get my UFO’s AI perception to recognize the player as hostile, but I can’t seem to figure out where I can set it. (I’ve already set my UFO’s Detect by Affiliation to Detect Enemies only).
Even after 2 years this is still not very clear on how to do in BluePrints. How do I set my character or another pawn to be on a different team like hostile? i found this guide…
but it says it can only be done in code, is this still correct?
I’ll try my best to explain using the perception system and having it “know” about hostile actors, at least my understanding of it; I’m using UE 4.27.
- The Pawn class has a reference to a Controller class.
- The Controller class has a reference to the Perception Component.
- The Controller also has to have the Generic Team Id implemented | The AAIController already uses this, but the APlayerController doesn’t (you’d have to implement the IGenericTeamAgentInterface in C++)
- This also means creating a variable to track the GenericTeamId (I wasn’t able to find a way to add this to either the AI or player controller outside of C++)
The issue I found to happen was the Perception Component, when processing the Stimuli was passing in the result of GetOwner() [Which is a controller class] and the Actor that was perceived [Which was a Pawn] to the GetTeamAffiliation() function which took actors for parameters; to me this indicated that the function was passing in a pawn and controller but only the controller was using the Generic Team Id setup.
So looking at this problem of comparing Controllers and Pawns, I see 2 easy ways to fix the problem:
- I can look to edit the engine’s code so that when processing the stimuli the function checks to see if the perceived actor is a Pawn and if so get the controller for it and run the affiliation test against that. But currently I’m hesitant to change editor code. Or…
- Implement the IGenericTeamAgentInterface on the Pawns and have their Team Ids match or reference their own controller.
That way when the Perception code runs the check from the Controller’s team id against the Pawn’s team id, the Pawn can also return it’s Generic Team Id. I’m made the #2 change and tested it, and now I am able to get a list of hostile actors from the blueprint function. Unfortunately the fix needed to be done in C++ code.
You need to do it in C++. Here’s how you would do it If you want to use the default Enemy, Neutral, or Friendly team system unreal has already implemented.
- In your player class, inherit from IGenericTeamAgentInterface
//The two includes below are only recognized if you add “AIModule” to your build.cs list
// PublicDependencyModuleNames.AddRange(new string { “Core”, “CoreUObject”, “Engine”,
// “InputCore”, “EnhancedInput”, “AIModule” });
include “Perception/AIPerceptionTypes.h”
include “GenericTeamAgentInterface.h”
class AYourCharacer : public ACharacter, public IGenericTeamAgentInterface
-
Override this function
virtual FGenericTeamId GetGenericTeamId() const override; -
Create a TeamID UProperty
UPROPERTY(EditDefaultsOnly, Category = “AI”)
uint8 TeamID;
- Define the overridden function in cpp to return TeamID
FGenericTeamId AYourCharacter::GetGenericTeamId() const
{
return FGenericTeamId(TeamID);
}
That’s it! Keep in mind Pawns, AIControllers, and anything that has the AIPerceptionStimuliSource component will be defaulted to neutral.