Search Unity

Possible Bug in NativeList and RemoveAtSwapBack?

Discussion in 'Entity Component System' started by Chris_Entropy, Feb 11, 2020.

  1. Chris_Entropy

    Chris_Entropy

    Joined:
    Apr 11, 2011
    Posts:
    202
    I am currently tinkering with implementing an A* pathfinding algorithm with ECS. I am using a JobSystem with an IJobChunk for this. I am not sure, if this will get me the maximum efficiency, but that's what I am working on. I have made the strange observation, that when I use NativeList.RemoveAtSwapBack, it changes the order of my List elements, with elements at the back suddenly being transported to the front. I first thought, that it was my A*-implementation doing bollocks, but now that I have implemented my own RemoveAt-function, it works fine. Is there someone else, who can confirm, that this is an issue, or maybe point me, what I am doing wrong?

    My code (with the replaced RemoveAt) is the following: https://pastebin.com/kSTSY0C2
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
  3. BrendonSmuts

    BrendonSmuts

    Joined:
    Jun 12, 2017
    Posts:
    86
    This is exactly the intended functionality. RemoveAtSwapBack performs a faster remove by removing the element at the index and then moving the last element to the index of the removed item. To maintain order of the elements you would need to shift a number of elements equal to (count - 1 - index) which for very long lists could be very slow.
     
  4. Chris_Entropy

    Chris_Entropy

    Joined:
    Apr 11, 2011
    Posts:
    202
    I am truly sorry. I totally missed that. Thanks for the insight. I now have to rethink my code.