Where to get the WorldContextObject when World is null?

Hi,

As the subject says… it is only problematic when we are doing our things in C++. Ie in C++, GetWorld() returns null when the client cannot connect to server. Or should I open a level first?

Btw, in BP it is just straightforward - you can always execute openconsole (c++ ConsoleCommand) node even though no world is loaded. Seems so unfair lol. There must be something extra in BP that enable it do so.

Apologize if this sounds like a total noob question.

A valid WorldContextObject can be any actor or UObject afaik. AActor->GetWorld() should work fine?

Probably in different class yes (I forgot to write that) but here in from within inherited UGameEngine. However day suddenly turn brighter and I have got the solution the minutes I posted this… lol… I guess this will work regardless of the class you are working on:-



UWorld* World = GEngine->GetWorldContexts()[0].World();


6 Likes

I’ve been looking for a correct way to do this for 12 hours now, you saved me! Thank you!

Glad it helps you.
I even forget that I solved this problem, and a look at my code… yes indeed I implemented it… lol.
One of the surest and best way to learn all this ‘quirks’ is by looking at the bp equivalent and double click where the corresponding UE4 c++ code will be revealed.

This was amazing! I’ve been stuck for days trying to figure this out!

For those wondering, I’ve tried the following method but it kept crashing UE since they were returning NULL

UWorld* World = GEngine->GameViewport->GetWorld();

You must check GameViewport for nullptr before use it.