Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

System Disabled Callback

Discussion in 'Data Oriented Technology Stack' started by Soaryn, Apr 17, 2018.

  1. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    210
    Would there happen to be a callback or event to listen for when a system has been disabled/enabled?
    By disabled I more so mean the method the EntityDebugger toggles.

    I was trying to write a websocket system, but I'd like the system to close its connection when it is disabled.
     
    Last edited: Apr 17, 2018
  2. xXPancakeXx

    xXPancakeXx

    Joined:
    Mar 14, 2015
    Posts:
    13
    You could derive from EntityDebugger and from SystemListView and create your own events.
    Or you could check in the systems update method if its enabled via .Enabled
     
    SubPixelPerfect likes this.
  3. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    210
    OnUpdate isn't called if Enabled is false though, so how exactly would that work out? And the View classes are just queries per individual systems. That doesn't exactly help per se with an "Hey I was disabled" without making another system just to purely observe changes.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,712
    Right now there is no nice way of doing that. Only checking from another system. But I think it makes sense to add.

    We have two ways of enabling / disabling. One is explicit enable / disable. The other is implicit based on no entities being relevant for the system. In which case we just dont call OnUpdate as an early out.

    What would you optimally like to see happen in either of those cases? Do you have a proposal for an API you would like to see in the form of usage code you would like to optimally write for your use case.
     
  5. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    210
    With systems being able to be disabled and enabled, it would make a little bit of sense for them to be consistent with the existing GameObject and ScriptableObject in the sense that they have some method callback or an optional override-able method similar to OnCreateManager.

    In this particular scenario, I'd like it to clean up the socket system to disconnect and reconnect gracefully as needed.
    Code (CSharp):
    1. [AlwaysUpdateSystem]
    2. public class SocketSystem : ComponentSystem {
    3.     private override void OnEnable() {
    4.         OpenConnection();
    5.         StartListeners();
    6.     }
    7.  
    8.     private override void OnDisable() {
    9.         CloseConnection();
    10.         StopListeners();
    11.     }
    12.     // Omitted arbitrary socket code
    13. }
    However, that is just a small use case and may be lacking in some nuances to the direction that Unity is heading. Because I am still learning the core concepts of Data Driven Design, I'm not sure what would be the best approach to it, though truth be told, I would likely either just add a method call to the existing Enabled property or at the end of a frame have something inform any system of state changes.
     
    Last edited: Apr 18, 2018
    FROS7 likes this.