UWorld and APlayerController

First of all, getting a pointer to a player controller in BP, is very easy. But it is troublesome in C++. I read other posts on how to do it. For instance, via GetWorld()->GetFirstPlayerController(). When I called this via a UObject derived object during execution of a custom event, GetWorld() always returns null. If I used UGameplayStatics::GetPlayerController(), it will require a valid pointer to UWorld object. If I tried to execute GEngine->GetWorldFromContextObject(), with its parameter set to “this” (the current object derived from UObject), then it will also be invalid.

In short, is it impossible to get a pointer to APlayerController (local player) or UWorld from any UObject-derived object, via any standard UE libraries, even though it is executed inside a custom event inside an AActor-derived object?

Or is this a bug?

Hey Pelangi,

The reason behind this is that UObject classes do not exist in the world, as they are more used for data storage and other functions that do not require access to the world.

While it is possible to get a world reference from an actor and store it in your UObject class, I’d recommend using an actor instead, as the performance difference is not noticeable and it will save you from having to do extra work.

Let me know if you have any further questions.

Have a great day