PostInitializeComponents() vs BeginPlay()

Testing the two in my project, I’ve found that I can use them pretty much interchangeably and it has no noticeable effect on the game. The only difference I’ve found is that PostInitializeComponents() gets called right before BeginPlay(), but I can move all the initialization code to the top of BeginPlay() and it seems to do the same thing.

Can someone explain the main differences between the two, and give me a scenario in which PostInitializeComponents() should be used?

1 Like

I am pretty much a newbie so got nothing to add except this link.

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/ActorLifecycle/

1 Like

I have found a difference Property initialized from Cast becomes null ? - C++ Gameplay Programming - Unreal Engine Forums

PostInitializeComponents() is called when the object is created and all of it’s components have been registered and initialized. It’s a good place to do setup because you know it’s called as soon as the actor is ready to be used and called before any networking operations have run. Bear in mind that this will also run when the object is opened in a blueprint editor for example, so sometimes you may need to filter out what you’re doing with a simple check:



const UWorld* lWorld = GetWorld();
if (lWorld && lWorld->IsGameWorld())
{
    // Do game setup
}


BeginPlay() is called at varying times depending on the environment and networking setup. In my experience it’s more reliable to do setup in PostInitComps, as you know exactly when it’s called.

6 Likes