// Loop to spawn each block
for(int32 BlockIndex=0; BlockIndex<NumBlocks; BlockIndex++)
{
...
// Spawn a block
**APuzzleBlock* **NewBlock = GetWorld()->**SpawnActor**<APuzzleBlock>(BlockLocation, FRotator(0,0,0));
// Tell the block about its owner
if (NewBlock != nullptr)
{
NewBlock->OwningGrid = this;
}
}
What happens to these pointers?
1 - They are not referenced in any UPROPERTY().
2 - Are they garbage collected?
3 - Would it be a good practice to add them to an TArray? Would it make any difference?
Well, assuming you’re locally scoped there they are thrown out. The actor will exist in the world until the next garbage collection and then since nothing is referencing it, they will be collected.
1 & 2 - Yes, because there isn’t a GC controlled stored reference to the actor they will be garbage collected at the next GC.
3 - Yes a UPROPERTY() TArray is a fine place to store them to stop them from getting GC’d. It’s not the old way but it’s the simplest.
4.- A couple of things can effect their lifespan. As above, since there is no reference they will be collected at the next GC. Also, all actors have a lifespan (see AActor::InitialLifespan). If this is > 0 then the actor will only be alive for that period of time. The actor can call out of the world, or you can manually destroy the actor.
They won’t. Spawning an actor places it in the level and it will stay referenced by that level - it won’t be destroyed until you explicitly destroy it or until it destroys itself (via lifespan etc.)
OMG, Thanks for point that out, you are totally right. UObjects need to be referenced, AActors spawned in to the world are already referenced by the world and I need to stop answering questions after 10pm