UActorComponent::GetWorld
#include "Components/ActorComponent.h"
Access: public
Specifiers: virtualoverrideinline
Description
Returns the UWorld that this component belongs to, or null if the component has not been registered with a world. Prefer this over GetOwner()->GetWorld() as it avoids a null owner dereference.
Caveats & Gotchas
- • Returns null for components that are not yet registered (e.g. freshly constructed via NewObject before RegisterComponent is called). Always null-check the result.
- • Marked final — you cannot override this in a subclass. The cached WorldPrivate pointer is set during registration and cleared on unregistration, so the result can change during a component's lifetime.
Signature
virtual UWorld* GetWorld() const override final { return (WorldPrivate ? WorldPrivate : GetWorld_Uncached()); } Return Type
UWorld* Example
Spawning an actor from a component C++
void UMyComponent::SpawnEffect()
{
UWorld* World = GetWorld();
if (!World)
{
return;
}
FActorSpawnParameters Params;
World->SpawnActor<AMyEffect>(EffectClass, GetOwner()->GetActorTransform(), Params);
} See Also
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?