# Unable to AddDynamic on AIPerception Component

**URL:** <https://forums.unrealengine.com/t/unable-to-adddynamic-on-aiperception-component/455857>\
**Category:** AI\
**Tags:** aiperception, perception-component, unreal-engine, CPP, question\
**Created:** [September 23, 2019, 11:48pm UTC](https://forums.unrealengine.com/t/unable-to-adddynamic-on-aiperception-component/455857 "2019-09-23T23:48:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr.Pathetic](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mr.Pathetic](https://forums.unrealengine.com/u/Mr.Pathetic)\
**Post date:** [September 23, 2019, 11:48pm UTC](https://forums.unrealengine.com/t/unable-to-adddynamic-on-aiperception-component/455857/1 "2019-09-23T23:48:39Z")

</div>

I am trying to get my head around the AI Perception Component in C++. I’m getting the idea, but I’m stuck at adding a dynamic for the OnPerceptionUpdated event. Every bit of code that I have seen does something like this:

```
AFPSGuard::AFPSGuard()
    {
     	// ...
    	AIPerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &AFPSGuard::OnSenseUpdated);
    }
    
    void AFPSGuard::OnSenseUpdated(TArray<AActor*> DetectedActors)
    {
          // ...
    }

```

However, this happens when I do that:

 ![288206-bugimage.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/6/e/46e5f9162b2fc2d2a863984c633747b7e1cfe25e.png)

Some solutions I found said that this happens when you don’t use the correct arguments, but every example I have found uses a TArray as the only argument.

I would appreciate any help. I can’t seem to find the problem on my own.

---

<div class="post-metadata">

**Author:** ![Mr.Pathetic](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mr.Pathetic](https://forums.unrealengine.com/u/Mr.Pathetic)\
**Post date:** [September 23, 2019, 11:56pm UTC](https://forums.unrealengine.com/t/unable-to-adddynamic-on-aiperception-component/455857/2 "2019-09-23T23:56:51Z")

</div>

I found this answer earlier: [AddDynamic not working with AI Perception Component - AI - Unreal Engine Forums](https://answers.unrealengine.com/questions/505374/view.html)  
It says to pass in a TArray, which is what it expects. It is strange, because if I replace the TArray in my code for just TArray, the error goes away. Clearly, though, the engine does not recognize the TArray as it does not specify a type. This kind of makes me think that it could be a bug in the engine version I’m using. I’m not sure, though, which is why I would like to know if something in my code is incorrect.

---

<div class="post-metadata">

**Author:** ![Mr.Pathetic](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mr.Pathetic](https://forums.unrealengine.com/u/Mr.Pathetic)\
**Post date:** [September 24, 2019, 12:07am UTC](https://forums.unrealengine.com/t/unable-to-adddynamic-on-aiperception-component/455857/3 "2019-09-24T00:07:32Z")

</div>

Nevermind, I found the solution. Seems like in earlier versions you could just pass the array of actors. However, you are now required to pass in a constant reference to the array. So the function should look like this:

```
void AFPSGuard::OnSenseUpdated(const TArray<AActor*>& DetectedActors)
{
      // ...
}

```

Also, in case it helps anyone, don’t forget that you need to add to declare the method with UFUNCTION() on top, or else it won’t compile:

```
UFUNCTION() 
void OnSenseUpdated(const TArray<AActor*>& DetectedActors);
```
