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

Entity creation/destruction callbacks in DOTS

Discussion in 'Entity Component System' started by MhmdAL1111, Apr 14, 2020.

  1. MhmdAL1111

    MhmdAL1111

    Joined:
    Oct 25, 2017
    Posts:
    30
    I've been messing with DOTS for a bit now, and I'm thinking about integrating it with my project slowly. However I need a way to be able to create healthbars for entities which have a HealthData component (I have not thought too much about the case where I want healthdata without healthbars because I might need that, but I'm sure it could be fixed by adding a flag or some other data component). Currently, I am using gameobjects with ConvertToEntity components, and in a start method of some monobehaviour I am instantiating a healthbar prefab which is just a worldspace canvas with some images (I know this is terribly inefficient, just testing) and placing it above the entities that need a healthbar. That is alright, but if I was to create new entities inside a System, I would need a way to create the healthbar in monobehaviour land. Currently I am just calling a Manager from the System and handling the creation of the entities and the healthbars there, same with destruction.

    I feel that calling monobehaviour managers (which I dislike using even in conventional unity) defeats the purpose of ECS, so does anyone have a better way to know when an entity has been created/destroyed?

    Thank you
     
  2. swejk

    swejk

    Joined:
    Dec 22, 2013
    Posts:
    20
    There is a method AddHybridComponent in GameObjectConversionSystem which you can use to add the HealthBar GameObject to the entity. This health bar is a companion to this entity, which means it will instantiate/destroy along with the entity and also will copy entities Translation/Rotation. So in practice you can have Unit Gameobject (with ConvertToEntity component). This Unit has child GameObject with HealthBar component.
    Code (CSharp):
    1. public class HealthBar : MonoBehaviour, IConvertGameObjectToEntity {
    2. // HealthBar Code
    3. ...
    4.  
    5. public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
    6.   conversionSystem.AddHybridComponent(this);
    7. }
    8. }
    *Use 0.6 or 0.9 version of Entities, as other one does not have this feature or have this broken.
     
  3. apaxnid

    apaxnid

    Joined:
    Nov 18, 2012
    Posts:
    34
  4. MhmdAL1111

    MhmdAL1111

    Joined:
    Oct 25, 2017
    Posts:
    30
    Hm, this is useful information but I don't see how that would work in this case because I still need the image/canvas in regular gameobject land because those are not convertable. Perhaps I'm misunderstanding something, time will tell. Thank you :)
     
  5. MhmdAL1111

    MhmdAL1111

    Joined:
    Oct 25, 2017
    Posts:
    30
  6. Radu392

    Radu392

    Joined:
    Jan 6, 2016
    Posts:
    210
    I suggest you use a hybrid approach. Mixing monobehavior with ecs is a valid approach when it makes sense.

    What I do in my game is the following: For complex UI involving events, such as buttons and scroll views, I use a good old fashioned Canvas and a mononbehavior to handle all of it. For simple but numerous UI, such as health bars and unit highlighting, I use ECS in combination with manual calls to Graphics.Draw() in its own UI system, not monobehavior.

    Basically, unless you want to reinvent the wheel and write a whole UI system that can handle what the normal Unity UI already does and take weeks if not months to implement, a basic rule of thumb is to use ECS and jobs whenever there's lots of computation or drawing to be done, otherwise just use the Canvas.

    Though if you do want to go the whole way pure ECS, I remember a thread claiming to have rewritten a lot of the canvas functionality in ECS. You might want to look into that and see if it satisfies your goals.
     
    Last edited: Apr 16, 2020
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    That is in fact exactly the purpose of system state components. When you call EntityManager.Destroyentity, the entity actually survives until all system state components are removed explicitly. Essentially the state is tied to the system and not touched by other code.

    This is the basic building block on top of which all add / remove / change patterns happen in systems.
     
    jdtec likes this.
  8. MhmdAL1111

    MhmdAL1111

    Joined:
    Oct 25, 2017
    Posts:
    30
    My bad for only glancing over the page, you are absolutely right, they are partly what I'm looking for. However, I still need to have some kind of event if I plan on staying hybrid and using the traditional unity UI> I have found a way to raise events from within systems which works for now so I'll be using that. Thank you :)
     
  9. MhmdAL1111

    MhmdAL1111

    Joined:
    Oct 25, 2017
    Posts:
    30
    I definitely was not planning on going full pure ECS, which is why I wanted to know how to recieve entity related events. (example: show something on screen using traditional UI when my enemy entity dies) But perhaps my question was not exactly what I wanted since things are still a bit foggy.

    And for the healthbars that is useful knowledge, I'll be looking into it, thank you. :)
     
  10. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    System state components are extremely verbose, I like the core design but when combined with Entities.ForEach I really dislike the high level flow. Can anyone think of a good way to abstract them out a bit, are there any generic patterns you could use with them?
     
  11. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I agree. We are thinking about how to make the reactive pattern Entities.ForEach style.
     
    optimise and _met44 like this.
  12. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Is this new API what you are talking about?
     
  13. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    No. that method is useful specifically for tracking creation of entities. I think thats fundamentally different from reactive systems. Which really dont care about entity creation but rather if a component now exists / didn't exist before. Destroying an entity in that context is one way to remove a component but its naturally also possible to just remove the component and a reactive system is expected to react to it the same way.
     
  14. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I see system state components as more of an optional feature in a reactive system. Reactive might need system state, the component itself, or just the component type depending on the context.

    Like you might have several different entities that should only exist together. You might have some notion of a primary or top level entity that once removed all associated entities are removed. The primary and associated might all have different needs for what data is required to cleanup. I tend to think the majority would not actually need system state.

    A common pattern we have is logical hierarchies where the top level we need an id from the component to trigger a network message. The rest of the hierarchy can cleanup itself when destroyed without any additional state..