RealDocs

UObject::GetWorld

function CoreUObject Since 4.0
#include "UObject/Object.h"
Access: public Specifiers: virtualconst

Description

Returns the UWorld this object belongs to. The base UObject implementation returns null; gameplay classes (AActor, UActorComponent, etc.) override this to return the correct world.

Signature

virtual UWorld* GetWorld() const

Return Type

UWorld*

Caveats & Gotchas

  • The base UObject implementation always returns null unless the outer chain leads to a UWorld. Do not assume a non-null return without checking.
  • During CDO construction GetWorld() returns null. Calling it in a constructor is a common crash source.
  • In multi-world PIE scenarios, GetWorld() returns the specific world for this object instance, not the global GWorld. Prefer GetWorld() over GWorld in all gameplay code.

Example

Safe world access pattern C++
void UMyComponent::DoSomething()
{
	UWorld* World = GetWorld();
	if (!World) { return; }
	World->SpawnActor<AActor>(MyClass, SpawnTransform);
}

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.