is "FindComponentByClass<>()" expensive?

This is very cheap because Epic guys are very clever (in Unity doing that is very expensive).

Unreal will do a “->IsA(…)” check before even casting anything in there.
Within IsA() method there’s a bunch of UE_ASSUME macros that speeds up things even further.

If your concern is bound to RAM (slow), Epic also provide us a workaround you can use inline with TInlineComponentArray<>:



TInlineComponentArray<UStaticMeshComponent*> SMComponets;
Actor->GetComponents<UStaticMeshComponent>(SMComponets);

for (UStaticMeshComponent* Component : SMComponets)
{
    // .....
}


https://docs.unrealengine.com/en-US/…ray/index.html

For custom types other than components you can also use custom allocators and specify the min size you expect, like:



TArray< FmyCustomType , TInlineAllocator<24> > myArray;


7 Likes