Search Unity

How to add variable Type component to entity?

Discussion in 'Entity Component System' started by jintaenate, Jul 19, 2019.

  1. jintaenate

    jintaenate

    Joined:
    Sep 12, 2018
    Posts:
    17
    Hi i add same type component to few entities when entities are created.
    in my work, add component with switch is fine but i guess its not good performance so i wanna make switch statement before use for statement


    This is my origin code

    Code (CSharp):
    1.                 for (int i = 0; i < Prefabs.lenght; i++)
    2.                 {
    3.                     Entity instance = CommandBuffer.Instantiate(index, prefabSpawner.Prefab);
    4.                     switch (prefabSpawner.unitType)
    5.                     {
    6.                         case ScriptType.ECS:
    7.                             CommandBuffer.AddComponent(index, instance, new Player());
    8.                             break;
    9.                         case ScriptType.JECS:
    10.                             CommandBuffer.AddComponent(index, instance, new Enemy());
    11.                             break;
    12.                         case ScriptType.JECS2:
    13.                             CommandBuffer.AddComponent(index, instance, new Unit());
    14.                             break;
    15.                         default:
    16.                             break;
    17.                     }
    18.                 }
    and i wanna make concept like this

    Code (CSharp):
    1.                 Type scripttype = typeof(None);
    2.                 switch (prefabSpawner.unitType)
    3.                 {
    4.                     case UnitType.Player:
    5.                         scripttype = typeof(Player);
    6.                         break;
    7.                     case UnitType.Enemy:
    8.                         scripttype = typeof(Enemy);
    9.                         break;
    10.                     case UnitType.Unit:
    11.                         scripttype = typeof(Unit);
    12.                         break;
    13.                     default:
    14.                         break;
    15.                 }
    16.  
    17.                 for (int i = 0; i < Prefabs.lenght; i++)
    18.                 {
    19.                     Entity instance = CommandBuffer.Instantiate(index, prefabSpawner.Prefab);
    20.                     CommandBuffer.AddComponent(index, instance, scripttype);
    21.                 }
    but I got error that

    The type 'Type' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'EntityCommandBuffer.Concurrent.AddComponent<T>(int, Entity, T)'

    How can i fix it?
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You need to pass the type into a generic method and then call instantiate from there.
    Something like this:
    Code (CSharp):
    1.     void Instantiate<T>() where T : struct, IComponentData {
    2.       for (int i = 0; i < Prefabs.lenght; i++) {
    3.         Entity instance = CommandBuffer.Instantiate(index, prefabSpawner.Prefab);
    4.         CommandBuffer.AddComponent(index, instance, default(T));
    5.       }
    6.     }
    and then your switch or whatever will look like this:

    Code (CSharp):
    1.       switch (prefabSpawner.unitType) {
    2.         case UnitType.Player:
    3.           Instantiate<Player>();
    4.           break;
    5.         case UnitType.Enemy:
    6.           Instantiate<Enemy>();
    7.           break;
    8.         case UnitType.Unit:
    9.           Instantiate<Unit>();
    10.           break;
    11.         default:
    12.           break;
    13.       }
     
  3. jintaenate

    jintaenate

    Joined:
    Sep 12, 2018
    Posts:
    17
    This is what i need it

    Thank you.:)