Download

Painting myself into a corner with GetGameInstance()

I am just trying to do what seems like the simplest thing, load some resources into a scene component so they are in memory before beginplay(). After many methods that resulted in crashes I finally got it working inside the gamemode constructor, only to find I cant GetGameInstance() , the world is used to get it and that is null or something, it crashes on editor start. How am I supposed to load things when gamemode is created and how do I get gameinstance there so I can get the global variables?

If you look into GameplayStatics’ function to get the GameInstance you’ll see it isn’t all that complicated;

GameMode is an Actor, so you can use it to call GetGameInstance().

Just make sure you’re not calling it from a default object.

Here this image will make it crystal clear what I mean, from the gamemode constructor you get null for the world, you cant even call getgameinstance without a crash.

Of course you can’t.
You are calling it within Constructor of Class Default Object which is called on Editor startup while no World exist;

You have to make sure the World exist and make sure you don’t use on Class Default Object’s Constructor.

if ( HasAnyFlags( CPF_ClassDefaultObject ) )
return;

if ( GetWorld() )
GetWorld()->GetGameInstance();

I thought putting it in gamemode::init() would not work but it appears to work with LoadObject<> even if you can’t use constructorhelpers:: so I’ll do that till I hear of a better way.