How can I change a SharedComponent in IJobProcessComponentData(WithEntity)?

I have a system that changes the material of units depending on their state (which is probably totally wrong, but it’s at least working):

public class UpdateMaterialsSystem : ComponentSystem
    {
        private struct Data
        {
            public readonly int Length;
            public EntityArray EntityArray;
            [ReadOnly] public ComponentDataArray<NeedsVisualUpdate> NeedsVisualUpdate;
            [ReadOnly] public SharedComponentDataArray<MeshInstanceRenderer> Renderers;
        }

        [Inject] private Data data;

        protected override void OnUpdate()
        {
            var em = World.Active.GetExistingManager<EntityManager>();
         
            for (var i = 0; i < data.Length; i++)
            {
                var entity = data.EntityArray[i];
                var renderer = data.Renderers[i];
                if (em.HasComponent<Fleeing>(entity))
                {
                    renderer.material = Materials.FleeingUnitMaterial;
                }
                else
                {
                    renderer.material = Materials.NormalUnitMaterial;
                }

                PostUpdateCommands.SetSharedComponent(entity, renderer );
                PostUpdateCommands.RemoveComponent<NeedsVisualUpdate>(entity);
            }
        }
    }

I tried to jobify this, only to find out that IJobProcessComponentData (or its WithEntity sibling) only can take arguments of type IComponentData but not ISharedComponentData. Is it possible to change shared components like the MeshInstanceRenderer component inside of a job system and how can this be done?

Currently SharedComponents can only be accesses on the main thread. We are working on changing this.

4 Likes

I see, thank you very much :slight_smile: