AActor::GetComponents
#include "GameFramework/Actor.h"
Access: public
Specifiers: const
Description
Fills OutComponents with all owned components of type T (or all components when called with a UActorComponent* array). A no-arg const overload returns a direct reference to the internal TSet to avoid any allocation.
Caveats & Gotchas
- • Use `TInlineComponentArray<UPrimitiveComponent*>` instead of a plain TArray to avoid heap allocation for actors with only a handful of components: `TInlineComponentArray<UPrimitiveComponent*> Prims(this);`
- • The zero-argument overload (`const TSet<UActorComponent*>& GetComponents() const`) returns a raw reference to the internal set. The comment in the header warns that anything changing component ownership or destroying a component invalidates this reference — do not store it across frames.
- • Blueprint equivalent is `K2_GetComponentsByClass`. Use that node in Blueprints; `GetComponents` is C++ only.
Signature
template<class T, class AllocatorType> void GetComponents(TArray<T*, AllocatorType>& OutComponents, bool bIncludeFromChildActors = false) const Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| OutComponents | TArray<T*, AllocatorType>& | Output array that will be reset and filled with all matching components. | — |
| bIncludeFromChildActors | bool | If true, also include components from nested ChildActor components. | false |
Return Type
void Example
Collect all mesh components C++
TInlineComponentArray<UStaticMeshComponent*> Meshes(this);
// Meshes is now populated — no heap allocation for typical actor component counts
for (UStaticMeshComponent* Mesh : Meshes)
{
Mesh->SetCastShadow(false);
} Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?