Search Unity

How to react (ie Change Entities) on System disable/enable [SOLVED]

Discussion in 'Entity Component System' started by mgmhunt, Sep 11, 2018.

  1. mgmhunt

    mgmhunt

    Joined:
    Apr 1, 2013
    Posts:
    62
    So OnStartRunning executes before injection, OnStopRunning is run AfterUpdate which again from my understanding the injected NativeArray's have been deallocated and therefore only for system use. Presume these only for internal setup and disposable. I found one example where a cache had been created in the system which was then disposed. (ie not injected or part of any jobs as far as I could see).

    Think I need a conceptual leap to this problem :)

    When I turn off a system, I want to 'reset' all the values of the Inject group to say a base value like 0. What's the correct ECS way of doing this?

    Is it not right to think of ever turning a system off and controlling all flow via Components? ie I'd need a tag component which determines whether the value are processed or if it's missing reset to 0 or something.

    Just with the checkboxes on the Entity Debugger, turning systems on/off is very easy and a easily changes behaviour. I'd like to develop parallel systems that are different ways of approaching the same problem and try them by turning only one on (say 3 different Health systems that all operate on a Health Component, but only one is processing at a time). But when the system is turned off, I can react to this somehow on a injected group? ie reset all Health Components to 0. Maybe you can do this via OnStopRunning but I'm not sure how.

    Thanks for any pointers...
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I recommend trying to keep the entire state of your game in Components. This lets you reason about your game's behaviour more easily than if you have multiple places you are keeping state (for instance, fields belonging to Systems).

    One way to start might be to make a settings Component for each of your health Systems, and have each system only run when its settings Component is somewhere in the scene. Then you could use the Reactive System pattern to let those systems set up or clean up when they're turned on and off.

    I understand that it's really easy to get started by enabling and disabling systems in the EntityDebugger. I think the problem here is that we don't yet support editing ComponentData in the Inspector. So we've got a situation where it's easy to turn systems on and off for testing, but it's annoying to change settings objects, even though they could give you much more detail control over behaviour. I'll get ComponentData editing in the Inspector prioritised.
     
    mgmhunt likes this.
  3. mgmhunt

    mgmhunt

    Joined:
    Apr 1, 2013
    Posts:
    62
    Thanks for the thoughts. Understand that the settings are kept mainly in a SystemStateSharedComponentData. Still not too clear how other systems 'react to each other' ... Don't want to have 'Health' component for each system, the idea would be a standard single component, operated on by multiple systems (but only one system at a time). Think I'll have to bury myself in the docs/examples a bit more.

    So the issue is how to keep multiple systems from running in parallel etc, and I think an approach would be a ManagerSystem that ExecutesBefore the other systems and sets a SystemStateSharedComponentData (used across the Manager and other controlled systems), which the other systems can check and see whether they should be running. So as long as this manager system is running, if you tried turning on a system that shouldn't be on, it would get disabled by checking a SystemStateSharedComponentData set by the (HealthSystem)ManagerSystem earlier.

    I've also got a bit of hybrid approach with ScriptableObjects providing settings to systems (for ease of access) and they could be copied across to a SystemStateSharedComponentData.

    Will give that page a triple read and see if it sinks in.

    + for the "ComponentData editing in the Inspector prioritised." !!
     
    Last edited: Sep 18, 2018
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    The solution you describe sounds good to me. Systems shouldn't react to one another so much as react to the state of your data.
     
    mgmhunt likes this.