Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Difficulty implementing game design without using collections in Components

Discussion in 'Entity Component System' started by orionburcham, Dec 12, 2018.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Hi folks! I'm trying to figure out how to best implement a feature from a previous game, using Unity ECS.

    Below is a description of the feature. If you have an idea of how Unity's ECS approach would like this to be organized, please comment below. I would sincerely appreciate your advice. Thanks!

    - - -

    Example:

    This is a game about a pizza chef. This chef has a queue of pizza orders, which he executes one at a time, from first to last. Each order defines a pie size, a crust type, and a list of desired toppings.

    Every 10 minutes, the chef takes the next order in his queue, and creates a pizza based on its instructions. That order is then removed from the queue and discarded. The next order in line then becomes the first.

    However, new pizza orders may be inserted anywhere in the queue. They aren't always added to the back of the line.

    - - -

    How would this be defined using *Pure* ECS? What data would be represented through Entities, Components, and Systems?

    I chose this example, since it seems difficult to achieve without using collections (which currently can't be used in Unity ECS Components), or IBufferElementData. How would one represent the queue of pizza orders?

    Sincerely, thank you for any advice. :)
     
    Last edited: Dec 12, 2018
  2. unity_j5_Q4CX6Mras7g

    unity_j5_Q4CX6Mras7g

    Joined:
    Dec 23, 2017
    Posts:
    1
    Assuming there's some sort of time limit for orders, wouldn't a simple component holding a countdown timer or other sorting number for each entity work for queuing?
     
  3. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Thank you for the reply!

    There’s no time limit on orders. One order is processed every 10 minutes.

    My question is about how the queue of orders would be represented in data, without the use of a collection.

    In another ECS engine, I might create a component with an array that represented the queue. But this isn’t supported with Unity ECS.

    I’m also not sure an IBufferElementData would work, since I need to be able to control the order of Components. Orders that have been filled must be removed from the queue. And new components must be able to be inserted at specific locations.
     
    Last edited: Dec 12, 2018
  4. Piefayth

    Piefayth

    Joined:
    Feb 7, 2017
    Posts:
    61
    Well, you could just store the queue directly in a system. Allocate some persistent storage on startup, using some NativeContainer for it if you need to run jobs on the queue. Have other systems spawn entities with an"Order" component that contains their data and desired position in the order queue, then have the order queue system modify the queue appropriately and preserve references to those "order" entities.

    If you're really set on not using a collection, you could just have the order components store their own position in queue and sort them with a job when you need them in order. I think this'd be awkward, since you'd have to make collection-like modifications to the order indices every time the queue changes.

    That said, there is nothing impure about using DynamicBuffers or SharedComponents when needed. You just need to consider what data you NEED to run jobs on, and make sure that data is job-compatible. If you had THOUSANDS of chefs and queues to manage, I would think the buffer solution would be close to ideal; it's in a format that you can process in a job + does not cause your components to move into separate chunks like attaching a SharedComponent would.

    Edit: As far as order in your buffer goes, you have to fill the buffer yourself, so just, y'know, maintain the order yourself. Can interact with it with random access like an array.
     
    Tony_Max likes this.
  5. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Well there’s my answer right there. My apologies, I didn’t realize you could do this.

    Please chalk it up to my ignorance of the API. Thanks again for the help!
     
  6. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Alternatively, you could have an component that holds a reference to the next pizza order entity, kind of like a linked list. Would make inserting orders a little easier.