Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Design advice on pooling entities

Discussion in 'Entity Component System' started by jay3sh, Sep 6, 2023.

  1. jay3sh

    jay3sh

    Joined:
    Dec 30, 2013
    Posts:
    18
    Hi,

    I designed my previous RTS game using traditional Unity architecture (i.e. GameObjects). For my next RTS game I'm using ECS.

    In my previous game, I maintained a pool of gameobjects in order to reuse them. In a typical RTS game there are large number of instances of same enemy type. Over the course they die and new instances of them are spawned. When my units die, instead of calling Destroy() on them, I disable them and the pool reuses them when new instances of same enemy type need to be spawned. This works fairly well. In my new game, the enemy units are entities. I know that this architecture will handle large number of units easily. But I wonder if I should still pool these entities, i.e. instead of DestroyEntity(), I maintain them in a pool and disable them somehow when they die and reuse them when new ones are needed. What do you think?

    Will the answer to this question change if my enemy entities had managed components on them?

    Thanks,
     
  2. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    288
    We pool GameObject because it's **very** expensive to repeatedly instantiate and destroy. Because GameObject is just that expensive. On the other hand, it is pretty cheap to create and destroy entities in bulk. I use batch APIs and perceive no drop in performance on systems that create and destroy entities. Furthermore, the general advice is don't pool entities unless there is no solution to a specific performance problem.

    Is it possible to avoid managed components? Well, I don't use managed components so I don't know if it relates. For me, I currently use a hybrid approach where I maintain a map between entities and gameobjects. I pool the gameobjects, but just destroy the entities.
     
  3. jay3sh

    jay3sh

    Joined:
    Dec 30, 2013
    Posts:
    18
    So far my RTS units are bare bones. For navigation I use Agent Navigation asset, for animation I use GPU ECS baker. To the best of my understanding these assets don't add any managed components to my units. I was considering what to do with particle systems and Audio source components (IIUC they are managed components). As you said, maybe I should maintain them as pooled gameobjects separately and not per unit.