Search Unity

Is ComponentType.TypeIndex deterministic between platforms/instances?

Discussion in 'Entity Component System' started by fholm, Nov 9, 2018.

  1. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Whole question is in the title basically, but i'll elaborate a bit more: Assuming that two instances of a game is built from the exact same project source, no matter their platforms (windows/osx/android/ios/etc.) is the value of ComponentType.TypeIndex deterministic? I.e. will it be the same between the two different instance of the game while they are running?
     
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    The TypeIndex is created and cached on the go as you use each type for method's generic throughout the ECS library. If one game instance use the type in different order than the 2nd one I think index will not be the same.
     
  3. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Okey, thanks... crap, i'll have to figure out another way to do what i want then.
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    This should be equal between any instances of the game, if you are looking to match type index of different game instances :
    TypeManager.GetTypeInfo<YourType>().FastEqualityTypeInfo.Hash;
     
  5. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Hashes are well... I don't like relying on them unless it's documented somewhere that they are gurantueed to be unique always.
     
  6. rigidbuddy

    rigidbuddy

    Joined:
    Feb 25, 2014
    Posts:
    39
    But you could pre-init type indices deterministically (at least in existing versions of ECS)

    Code (CSharp):
    1. static void InitTypeManager(params Type[] replayableTypes)
    2. {
    3.     TypeManager.Initialize();
    4.  
    5.     // order matters
    6.     foreach (var type in replayableTypes)
    7.         TypeManager.GetTypeIndex(type);
    8. }
    And call it like that

    Code (CSharp):
    1. InitTypeManager(typeof(ComponentA), typeof(ComponentB), typeof(ComponentC));
    or you could add more complex logic (e.g. using static reflection).

    But should call it before any other calls to ECS. Don't forget to use UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP
     
    5argon likes this.