Search Unity

Get entity data? Not in job.

Discussion in 'Entity Component System' started by JamesWjRose, Feb 20, 2020.

  1. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687
    So, there may be multiple ways I am screwing this up, so yea, feel free to point and laugh.

    I am using an asset called Easy Roads 3D because it contains the road and lane data I need for traffic. I am converting it's data to entities, that's all fine. What I need to do is loop through all roads and add auto's to them. I only need to do this once, so not something I should put in "Update"

    So the first question is; Can I do this? Do I need a Job that does this, or is it possible in some other manner? If it is a job, how do I have it run only once? (see, I told you that you could laugh)

    Thanks
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    Not funny. This is one of the most dangerous corners in ECS.

    To solve this correctly, you actually do want to put your logic inside of an OnUpdate in a system and write the job the way you normally would (or I suppose you could use EntityManager of Entities.WithStructuralChanges.ForEach if that works better for you). The catch is that you only schedule and execute the job if a particular component (call it RequestPopulateAutosOnRoads or something) exists. And as soon as you are done, you delete the component (or tell an EntityCommandBuffer to delete it).

    This is an often overlooked piece of existence-based processing. What exists isn't just the data, but also the signal to process it. That signal also manifests itself as data.
     
    JamesWjRose likes this.
  3. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687

    Interesting. I'm going to need to digest that. Thank you very much.
     
  4. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    224
    If each Road segment will be a separate entity, you could use Entities.WithNone<TrafficPopulated>() (or, ComponentType.Exclude<TrafficPopulated>) so your system only runs on road segment entities that don't yet have a traffic component on them, where TrafficPopulated is an empty tag component. That way if new roads get loaded (from an additive scene load or maybe subscene streaming in or prefabs spawned) they'd spawn in without the necessary tag, the system would pick them up on the next Update, populate them with traffic (and the TrafficPopulated component) and then go back to sleep.
     
    siggigg likes this.
  5. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687

    Thanks. Because I am making auto entities, and because this has to be done with Entity Command Buffer, which runs on the main thread, so I have had to move the process out of a Job System. Thank you very much for the info and I will keep it in mind for future issues.

    Have a great evening