user3795555
user3795555

Reputation: 7

How do you reference an array item within nativeArrray<Entity> in ECS

I'm trying to do the following:

private void AssignPlayer()
{
    EntityQuery playerQuery = GetEntityQuery(ComponentType.ReadOnly<PlayerTag>());
    Entity playerEntity = playerQuery.ToEntityArray(Allocator.Temp);

    Entities.
        WithAll<ChaserTag>().
        ForEach((ref TargetData targetData) =>
        {
            if (playerEntity != Entity.Null)
            {
                targetData.targetEntity = playerEntity;
            }

        }).Schedule();
}

But the line that assigns targetData.targetEntity = playerEntity; throws the error:

Cannot implicitly convert type 'Unity.Collections.NativeArray<Unity.Entities.Entity>' to 'Unity.Entities.Entity'

Upvotes: 0

Views: 1799

Answers (1)

Andrew Łukasik
Andrew Łukasik

Reputation: 1637

if( playerQuery.CalculateEntityCount()!=0 )
{
    NativeArray<Entity> players = playerQuery.ToEntityArray( Allocator.TempJob );
    Entities.WithName("targetData_update_job")
        .WithReadOnly( players ).WithDeallocateOnJobCompletion( players )
        .WithAll<ChaserTag>()
        .ForEach( ( ref TargetData targetData ) =>
        {
            targetData.targetEntity = players[0];
        }
        ).ScheduleParallel();
}
else Debug.LogWarning($"{nameof(AssignPlayer)}(): no players tho");

Upvotes: 1

Related Questions