Search Unity

Optional Component in Hybrid ECS?

Discussion in 'Entity Component System' started by Philip_Zhang, May 10, 2018.

  1. Philip_Zhang

    Philip_Zhang

    Joined:
    Feb 15, 2017
    Posts:
    25
    Hello,

    I'm currently building a platformer using Hybrid ECS, I implemented my own Position2D and Heading2D components and a SyncTransformSystem that syncs Transform, Position2D and Heading2D.

    Problem is I have some entities that only need a Position2D and don't need a Heading2D, so I'm wondering whether I can let SyncTransformSystem operate on like [Optional] Heading2D component thing. If the entity has Heading2D, do some stuff, if not, still do the operation on Position2D but skip the Heading2D part.
     
    Last edited: May 10, 2018
  2. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I think it is better to create two systems and avoid using if.

    One system uses Transform, Position2D second uses Transform, Position2D and Heading2D.
    The second system would have only code that would be in this if. You can annotate the second system to be executed after first one if order matters.
     
    Orimay likes this.
  3. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    You could also have 1 system that schedules 2 groups simultaneously (especially if they're related, but use different data). If it's jobified, you could put each set of logic in it's own job and combine the job handles with JobHandle.CombineDependencies(jobhandle1, ...).
     
  4. Philip_Zhang

    Philip_Zhang

    Joined:
    Feb 15, 2017
    Posts:
    25
    Thanks, guys.

    Since the code for syncing Position2D should be the same for two groups, so I think using two systems will cause some maintain inconvenience, so I currently am using two groups in one system while the second group has SubtractiveComponent<Heading2D> in it so that those entities are not included in the first group.

    But still, the code in the OnUpdate() function of SyncTransformSystem have to iterate over two groups, which is not very convenient, but it will do for now.
     
  5. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    You could also iterate only once through the group (via a special tag component) and check each entity for those 2 components...

    Not the fastest but if your goal is code readability it might be an option
     
  6. Philip_Zhang

    Philip_Zhang

    Joined:
    Feb 15, 2017
    Posts:
    25
    Hi, @sngdam
    What do you mean by "special tag component"? Can you show me some code examples, please?
     
  7. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    Code (CSharp):
    1. public struct Foo : IComponentData { }
    2.  
    3. // expose to MonoBehaviour-world, ensure file name matches: EG FooComponent.cs
    4. public sealed class FooComponent : ComponentDataWrapper<Foo> { }
    It's just any blank component, if it's present on an entity, it acts like a tag. An entity could have multiple tags for different purposes.

    I have an event System that the basic components are just tags:
    Event
    and
    EventUsed


    If an entity represents an event, it'll have the Event component to mark it as such. If it's used by a system, other systems can mark an event as Used, and others can filter it out.

    I also have a tag component to mark things for destruction at the end of the frame, IsGarbage.
     
  8. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    I am not at computer but what i meant is.

    1. Add a tag component (as described in previous post) to all entities you want to be processed by your system - in your case that might not even be required and you could use the position component to function as a tag
    2. You inject the position array and the corresponding entity array
    3. In your loop, you simply check each entity if it also contains a heading component like this

    EntityManager.HasComponent<MyComponentData>(entity);


    Edit:
    As mentioned earlier, I would not use this approach if you iterate over a large number of entities or if you need fastest speed possible
     
    Seb-1814 likes this.
  9. Philip_Zhang

    Philip_Zhang

    Joined:
    Feb 15, 2017
    Posts:
    25
    Uh, I see, thanks sngdan ;)