I have a problem where, once some enemies have despawned and it starts to re-use old, despawned objects, none of the changes are reset. For example tags, physics settings, AI+enemy variables and more change before the enemy dies. Am I meant to be trying to get then manually change and reset all of this information on the object after it has despawned/while inactive? I feel like surely someone would have wanted to be able to recycle enemies with this many or some untweakable AI changes etc but couldn't find any details short of easy things to reset.
Also if that's something I can't do, I'd appreciate knowing a 'comfortable' max starting pool.
Thats how pooling works, since it reuses game objects. All of the pooling plugins work that way, not just ours. You will need to write code to reset the state for the things that matter to your pooled game objects. The easiest place is to add code to the OnDespawned event that gets called just prior to despawning things. I remember doing this when using PoolManager about 6 years ago and was confused about it before the author let me know it's normal.
As far as a comfortable pool size, there's no way to answer that. Make the pool as big as your game needs and no bigger.
Thanks a lot for the response. I guess at that point for me though, is that the code I'd have to run on despawn would likely void any kind of performance increase from the spawn/despawn process, so then maybe I should use kill instead of despawn and tick the instantiate if run out of preloaded pool assets instead. Or is the kill/instantiate process more significant than something like multiple calls to alter physics settings/animation states etc?
Sorry that's fair enough, unity is just such a messy nightmare for me when trying to workout how to increase performance so I hoped to get an answer here but asked to broadly. The manual showed I believe 50 for the prepool and I wasn't sure if there was a recommended value somewhere I could find, e.g. a pretty simple game graphically but wanting to have comfortable waves of enemies. If as there is now, there was only 1 enemy type, would 100 be okay? I understand though if that's still not an easy thing to answer though.
No way would that script void any kind of performance increase. Not even close. I'm not sure why you would think so. Instantiate is a very slow process, and Destroy causes garbage collection which will kill your frame rate unpredictably. Adding code to "OnDespawned" is event-driven (does not hurt your general per-frame performance because it doesn't run every frame like "Update" code does). Instantiate will be slower the more sub-game objects and scripts you have on each prefab, sure, but it's always orders of magnitude slower than spawning and despawning.
I would say the #1 way to make sure your performance is good is to use pooling. Since it's pretty easy compared to other performance-gaining methods, it's best to start with it already there.
Pool Items are created with a default of just a few items, but you should put as many in there as your spawners need at any one time, or as many as you want to exist at one time. There's no way I can answer that question. A game like Gauntlet might need 150 of each enemy, but a space shooter might only need about 15. It's important not to put it much higher than you need because all those clones need to be Instantiated to be available in the pool before the game can start. If you put 100 each of dozens of different enemies, you might need a 5 second pause to set up pooling on a mobile device. So keep it low and increase as you need more. If you run out of items, Pool Boss will tell you in the Console, and you can increase.
Thanks so much for taking the time to break it down some more. I'd searched but found a lot of things mentioning it should be used but nothing really describing the impact in more detail so I wasn't certain of the cost differences, hence being concerned about what might put that cost out of balance. Now I'll feel more comfortable taking the time to try get the objects state reset.
Yeah, definitely wouldn't want to use more than necessary. Just in my current case, in the context of a time limited game jam I know I'm not going to be able to get through the theory and start testing if my objects are properly resetting, so I hoped to know if something like 100 would be okay. Thanks again a lot for sharing some examples. Post jam thanks to your comments I'll definitely try clean it up by resetting the objects state so I can reduce the necessary pool.
Your responses have been really really appreciated, thanks !
Post by DarkTonic Dev on Jan 10, 2019 17:01:17 GMT
If you don't know how many to put in the pool, you can actually do a few playthroughs leaving "Allow Instantiate More" for each item on and look at the peak (max) of each pool item (it's displayed in Pool Boss) before you stop playing the game. Write those numbers down and change the item count in each pool to those numbers or maybe 10% more, then turn off Allow Instantiate More everywhere.