Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to share a cached array between systems? The array size is not fixed.

Discussion in 'Entity Component System' started by GarthSmith, Oct 4, 2018.

  1. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Hello Unity Forum Members!

    We want to share a cached array between systems. Unfortunately, the size of this array is not fixed.

    We have a puzzle game where we check for connected gamepieces/blocks/tiles. If you're familiar with A* or path finding algorithms, it is similar.

    In the past we would do this with Dictionaries (position keys and gamepiece values) and HashSets (to track positions we have already checked.)

    In ECS, we avoid Dictionaries, HashSets, nested loops, or .Contains() calls. Instead, we have a pre-processing step where we cache a map of every gamepiece by using a 2 dimensional array. This allows us to do direct lookups of gamepieces using the position as an index, instead of using a nested loop to search for the right gamepiece.

    I would like to have several JobComponentSystems reuse this cached map. What would be the best way to share this array between systems? I would use a ComponentData to store the map except the size of the 2 dimensional array changes based on the size of the level. ComponentData needs to know the size of the array ahead of time.

    How do you share a re-sizable cached map between systems?

    EDIT: I saw a suggestion to inject one system into another system. Isn't this very anti-ECS? Storing cached data in a system?
     
  2. dstilwagen

    dstilwagen

    Joined:
    Mar 14, 2018
    Posts:
    36
    Have you considered DynamicBuffers attached to entities?

    First part of the documentation for DynamicBuffers:
    the rest of the document can be found here https://github.com/Unity-Technologi...ster/Documentation/content/dynamic_buffers.md
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I was not familiar with dynamic buffers, but I know what I'm trying out tomorrow!
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Still going to try it, but now I have questions about how these are stored in memory.

    Should I treat them as reference types with all the performance implications that has?

    Or if multiple buffers are stored continuously in memory, won't it cause fragmentation once a buffer needs to resize?
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,654
    upload_2018-10-4_9-5-0.png