Garbage Destroy

Hey guys, @ben and @sampattuzzi I don´t know if this is going to be fixed later in the course, but I did it anyways.
I noticed that when we destroy the Tile all the actors stayed there (at least in my game) so that is a lot of memory that is being useless so my solution for this was.

  1. At the Tile.h in the private section i created an array to store the actors that were created in that Tile to destroy them when the Tile is destroy
    //Holds the actors created every tile and get destroy when the tile is destroy
    TArray<AActor *> Garbage;

  2. At place actors you can use that place to add the actors
    if (Spawned)
    {
    Garbage.Add(Spawned);
    Spawned->SetActorScale3D(FVector(SpawnPosition.Scale));
    Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));

  3. This is self explanatory
    void ATile::EndPlay(const EEndPlayReason::Type EndPlayReason)
    {
    if (!ensure(NavMeshBoundsVolume)) { return; }
    Pool->Return(NavMeshBoundsVolume);
    if (Garbage.Num()!=0)
    {
    AActor * Prop;
    while (Garbage.Num() != 0)
    {
    Prop = Garbage.Pop();
    Prop->Destroy();

     }
    

    }

}
4. At this point you will notice that the guns for the AI wont get destroy I think because we attach them at begin play or something, so at Mannequin you will have to do this

void AMannequin::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Gun->Destroy();
}

Hope you find this usefull and tell me if you have something better.

3 Likes

Yes, destroying child actors doesn’t happen automatically. We don’t cover it in the course so well spotted.

Privacy & Terms