Is a .ForEach and ToComponentDataArrayAsync Guaranteed to have the same order of components?

-I took the sample trigger physics example from the github

  • altered the “collision entrance section” so that I grab the ghostOwnerComponent (networkId) and compare it to a NativeArray of PlayerScore networkIds

  • If they match, I ++ the player score.

  • I grab the PlayerScore with: ```
    var playerScores = m_PlayerScores.ToComponentDataArrayAsync(Allocator.TempJob, out playerScoreDep);


- I update the values of the array with a .ForEach, here is the important part:

```csharp
                    //this is what happens when the bullet enters
                    else if (triggerEvent.State == EventOverlapState.Enter)
                    {

                        // //we find the bullets Owner
                        var bulletsPlayerNetworkId = ghostOwner[e].NetworkId;
                        int pointsToAdd = 0;
                        if (EntityManager.HasComponent<AsteroidTag>(otherEntity))
                        {
                            pointsToAdd += 1;
                        }
                        for (int j = 0; j < playerScores.Length; j++)
                        {
                            //if the playerScore's owner is the same as the bullet's owner, add to the score
                            if(playerScores[j].networkId == bulletsPlayerNetworkId)
                            {
                                Debug.Log("points to add: " + pointsToAdd);
                                Debug.Log("Current score before addition: " + playerScores[j].currentScore);
                                var newPlayerScore = new PlayerScore{
                                    networkId = playerScores[j].networkId,
                                    playerName = playerScores[j].playerName,
                                    currentScore = playerScores[j].currentScore + pointsToAdd,
                                    highScore = 0
                                    };
                                playerScores[j] = newPlayerScore;
                                Debug.Log("what seems to be the final result score: " + playerScores[j].currentScore);
                            }
                        }
...
.Run();

I then have another .ForEach that runs on the actual PlayerScore entities and I update the components with the NativeArrayValues:

        Entities
        .ForEach((Entity Entity, int entityInQueryIndex, ref PlayerScore playerScore) =>
        {
            playerScore = playerScores[entityInQueryIndex];
        }).Run();
        playerScores.Dispose();
        // highestScore.Dispose();
        m_CommandBufferSystem.AddJobHandleForProducer(Dependency);

Are these guarantees to have the same entities in the same index?

As long as the query in the ForEach and the EntityQuery you call ToComponentDataArray/ToEntityArray from both have the exact same archetype they are guaranteed to align.

That’s great news! Both are just calling a “PlayerScore” component so all good. Thank you Sarkahn and Merry Christmas!