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

use managed container inside ecs system?

Discussion in 'Entity Component System' started by Ahmed_Al_Asl, Mar 27, 2019.

  1. Ahmed_Al_Asl

    Ahmed_Al_Asl

    Joined:
    Jul 14, 2017
    Posts:
    9
    I know that we already have native container that can replace most of manged container but I want to know what is the downside of using them inside a system or a job?

    - is it ok to read from managed container that exists in Mono scripts for example from a job or a system ? and is it ok to write to them?

    - I heard that IComponent could contain references. can I get an example on entity that have IComponent attach to it ?
     
  2. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    You can use managed containers most anywhere except inside of actual jobs, so things like ComponentSystem, IJobComponentSystem, can have containers in them, but if you want to use the data within it, when you schedule the job, you have to pull the data out and then put it into a nativecontainer and pass it into the job.

    Here is an example of how you could go about taking the data from a managed container and put it into a NativeContainer to be used in a job if you wanted to, but I mean, you could go about it any number of ways. It's probably not the best/most efficient way to go about it, you probably don't want it to be done on update, but just so you have an idea what I mean.

    Code (CSharp):
    1. public class MyJobSystem : JobComponentSystem
    2. {
    3.     public Dictionary<MyStruct, List<MyStructsInList>> myDictionary;
    4.     public NativeMultiHashMap<MyStruct, MyStructsInList> myNativeMultiHashMap;
    5.  
    6.  
    7.     public struct MyJobNeedsDataFromContainer : IJob
    8.     {
    9.         public NativeMultiHashMap<MyStruct, MyStructsInList> ableToBeUsedInAJob;
    10.  
    11.         public void Execute()
    12.         {
    13.             // Do something with ableToBeUsedInAJob here
    14.         }
    15.     }
    16.  
    17.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    18.     {
    19.         var ableToBeUsedInAJob = ManagedContainerToNativeContainer();
    20.  
    21.         return inputDeps = new MyJobNeedsDataFromContainer {ableToBeUsedInAJob = ableToBeUsedInAJob}.Schedule(inputDeps);
    22.     }
    23.  
    24.     public NativeMultiHashMap<MyStruct, MyStructsInList> ManagedContainerToNativeContainer()
    25.     {
    26.         var myDict = myDictionary;
    27.         var dictKeys = myDict.Keys.ToList();
    28.  
    29.         for (int i = 0; i < dictKeys.Count; i++)
    30.         {
    31.             myDict.TryGetValue(dictKeys[i], out var dictValue);
    32.  
    33.             for (int j = 0; j < dictValue.Count; j++)
    34.             {
    35.                 myNativeMultiHashMap.Add(dictKeys[i], dictValue[j]);
    36.             }
    37.         }
    38.  
    39.         return myNativeMultiHashMap;
    40.     }
    41. }
     
    Ahmed_Al_Asl likes this.