Search Unity

Help with pure ECS approach - current release doesn't match tutorial

Discussion in 'Entity Component System' started by reggie_sgs, Oct 28, 2019.

  1. reggie_sgs

    reggie_sgs

    Joined:
    Jun 29, 2014
    Posts:
    276
    I'm trying to get started with ECS in 2019.2.9 and have gone through the tutorial here:

    https://learn.unity.com/tutorial/entity-component-system#5c7f8528edbc2a002053b67b

    It seems that a lot has changed since this tutorial was created and I'm on the final step of trying to get a pure ECS example working but it seems the HelloCube sample is now the correct way to do what is shown in that video.

    I've managed to convert the code shown in the tutorial to match the HellowCube->SpawnFromMonoBehaviour example but the one aspect I don't understand is the change to job scheduling.

    In the tutorial, we create and schedule a job each pass and can set job specific variables such as time passed. In the HelloCube example, it appears that jobs are now automatically scheduled and run with no interaction. Is there any info to explain how we manage jobs now and set variables in them?

    Thanks,
    Reggie
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    bugfinders likes this.
  3. reggie_sgs

    reggie_sgs

    Joined:
    Jun 29, 2014
    Posts:
    276
    Thanks for the reply. Yes I've been looking through that sample but those samples don't show anything related to creating and scheduling jobs. I've done some more poking around and it seems to me that you are no longer supposed to schedule these as shown in the outdated tutorial. As best I can tell, jobs are scheduled and run now from OnUpdate of a class that derives from JobComponentSystem.

    I've looked and OnUpdate doesn't get called on every script, just the scripts that have entitites available to operate on. This leads to the question of how the system knows which OnUpdate to call. Is there supposed to be only one struct per class and that dictates whether the OnUpdate is called for that class? Or does it look at the return type of OnUpdate to determine which job it will run and thus whether there are entities to run on?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Unless system is ordered explicitly to run at desired moment, by default it runs every DOTS frame. When job has specified components assigned, then system update is called as long, as there are matching entities, containing these components.
     
  5. reggie_sgs

    reggie_sgs

    Joined:
    Jun 29, 2014
    Posts:
    276
    Thanks for the reply. The thing I'm trying to understand is what dictates when OnUpdate is called. The job is created/scheduled there so if it doesn't run, it would seem that the job wouldn't get scheduled. If there were 2 structs in the class with different entities, would it run OnUpdate if either struct had matching entities?
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    The entry point for a system is actually Update(), and there are several methods inside it that get called, one of them being OnUpdate(). Specifically, there is an InternalUpdate that checks:
    if (enabled && ShouldRunSystem())


    By default it uses OR logic, but you can override this behavior using attributes or API.

    Update() gets called by ComponentSystemGroups which by default your systems get assigned to on initialization using reflection. And the top-level ComponentSystemGroups get injected as delegates into the PlayerLoop.
     
  7. reggie_sgs

    reggie_sgs

    Joined:
    Jun 29, 2014
    Posts:
    276
    Thanks for the reply. Some of that is above my head as I'm new to all this.

    If I understand this correctly, any class derived from JobComponentSystem will get added to the ComponentSystemGroups for processing. So does this mean that if you buy assets and add to your project, you'll be adding a collection of jobs you know nothing about?

    In the past, you typically have to add an object to your scene and attach a script before any asset code would get run giving you a little better idea what coding was getting added.
     
  8. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    Yes it does. However, if those packages are designed correctly, they will only ever schedule jobs if a component from those packages is attached to the entity.

    But if it is a concern, you will want to look into custom bootstraps with ICustomBootstrap which gives you control over what systems get added to your game loop.
     
  9. reggie_sgs

    reggie_sgs

    Joined:
    Jun 29, 2014
    Posts:
    276
    Ok thanks for clearing that up.