Search all ActorComponents on Actor

Haven’t had any luck searching this out myself, so I figured I’d drop a line here.

I have two objects in my scene, one is an actor, and the other one is just an object (UObject, but it doesn’t really matter). We will call these A and B, respectively.

In my object B, I want to check if actor A has a component of a given class. In pseudocode, something like this:

function DoSomething
{
    // this is the important part
    ExtraSpecialComponent* FoundComponent = A.FindComponentByType(ExtraSpecialComponent);

    if (FoundComponent)
    {
        FoundComponent.DoCoolStuff();
    }
}

Can someone help me out with an example of searching components on an actor in this way?

ExtraSpecialComponent* foundComponent = A.FindComponentByClass();

That will find and return the first ExtraSpecialComponent in A. If you want to get all ExtraSpecialComponent in A then use GetComponentsByClass or GetComponentsByTag instead.

A potentially more performant way of getting multiple components of the same type from an actor is:

TInlineComponentArray<ExtraSpecialComponent*> foundComponents(A);

Thanks! I gave this some time to mull over and this morning after browsing the API for AActor I was able to find these functions. Good point on getting multiple components of the same type!