So, as I have finished this lecture I’ve noticed that none of the spawned actors were being deleted when the tile is. It poses a problem since we don’t want to have a bunch of actors needlessly sitting around in the scene, taking up memory and affecting performance.
I’ve started trying to fix that problem, thinking it would be a breeze. At first, I tried to get all children of our tile and delete them when the player collides with the “Destroy Volume”. It didn’t work. At all. After some surfing, I realized that maybe we needed to find not the children of the tile itself, but the components attached to the root, and delete them. Did just that and it worked… Only partially though. For some reason, it was deleting some objects and leaving all the other untouched, even though they were obviously attached to the root component (Shared Root).
After a bit of head scratching I was on the verge of throwing in the towel and calling in the L, but then I realized that we can do something in the C++ when the tile is being or is destroyed. Looked up some docs and overridable functions I’ve overridden BeginDestroy() function, but it gave me crashes when I tried compiling. Changed it to Destroyed(), got all the spawned objects to be stored in a private TArray, written some code to destroy all of them when the actor is destroyed, and voila, the miracle happened. Now all the actors were destroyed with their respective tiles.
Long story short, here is my solution for the problem:
In the Tile.h we override the Destroyed() method and create a private TArray of actor pointers to store the spawned actors in
in the Tile.cpp, at PlaceActors method we add actors in the array every time they spawn
and in the definition of a Destroyed() method, we go through every single spawned actor and destroy them.
Now, every time when we hit the volume that destroys the Tile object, after being destroyed it destroys all the objects that spawned under that instance of a tile. Hope this is going to help someone to solve the problem a bit faster than I’ve been torturing it. Happy codin’ folks!