Search Unity

questions on the Boids example

Discussion in 'Entity Component System' started by laurentlavigne, Mar 26, 2018.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,365
    The Boids sample is some beautiful code, but, without comments it's a steep learning curve. I watched Mike's presentation and about 30 minutes he lost me. https://www.twitch.tv/videos/242024723?t=29m34s so:
    1. Why using hash as index? is there no <vector3, entityIndex> data structure that would do the same?
    2. is a hashmap some sort of dictionary where value is a list?
    3. do hashmap have set like operations?
    4. do hashmap allow querry on one value to get the key?
    5. componentgroups have simple set operations, is there intersection as well?
    6. any other set like data structure in ECS? When programming in C# I yearn for List.Intersect(OtherList), set theory is sweet like honey.
    7. what does SetFilter do? m_BoidGroup.SetFilter(settings);
    8. I see mergecell, what is it for?
    9. Could we get that same boid example with zero optimization? Seems that a lot of the code is into packing boids in cells and smart tricks in proximity grouping, beautiful stuff, but make things hard to learn.
     
    MadeFromPolygons and Gametyme like this.
  2. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    1. A hashed index boils down the Vector3 into an int which is less data, much faster to access and also a common thing to do. You can get some more info here: https://en.wikipedia.org/wiki/Hash_table

    2. Are you talking about a NativeHashMap or a NativeMultiHashMap? A NativeHashMap is similar to a Dictionary a NativeMultiHashMap is similar to a MultiValueDictionary. But keep in mind that the Native containers are much more optimized than the standard C# containers in terms of data layout. This is why they are also not as convienient as the standard C# containers, but they have all functionality which is needed to write jobified code.

    3. Are you talking about [key] = value? No.

    4. No.

    5. No, what you would need them for?

    6. Not, as far as I undestand your question.

    7. The boid example has more than one boid type (check BoidComponent), which is a SharedComponent. SetFilter is called with a specific BoidComponent to reduce the group to the boids which have exactly those settings.

    8. merge cells does a few different things. First of all it finds the obstacles/targets for each cell (not for each boid) and saves them in a separate list for later use in the steering job. It also saves the cellIndices (hashes), headings and positions of each boid into a separate list, to avoid TryGetFirstValue/TryGetNextValue when processing the steering job. MergeCells is a simple but very effective optimization.

    Try to watch the talk again, while looking at the code. It needs some time to understand how this all works. You will realize it's quite simple code, as soon as you get the hang of it.
     
    Last edited: Mar 26, 2018
    Games4Stream likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    On hashmap vs. dictionary, from MSDN's page on Dictionary<TKey, TValue>:

    So a dictionary is a hashmap. It's simply a question about abstraction level - the name Dictionary tells you what it does (you look up something by it's identifier), and the name HashMap tells you how it does what it does (hash the key to get a lookup index).

    Unity could've gone with NativeDictionary for naming, but the whole ECS philosophy is a lot lower level than C# in general, so NativeHashMap is probably a better fit.
     
    Last edited: May 16, 2018
    MadeFromPolygons likes this.