Garbage collection or Actor lifecycle


#1

As we recently learned SK Requires UE4 Asset References to spawn Actors..

There was a hint that garbage collection and actor life cycle was a separate topic.

If we use the same examples:
Example 1 (Part 1: Spawning)
A map named Level1 is created (in UE4).
BP_Horse is dragged into the level. BP_Horse is now referenced in Level1.
We try to spawn BP_Carrot (In SK) but fail because it is not referenced.
In BP_Horse we create an Actor variable which is set to BP_Carrot. Now BP_Carrot is referenced.

Example 1 (Part 2: Destroying)
What happens in SK to BP_Carrot if BP_Horse is destroyed in UE4?
What is the proper sequence to destroy an object using SK?

Since coroutines are an important part of SK, is there something we need to worry about as far as having coroutines running with references to “objects” that were destroyed before we have a chance to do whatever we need to do with them?

So the overall question revolves around: What are the proper steps to take care of garbage collection and actor life cycle in SK?
(Any specific Gotchas we should know about?)

Do you have a helper class suggestion similar to the one for spawning?


#2

Everything remains referenced and nothing else happens. This example would follow the text book actor lifecycle.

See helper class below.

Just make sure to check references for validity. For instance if BP_Horse were destroyed and BP_Carrot did something with the horse:
!loc : @my_horse.actor_location

This would crash after the horse is destroyed. So get used to checking the pointer before using it:

if @my_horse.valid?
[...
]

I do. Although you can call @some_actor.destroy_actor on any actor, I wouldn’t recommend calling this on actors that have :sk: code running. In :ue4:, actor destruction is not instantaneous, when you call destroy actor, the actor is put into a PENDING_DESTROY state and then typically a frame later is actually taken out of the world.

What this often means for objects that have :sk: coroutines running is that once the object is destroyed, they tick for 1 more frame but with a bunch of their internal data structures nulled out. The way this will manifest is that you will see crashes in your actor because this is null or because actor_location cannot be called on a null actor.

This helper method destroys the actor and then immediately aborts all coroutines on the actor to avoid this scenario.


#3

Awesome info. Thanks!