How to GetWorld() in the Init() method of a UGameInstance in Standalone?

I created a UCustomGameInstance that extends UGameInstance and overrides the Init() method. I use the Init() method to create some objects at startup, before anything else is created, but in order to do so I need the reference to the UWorld.

In the editor it works fine, but in standalone the UWorld isn’t yet created when the Init() method is called. So, is there a workaround to execute some code before ANY actor’s BeginPlay() method is executed, but at the same time has already the UWorld, in Standalone?

You can’t. The world that it’s getting with the editor open is the editor world, which is shared by the editor and PIE.

You probably don’t want to do this anyway, because as soon as you change world none of the objects will have a valid world anymore.

Thanks for the clarification.

In my case it’s exactly what I want though, since I will be only ever using a single UWorld in my project. As a workaround for now I’m basically calling the GameInstance Init() method from the user’s pawn BeginPlay(), but I would like a “cleaner” solution…

Your best bet is to create them either in a custom World Settings class, or bind a function for FCoreDelegates::PostLoadLevelWithWorld

The second approach seems the easiest choice, but I can’t find that delegate in CoreDelegates.h. I’m using version 4.14, maybe it was added later?

In 4.14 I think it’s just ‘PostLoadMap’ - but in later versions they added the map name as an FString parameter I believe.

You will probably still get weird behavior in PIE just because of how Game Instances work - but launching in Standalone should work.

Found it, thanks!