# OnPerceptionUpdated Detecting Dead Actor

**URL:** <https://forums.unrealengine.com/t/onperceptionupdated-detecting-dead-actor/468477>\
**Category:** C++\
**Tags:** aiperception, ai-controller, unreal-engine, question, CPP\
**Created:** [July 21, 2020, 5:49pm UTC](https://forums.unrealengine.com/t/onperceptionupdated-detecting-dead-actor/468477 "2020-07-21T17:49:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinalRockstar](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/finalrockstar/32/88718_2.png) [@FinalRockstar](https://forums.unrealengine.com/u/FinalRockstar)\
**Post date:** [July 21, 2020, 5:49pm UTC](https://forums.unrealengine.com/t/onperceptionupdated-detecting-dead-actor/468477/1 "2020-07-21T17:49:13Z")

</div>

Hi I have a following Case

There are three AI candidates. In **team one** there is one AI and in **team 2** there are two AI candidates. Initially AI of **team 1** senses both AI of **team 2** using **OnPerceptionUpdated** after AI of **Team 1** kills one of the AI of **Team2** then on printing I get name of AI who got dead in other words I am not getting AI who is still in the sight instead I get the AI who was killed and destroyed.Following is my sample code.

```
void AAIController::BeginPlay()
 {
	Super::BeginPlay();
    //Function that will be called When Perception Updates
    AIPerception->OnPerceptionUpdated.AddDynamic(this, &AIController::SenseAllActors);

  }
    
    void AAIController::SenseAllActors(const TArray<AActor*>& detectedActors)
    {

    	for (auto act : detectedActors)
    		UE_LOG(LogTemp, Warning, TEXT("I %s detected %s"), *GetPawn()->GetName(), *act->GetName());
    
    }
```

---

<div class="post-metadata">

**Author:** ![knapeczadam](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@knapeczadam](https://forums.unrealengine.com/u/knapeczadam)\
**Post date:** [July 22, 2020, 3:32pm UTC](https://forums.unrealengine.com/t/onperceptionupdated-detecting-dead-actor/468477/2 "2020-07-22T15:32:48Z")

</div>

What about detectedActors? bestActors is used in range loop.

---

<div class="post-metadata">

**Author:** ![FinalRockstar](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/finalrockstar/32/88718_2.png) [@FinalRockstar](https://forums.unrealengine.com/u/FinalRockstar)\
**Post date:** [July 22, 2020, 4:00pm UTC](https://forums.unrealengine.com/t/onperceptionupdated-detecting-dead-actor/468477/3 "2020-07-22T16:00:25Z")

</div>

Sorry I pasted selected portion of code to avoid confusion I have updated accordingly.

---

<div class="post-metadata">

**Author:** ![FinalRockstar](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/finalrockstar/32/88718_2.png) [@FinalRockstar](https://forums.unrealengine.com/u/FinalRockstar)\
**Post date:** [July 22, 2020, 4:03pm UTC](https://forums.unrealengine.com/t/onperceptionupdated-detecting-dead-actor/468477/4 "2020-07-22T16:03:54Z")

</div>

I solved by using GetCurrentlyPerceivedActors method which detects all the actor in the radius and on passing nullptr it will select actors for all senses.

```
void AAIController::SenseAllActors(const TArray<AActor*>& detectedActors)
{
	UE_LOG(LogTemp, Warning, TEXT("I am sensing"));
	TArray<AActor*> sensedActors;
	AIPerception->GetCurrentlyPerceivedActors(nullptr, sensedActors);
}
```
