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. Dismiss Notice

Question Changing SystemState.Enabled has no effect?

Discussion in 'Entity Component System' started by AlexAdach, Jul 27, 2023.

  1. AlexAdach

    AlexAdach

    Joined:
    Jan 25, 2023
    Posts:
    24
    As the title says. I finished my Monobehavior SystemManager. It has an instance of the below class for each of my systems. The monobehavior component runs GetSystem, SetSystemEnabledState, and UpdateSystemData for each in Update().

    I included fields for SystemName, and WorldName to help debugging. They show the correct system names and "default world". I'm at a lose for why changing the enabled state to false has no affect on the system.

    Code (CSharp):
    1.     [Serializable]
    2.     public class SystemDataRefWrapper<D,S> : ISystemManagerWrapper where D : unmanaged, IComponentData, ISystemManagerData
    3.     {
    4.         [field: SerializeField]
    5.         public bool Enabled { get; set; }
    6.         public bool EnabledFb;
    7.         public string SystemName;
    8.         public string WorldName;
    9.         public bool HasFoundSystem;
    10.  
    11.        
    12.         public D Data;
    13.         private S System;
    14.  
    15.         private SystemHandle _handle;
    16.        
    17.         bool ISystemManagerWrapper.GetSystem()
    18.         {
    19.             if (HasFoundSystem)
    20.                 return true;
    21.  
    22.             var systemTypeIndex = TypeManager.GetSystemTypeIndex<S>();
    23.             _handle = World.DefaultGameObjectInjectionWorld.GetExistingSystem(systemTypeIndex);
    24.  
    25.             if(!_handle.Equals(default(SystemHandle)))
    26.             {
    27.                 HasFoundSystem = true;
    28.                 var state = World.DefaultGameObjectInjectionWorld.Unmanaged.ResolveSystemStateRef(_handle);
    29.                 SystemName = state.DebugName.ToString();
    30.                 var systemWorld = state.World;
    31.                 WorldName = systemWorld.Name;
    32.             }
    33.  
    34.             return HasFoundSystem;
    35.         }
    36.  
    37.         void ISystemManagerWrapper.SetSystemEnabledState()
    38.         {
    39.             var state = World.DefaultGameObjectInjectionWorld.Unmanaged.ResolveSystemStateRef(_handle);
    40.             if(state.Enabled != Enabled)
    41.                 state.Enabled = Enabled;
    42.  
    43.             EnabledFb = state.Enabled;
    44.  
    45.  
    46.         }
    47.  
    48.         void ISystemManagerWrapper.UpdateSystemData()
    49.         {
    50.             if (!EqualityComparer<D>.Default.Equals(Data, default(D)))
    51.                 World.DefaultGameObjectInjectionWorld.EntityManager.SetComponentData<D>(_handle, Data);
    52.         }
    53.     }
    thanks for your help!
     
  2. AlexAdach

    AlexAdach

    Joined:
    Jan 25, 2023
    Posts:
    24
    I forgot to mention,

    If I look at the system entity in the hierarchy, the data component I'm updating from the Monobehavior is updating correctly at runtime.
     
  3. AlexAdach

    AlexAdach

    Joined:
    Jan 25, 2023
    Posts:
    24
    I'm probably just going to make the data components enable able and handle it that way. just wondering what the issue might be.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Changing SystemState.Enabled works for me fine. So either SetSystemEnabledState isn't being called, or something is undoing that action.
     
  5. AlexAdach

    AlexAdach

    Joined:
    Jan 25, 2023
    Posts:
    24
    can you show me how you got a reference to a systemstate?
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
  7. AlexAdach

    AlexAdach

    Joined:
    Jan 25, 2023
    Posts:
    24
    I was missing ref.....

    Code (CSharp):
    1.     [Serializable]
    2.     public class SystemDataRefWrapper<D,S> : ISystemManagerWrapper where D : unmanaged, IComponentData, ISystemManagerData
    3.     {
    4.         [field: SerializeField]
    5.         public bool Enabled { get; set; }
    6.         [SerializeField] private string systemStatus;
    7.         [SerializeField] private string systemName;
    8.         public bool HasFoundSystem;
    9.  
    10.        
    11.         public D Data;
    12.         private S System;
    13.  
    14.         private SystemHandle _handle;
    15.        
    16.         bool ISystemManagerWrapper.GetSystem()
    17.         {
    18.             if (HasFoundSystem)
    19.                 return true;
    20.  
    21.             var systemTypeIndex = TypeManager.GetSystemTypeIndex<S>();
    22.             _handle = World.DefaultGameObjectInjectionWorld.GetExistingSystem(systemTypeIndex);
    23.  
    24.             if(!_handle.Equals(default(SystemHandle)))
    25.             {
    26.                 HasFoundSystem = true;
    27.                 ref var state = ref World.DefaultGameObjectInjectionWorld.Unmanaged.ResolveSystemStateRef(_handle);
    28.                 systemName = state.DebugName.ToString();
    29.             }
    30.  
    31.             return HasFoundSystem;
    32.         }
    33.  
    34.         void ISystemManagerWrapper.SetSystemEnabledState()
    35.         {
    36.             ref var state = ref World.DefaultGameObjectInjectionWorld.Unmanaged.ResolveSystemStateRef(_handle);
    37.             state.Enabled = (Enabled) ? true : false;
    38.  
    39.             systemStatus = (state.Enabled) ? "Enabled" : "Disabled";
    40.         }
    41.  
    42.         void ISystemManagerWrapper.UpdateSystemData()
    43.         {
    44.             if (!EqualityComparer<D>.Default.Equals(Data, default(D)))
    45.                 World.DefaultGameObjectInjectionWorld.EntityManager.SetComponentData<D>(_handle, Data);
    46.         }
    47.     }