Destroy actor vs hide actor

Hi. I 'm making some blueprints where I spawn AI 's. After I kill them, I hide them and disable their collision, so I can count them and move on the next part of the game.

1)is that method ok?
2)should I destroy the AI 's after I accomplish my goals? Do they affect the game if they stay hidden?

Hi,

1)is that method ok?

Destroying many actors at once may cause spkes, so you can disable them instead if you want to. Especially if you want to reuse them, instead of spawning new ones.

I hide them and disable their
collision

That is not enough. You need to disable tick and all timers on both the pawn and the AI controller and also disable tick and deactivate each and every component. Otherwise CPU performance cost will still be there.

Do they affect the game if they stay
hidden?

That mainly depends on the logic you have build. If you use something like “GetAllActorsOfClass” then those disabled actors will of course show up there as well. Also object references to them won’t get invalid. Further every time you use “GetAllActorsOfClass” it will loop through each and every actor you have in your world. Therefore if you have a couple of thousand disabled actors in your world that will of course increase the performance impact of “GetAllActorsOfClass” (but then you should generally be careful using this node during gameplay).

1 Like

Depends on the complexity of the actor … as Chrudimer noted. Personally I destroy actors.

Thanks for your helpful answer!

They are pretty simple. Basically these AI’s are just for the tutorial of the game. Probably I 'll destroy them anyway. Thanks for your answer!!

After I kill them, I hide them and
disable their collision, so I can
count them and move on the next part
of the game.

  • if you do it one by one, you should consider destroying them if you have no plan of recycling them back into the game. It’s a memory footprint friendly approach.

  • if you must destroy a huge amount in one go, doing it in one frame can make the Garbage Collection go into a hiccup mode and it might be noticeable during gameplay. An easy solution is to disable them fully as @chrudimer pointed out and then destroying some of them each second / a couple each tick. Essentially, spread the workload over some time.

  • if your game is small, it probably does not matter much

  • if you load another level, it probably does not matter much, they’ll be wiped anyway


If you’re not destroying them because you need to count them, consider the following:

You can use the Destroyed event to count things. Best of both worlds.

3 Likes

Thanks for your answer!!