Rewrite as a JobComponentSystem

Question is possible to write the following code as JobComponentSystem using 2019.2.6 with entities .0.1.1 preview?
The issue I am having is I have 2 entity in which I need to make changes to. My guard entity need to find a station the needs a guards and once it does it increase the guard count at that station so another guard doesnt get assigned there.

Consider making it easier for people to read your code by using code tags instead of a screenshot.

Was rushing this morning to get something out before hitting the road. Wasnt thinking

namespace IAUS.ECS.Systems
{
    public class GetGuardPoint : ComponentSystem
    {
        public EntityQueryDesc Guards= new EntityQueryDesc()
        {
            All = new ComponentType[] { typeof(Guard), typeof(LocalToWorld) }

        };
        public EntityQueryDesc post = new EntityQueryDesc()
        {
            All = new ComponentType[] { typeof(GuardStationComponent), typeof(LocalToWorld) }

        };

        protected override void OnUpdate()
        {
            Entities.With(GetEntityQuery(Guards)).ForEach((Entity entity, ref Guard GC, ref LocalToWorld toWorld) => {
                if (!GC.Assigned())
                {
                    float3 PostLocation = new Vector3();
                    Entity ClosestPost = Entity.Null;
                    float3 CurrentPosition = toWorld.Position;
                    GuardStationComponent closest = new GuardStationComponent();
                    Entities.With(GetEntityQuery(post)).ForEach((Entity entityPost, ref GuardStationComponent Station, ref LocalToWorld transform) => {
                        if (Station.NumOfAssignedGuards >= Station.NumberOfGuardRequired)
                        {
                            return;
                        }
                        else
                        {
                            if (ClosestPost == Entity.Null)
                            {
                                ClosestPost = entityPost;
                                PostLocation = transform.Position;
                                closest = Station;
                            }
                            else
                            {
                                if (Vector3.Distance(CurrentPosition, transform.Position) < Vector3.Distance(CurrentPosition, PostLocation))
                                {
                                    // this target is closer
                                    ClosestPost = entityPost;
                                    PostLocation = transform.Position;
                                }
                            }
                        }

                    });
                    if (!PostLocation.Equals(new float3(0, 0, 0)))
                    {
                        GC.TargetLocation = PostLocation;
                    }

                    Entities.With(GetEntityQuery(post)).ForEach((ref GuardStationComponent Station, ref LocalToWorld transform) => {
                        if (PostLocation.Equals(transform.Position))
                        {
                            Station.NumOfAssignedGuards++;
                        }

                    });

                }
            });

        }
    }
}

If I understand correctly you are moving guards around but want to move them using the job system? The first thing would be to see if it works without using the job system.I assume you started from there. In any case, you can write anything in the job system if you want to, but the question would be if its necessary/better than doing it on the main. I have not worked with the Job system, but if its the same as async tasks in c# they cant interact with objects on the main thread…

No, The code is for the guard entity to find a empty guard station and get assign the location. Once the station location is assigned, the stations count of assigned guards is increase. The issues is that doing so in a jobcomponentsystem only allows of one entity type to be used at a time. So I can assign a guard a station but I can not increase the station’s count of assigned guards.

Based on reading last response, I would consider following.

Each guard is an entity.
Each station is an entity.
If station is empty, assign empty component tag, to allow gas system query search.
Once guard found an empty station, remove empty component tag from station, now indicating that station is occupied by at least one guard.
If you consider multiple guards per station, use dynamic buffer on stations, storing references to guard entities.
Also, guard will need component with entity reference, to occupied station.

Is these makes sense for your case? Is something would feel your requirement?

I mean it works but that’s a messy implementation. Stations don’t need to have a reference to the guard entity. it just need to have a count of the number of guard assigned to it. Just need to increment the NumberOfAssigned variable in the station component once the system applies it

Pulled my hair for a bit but unite Copenhagen talk had the solution

    public class GetGuardPointJob : JobComponentSystem
    {
        public EntityQueryDesc posts = new EntityQueryDesc()
        {
            All = new ComponentType[] { typeof(GuardStationComponent), typeof(LocalToWorld) }

        };

        public struct Jobas : IJobForEachWithEntity<Guard>
        {
            [NativeDisableParallelForRestriction] public ComponentDataFromEntity<GuardStationComponent> CDFEGP;
            [ReadOnly] [NativeDisableParallelForRestriction] public ComponentDataFromEntity<LocalToWorld> CDFETrans;

            [DeallocateOnJobCompletion] public NativeArray<Entity> posts;
            public void Execute(Entity entity, int index, ref Guard GC)
            {
                if (!GC.Assigned())
                {
                    LocalToWorld toWorld = CDFETrans[entity];
                    float3 PostLocation = new Vector3();
                    Entity ClosestPost = Entity.Null;
                    float3 CurrentPosition = toWorld.Position;
                    GuardStationComponent closest = new GuardStationComponent();
                    foreach (Entity entityPost in posts)
                    {
                        GuardStationComponent Station = CDFEGP[entityPost];
                        LocalToWorld transform = CDFETrans[entityPost];
                        if (Station.NumOfAssignedGuards >= Station.NumberOfGuardRequired)
                        {
                            return;
                        }
                        else
                        {
                            if (ClosestPost == Entity.Null)
                            {
                                ClosestPost = entityPost;
                                PostLocation = transform.Position;
                                closest = Station;
                            }
                            else
                            {
                                if (Vector3.Distance(CurrentPosition, transform.Position) < Vector3.Distance(CurrentPosition, PostLocation))
                                {
                                    // this target is closer
                                    ClosestPost = entityPost;
                                    PostLocation = transform.Position;
                                }
                            }
                        }

                    };
                    if (!PostLocation.Equals(new float3(0, 0, 0)))
                    {
                        GC.TargetLocation = PostLocation;
                        GuardStationComponent test = CDFEGP[ClosestPost];
                        test.NumOfAssignedGuards++;

                        CDFEGP[ClosestPost] = test;
                    }
                }
            }

        }

Sure if that what you need, that even better.
What matter is, you figured out your own solution.

if (Vector3.Distance(CurrentPosition, transform.Position) < Vector3.Distance(CurrentPosition, PostLocation))

You should be using floats really. float3 specifically.
Also, you can subtract both float3 (Vectors3) before calculating distance. For even reducing further computation overhead, you could use ‘squared’ distance (before get squared).

Also, you should be able to add burst to the job.