I am using AI perception for my NPC AI but when a character is killed the other AI characters still register it in their perception. Is there a way to make a pawn not be perceived?
In C++ all you need to do is to call UAPerceptionComponent::UnregisterSource
. This function however is not exposed to BP, so if you want to do it in BP you’ll need to put more work in.
You need to make pawns not auto register for AIs’ sight sense. To do that add following to your DefaultEngine.ini:
[/Script/AIModule.AISense_Sight]
bAutoRegisterAllPawnsAsSources=false
And then everything that you want observable by AI needs to have a AIPerceptionStimuliSourceComponent
. That component has a BP function for unregistering from AIPerceptionSystem.
Cheers,
–mieszko
Hello,
Is this still a valid solution? I’m having trouble getting it to work. I’ve added this flag to my DefaultEngine.ini file but all pawns are still being registered as sight sources.
Thanks,
Um I know its like 2 years later, but for future reference. This code should now be moved to DefaultGame.ini
I added an extra line just for safty, not sure if bAutoRegisterAllPawnAsSource = false will work just fine. Will appreciate to anyone can point that out for me.
[/Script/AIModule.AISense_Sight]
bAutoRegisterAllPawnsAsSources=false
bAutoRegisterNewPawnsAsSources=false
[/Script/AIModule.AISense_Hearing]
bAutoRegisterAllPawnsAsSources=false
bAutoRegisterNewPawnsAsSources=false
Confirmed, UE4.27.2:
[/Script/AIModule.AISense_Sight]
bAutoRegisterAllPawnsAsSources=false
[/Script/AIModule.AISense_Hearing]
bAutoRegisterAllPawnsAsSources=false
In DefaultGame.ini works.
Unfortunately, this function seems to have been deprecated. If anyone wants to achieve the same result without having to switch off auto registering for pawns, the following seems to work for me:
UAIPerceptionSystem::GetCurrent(GetWorld())->UnregisterSource(*ActorToUnregister, UAISense_Sight::StaticClass());
Don’t forget to dereference your pawn!
It works fine in UE4.27.2 as long as you tick “Auto-register as source” in the AI perception system of the AIPerceptionStimuliSource component.
This no longer works in UE5.3.2 so far as I can tell. If anyone knows how to make it so pawns are not automatically registered let me know.
I know the question is a bit old, but this is how I do it (I’m currently using 5.3.2)
Extend your pawn from a c++ pawn class, create a method like this, and call it when your pawn is killed.
void AYourPawnClass::UnregisterSource()
{
UAIPerceptionSystem* PerceptionSystem = UAIPerceptionSystem::GetCurrent(World);
if (IsValid(PerceptionSystem))
{
PerceptionSystem->UnregisterSource(*this);
}
}
I also added the following piece of code in the controller, some calls might not be necessary but it makes stop all AI related stuff
UAIPerceptionSystem* PerceptionSystem = UAIPerceptionSystem::GetCurrent(World);
if (IsValid(PerceptionSystem))
{
PerceptionSystem->UnregisterSource(*this);
}
if (IsValid(PerceptionComponent))
{
PerceptionComponent->SetComponentTickEnabled(false);
PerceptionComponent->UnregisterComponent();
PerceptionComponent->Deactivate();
PerceptionComponent->DestroyComponent();
}
if (IsValid(BrainComponent))
{
BrainComponent->SetComponentTickEnabled(false);
BrainComponent->StopLogic(TEXT("UnregisterPerception"));
BrainComponent->Deactivate();
BrainComponent->DestroyComponent();
}
Note that this (below) does NOT work properly in UE 5.4 on macOS. Haven’t tested on Windows.
[/Script/AIModule.AISense_Sight]
bAutoRegisterAllPawnsAsSources=false
bAutoRegisterNewPawnsAsSources=false
[/Script/AIModule.AISense_Hearing]
bAutoRegisterAllPawnsAsSources=false
bAutoRegisterNewPawnsAsSources=false