Search Unity

Question EntityRemapping on World Serialization

Discussion in 'Entity Component System' started by SchnozzleCat, Jul 24, 2021.

  1. SchnozzleCat

    SchnozzleCat

    Joined:
    Oct 23, 2013
    Posts:
    42
    I have a system that that moves a specific set of entities to another world, serializes it, and moves them back.

    The issue I have is that not all entities are moved back, and some that aren't moved back reference entities that are moved. From what I understand, this is where EntityRemapInfo comes into place.

    I'm just not sure how to actually use it. I've tried a bunch of different stuff, but none of it seemed to work.

    The three parts to it are:

    1.) Move entities from game world to save world (EntityManager.MoveEntitiesFrom)
    2.) Serialize save world (SerializeUtility.SerializeWorld)
    3.) Move entities from save world to game world (EntityManager.MoveEntitiesFrom)

    All three of these functions take a NativeList<EntityRemapInfo> as a parameter. My question is just how to remap afterwards.

    Things I've tried:
    1) Pass the same NativeArray<EntityRemapInfo> to all three functions, this doesn't seem to work and while it does remap to an entity, it is not the correct one. It seems to remap to the entity index that the entity had on the save world, not the new index on the game world

    2) Use a separate NativeArray for each function. I tried ERU.RE(R3, ERU.RE(R2, ERU.RE(R1, originalEntityIndexOnRealWorld))), where ERU.RE is EntityRemapUtility.RemapEntity, and R1 is the NativeArray<EntityRemapInfo> passed to the first step shown above. (Same for R2, and R3). This makes sense I feel (remap original game world entity to save world entity -> remap save world entity to serialize entity -> remap serialize entity to new game world entity), but it doesn't work either (it remaps to Entity.Null), and seems very cumbersome.

    So what is the actually correct way to do this?
    I appreciate any help, thanks.