RealDocs

TArray::FindItemByClass

function Core Since 4.0
#include "Containers/Array.h"
Access: public Specifiers: const

Description

Searches a TArray of UObject pointers for the first element whose runtime class matches (via IsA) the template parameter SearchType. Returns true and optionally fills Item and ItemIndex if found.

Caveats & Gotchas

  • Only works on TArray<UObject*> or TArray<T*> where T derives from UObject — the implementation calls IsA on each element, which requires UClass. Using this on a TArray of raw structs or non-UObject pointers will not compile.
  • Null elements in the array are skipped without asserting, so sparse UObject pointer arrays are safe. However, the function is a linear scan — prefer a TMap<UClass*, UObject*> cache if you call this frequently each frame.

Signature

template<typename SearchType>
bool FindItemByClass(SearchType **Item = nullptr, SizeType *ItemIndex = nullptr, SizeType StartIndex = 0) const

Parameters

Name Type Description Default
Item SearchType** Optional output pointer that will be set to the found element. Untouched if not found. nullptr
ItemIndex SizeType* Optional output index of the found element in the array. Untouched if not found. nullptr
StartIndex SizeType Index from which to start the search. 0

Return Type

bool

Example

Find the first UStaticMeshComponent in an actor's component list C++
TArray<UActorComponent*> Components;
Actor->GetComponents(Components);

UStaticMeshComponent* MeshComp = nullptr;
int32 FoundIdx = INDEX_NONE;
if (Components.FindItemByClass<UStaticMeshComponent>(&MeshComp, &FoundIdx))
{
    UE_LOG(LogTemp, Log, TEXT("Found mesh at index %d"), FoundIdx);
}

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.