Why use GameInstance::Init()?

The question is simple: Why use GameInstance::Init() instead of using the instance constructor?

1 Like

Init() in GameInstance is something like BeginPlay in Actors.

In UE4 no matter if this is UObject or AActor you can use constructors safely only for initializing variables or doing things that must be done explicitly in constructors (like creating components in actors).

Creating objects, spawning objects, filling arrays with objects etc. are concidered unsafe in constructors and must be done via Init(), BeginPlay() or other function designed for it.

UE4 uses heavily modified C++ with its own set of rules. Constructors are not always called when spawning objects as they use CDO, which is something like master object used for cloning other objects.

Lets just say - the best practice is to use UE4 functions as they meant to be used to avoid future problems :smiley:

2 Likes

This question more like what is the difference between constructor and initializer.

The initializer lets you set obj’s properties after the obj has been constructed, but before that you can do a lot anything else. And the initializer could be failed by certain reason, but constructor not.

So the constructor is about if the object exist in the world, and the initializer is about if the object start running properly.

1 Like

Thanks! Super clear and helpfull!

Thanks for the prompt answer! Super clear and helpfull :slight_smile: