Search Unity

Using ISharedComponentData to store something like "time elapsed"?

Discussion in 'Entity Component System' started by Buretto, Apr 25, 2019.

  1. Buretto

    Buretto

    Joined:
    Mar 23, 2015
    Posts:
    49
    I need a way to store something like "time elapsed", which would be unique to each "shared index". Right now I have each individual entity store it in an attached IComponentData, but this seems wasteful as each entity will have the same time value as other entities belonging to the same "shared index" chunk.

    I read this in the docs:
    "SharedComponentData should change rarely. Changing a SharedComponentData involves using memcpy to copy all ComponentData for that entity into a different Chunk.."

    So I definitely would not want to do that.
    I couldn't find much information about SetChunkComponentData, but perhaps it is what I need? If someone could point me to a thread or doc on what it is about, it would be much appreciated.

    Or maybe there is a simpler way of doing this that I cannot think of?
    Here is a brief to specifics of my situation, to maybe give a better picture:

    • I have an ISharedComponentData, "SubordinateSpawner", which contains the information on what prefab to spawn, where, and how often,etc..
    • I have a system that operates on all entities that have it
    • The system needs a "time elapsed" value that tells the system how long it has been since last spawn, and if it has been > rate then it instantiates a prefab, and resets it back to 0
    • The "time elapsed" value needs to be unique to each different "SubordinateSpawner", because the spawn rates may be different and thus the "time elapsed" should be reset at different times
    Thank you all in advanced :)
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    I think what you want is an entity reference in your SubordinateSpawner that has your TimeElapsed IComponentData.
     
  3. Buretto

    Buretto

    Joined:
    Mar 23, 2015
    Posts:
    49
    @DreamingImLatios I think that will work! I'm guessing as long as the entity doesn't move chunks, the reference will stay valid. If this is true then I can see so many ways I can use this. I'll give it a go, thanks!
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Doesn't matter if the entity moves chunks. An Entity is a stable reference. Using something like ComponentDataFromEntity will always work regardless of where the entity with the TimeElapsed component moves to.
     
    Buretto likes this.