RealDocs

AActor::ForEachComponent

function Engine Since 4.14
#include "GameFramework/Actor.h"
Access: public Specifiers: const

Description

Iterates every owned component of the given type (or all UActorComponent instances when no type parameter is specified) and invokes the provided callable on each. Avoids heap allocation by using the internal set directly.

Caveats & Gotchas

  • Do not add or remove components inside the lambda — modifying OwnedComponents while iterating it causes undefined behavior.
  • The untyped overload (no ComponentType template parameter) visits every non-null owned component. If you only need a specific type, supply ComponentType to avoid the extra IsA check overhead.
  • Unlike GetComponents(), ForEachComponent does not produce an output array — use GetComponents when you need to store or sort results.

Signature

template<class ComponentType, typename Func> void ForEachComponent(bool bIncludeFromChildActors, Func InFunc) const

Parameters

Name Type Description Default
bIncludeFromChildActors bool If true, recurse into ChildActor components and visit their components as well.
InFunc Func A callable (lambda or functor) that accepts a pointer to ComponentType.

Return Type

void

Example

Disable tick on all audio components C++
GetOwner()->ForEachComponent<UAudioComponent>(false, [](UAudioComponent* AC)
{
    AC->PrimaryComponentTick.bCanEverTick = false;
});

Version History

Introduced in: 4.14

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.