RealDocs

AActor::ForEachComponentOfActorClassDefault

function Engine Since 5.0
#include "GameFramework/Actor.h"
Access: public Specifiers: static

Description

Iterates every CDO component of the given type on an actor class (including Blueprint-added ones) and calls InFunc for each. InFunc returns bool to allow early exit.

Caveats & Gotchas

  • The underlying implementation of GetActorClassDefaultComponents and GetActorClassDefaultComponent — both call this function internally. Use this directly when you need early-exit logic or want to avoid allocating a result array.
  • TFunctionRef does not own the callable — the lambda or functor must remain alive for the duration of the call. Do not capture values that go out of scope before the iteration finishes.
  • Iteration order matches the component template order in the CDO, which may differ from runtime OwnedComponents order.

Signature

static void ForEachComponentOfActorClassDefault(const TSubclassOf<AActor>& InActorClass, const TSubclassOf<UActorComponent>& InComponentClass, TFunctionRef<bool(const UActorComponent*)> InFunc)

Parameters

Name Type Description Default
InActorClass const TSubclassOf<AActor>& The actor class whose CDO to inspect.
InComponentClass const TSubclassOf<UActorComponent>& Only visit components of this type.
InFunc TFunctionRef<bool(const UActorComponent*)> Callable invoked per component. Return true to continue iteration or false to stop early.

Return Type

void

Example

Find the first audio component with a specific sound C++
const UAudioComponent* Found = nullptr;
AActor::ForEachComponentOfActorClassDefault(
    MyActorClass,
    UAudioComponent::StaticClass(),
    [&Found, TargetSound](const UActorComponent* Comp) -> bool
    {
        const UAudioComponent* AC = CastChecked<UAudioComponent>(Comp);
        if (AC->Sound == TargetSound)
        {
            Found = AC;
            return false; // stop early
        }
        return true;
    });

Version History

Introduced in: 5.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.