It is often the case that we want to execute custom logic when actor is being destroyed. Most often people use OnDestroyed blueprint event or Destroyed native function. But is this the best solution?
A good reason why you you should always, and especially with With World Partition maps, pair any initialization code from BeginPlay with cleanup in EndPlay is because WP maps turn off s.ForceGCAfterLevelStreamedOut, meaning that levels can be made invisible (all actors call EndPlay) and then made visible again (all actors call BeginPlay) multiple times before actors actually get garbage collected and OnDestroyed gets called.
That means that actor lifecycles are:
Construction -> BeginPlay -> EndPlay -> N * (BeginPlay -> EndPlay ) -> OnDestroyed.
A call to BeginPlay will always be paired with an EndPlay call. Construction and Destruction only happen once each.
For smooth world streaming, projects using classic levels and level streaming might also turn off this cvar manually to get rid of full purge garbage collections which cause hitches. Studios sometimes do that (and I actually recommend it). So this can also apply outside of WP levels 🙂
A good reason why you you should always, and especially with With World Partition maps, pair any initialization code from BeginPlay with cleanup in EndPlay is because WP maps turn off s.ForceGCAfterLevelStreamedOut, meaning that levels can be made invisible (all actors call EndPlay) and then made visible again (all actors call BeginPlay) multiple times before actors actually get garbage collected and OnDestroyed gets called.
That means that actor lifecycles are:
Construction -> BeginPlay -> EndPlay -> N * (BeginPlay -> EndPlay ) -> OnDestroyed.
A call to BeginPlay will always be paired with an EndPlay call. Construction and Destruction only happen once each.
I haven't had an opportunity to use World Partition yet and had no idea it works like this. Great insight, thanks for sharing!
For smooth world streaming, projects using classic levels and level streaming might also turn off this cvar manually to get rid of full purge garbage collections which cause hitches. Studios sometimes do that (and I actually recommend it). So this can also apply outside of WP levels 🙂