Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best way to copy entities from one world to another?

Discussion in 'Entity Component System' started by Vuh-Hans, Jan 22, 2019.

  1. Vuh-Hans

    Vuh-Hans

    Joined:
    Sep 24, 2013
    Posts:
    42
    I'm in a situation where I need to replicate entity component data between two worlds. I was hoping there would be something like EntityManager.MoveEntitiesFrom but as a copy, without removing the entities from the original world. I imagine there has to be an efficient way of doing this since entity data is packed in chunks already. Do you guys know if the devs hinted anywhere to something like this in the future?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    I suspect something like this needs an asynchronous method to avoid locking when coping large amount of chunks.

    Without looking at code, my guess is MoveEntities doesn't actually copy any data just changes which world references a chunk so it's probably very very fast.

    Whereas if you had a CopyEntities you might need to copy a lot of chunks which could lock the main thread for 10s or ms causing spikes in the game.

    That said, there are definitely solid use cases for this so an asynchronous version that could do this would be very nice. (I also just be wrong and a synchronous version might be fast enough for most use cases.)
     
    Last edited: Jan 22, 2019
  3. Vuh-Hans

    Vuh-Hans

    Joined:
    Sep 24, 2013
    Posts:
    42
    You might be right, which might explain why MoveEntitiesFrom works on a per-world basis, instead of per-entity. Still I was hoping that a direct memory copy of chunks would be possible and make it fast enough to do in the main thread. Only issue that might still remain is race conditions between the two worlds, but if we can manually determine that no jobs are running at some point in time, it would be nice to do the copying there quickly.