Search Unity

Issues with non active objects

Discussion in 'Entity Component System' started by Held0fTheWelt, Jan 3, 2019.

  1. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    Hello there,

    I am receiving some issues with working with the ecs right now, i didn't expect and cannot really explain, so i am low with workarounds.
    I have implemented the new Input System into an InputComponentSystem, which is capable to receive all that inputs and put it into a seperate PlayerController, which has nothing more in it, than just the input and a given id.

    Then i have several entities inside the area, which share the same id and receive input from another system, which i can easily swap around.

    Somehow everything works ...
    But it only works, if i have the component, that should receive input active in the hierarchy.
    Whenever i loose focus in hierarchy it stops working.

    And - if you can imagine - when build into a game, i have no hierarchy anymore, where i would focus objects.

    So it looks like i show in the small youtube video down here, and i can't really say, what's going on.



    The ComponentWrapper are no voodoo, and the component itself also just holds float...

    Code (CSharp):
    1. [System.Serializable]
    2. public struct UpdateFloatValue : IComponentData
    3. {
    4.     [SerializeField]
    5.     public float Value;
    6.  
    7.     public UpdateFloatValue(float value)
    8.     {
    9.         Value = value;
    10.     }
    11. }
    12.  
    13. public class UpdateSliderComponent :  ComponentDataWrapper<UpdateFloatValue>
    14. {
    15.     Slider slider;
    16.     private void Start()
    17.     {
    18.         slider = GetComponent<Slider>();
    19.     }
    20.  
    21.     void LateUpdate()
    22.     {
    23.         float sliderValue = (Value.Value + 1)*.5f;
    24.  
    25.         slider.value = sliderValue;
    26.     }
    27. }
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    From memory this doesn't work

    Code (CSharp):
    1.         float sliderValue = (Value.Value + 1)*.5f;
    2.         slider.value = sliderValue;
    The value field on the ComponentDataWrapper is only updated when you have the entity selected. It's for viewing/debugging only.
     
  3. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    So what would i want to do instead with it to receive my UI ?

    I understand the problem, but not the solution.

    Thank you for answering !
     
    Last edited: Jan 3, 2019
  4. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine.UI;
    3.  
    4. public class UpdateTextSystem : ComponentSystem
    5. {
    6.     unsafe public struct TextGroup{
    7.         public Text Text;
    8.         public UpdateFloatValue* UpdateFloatValue;
    9.     }
    10.  
    11.     unsafe protected override void OnUpdate()
    12.     {
    13.         var entities = GetEntities<TextGroup>();
    14.         for (int i = 0; i < entities.Length; i++)
    15.         {
    16.             var entity = entities[i];          
    17.             entity.Text.text = entity.UpdateFloatValue->Value + "";
    18.         }
    19.     }
    20. }
    21.  
    This does the job.

    But i really really don't know, if i do good, doing this...

    Is it usual to have unsafe code for various situations ???
     
  5. riskparitygawd

    riskparitygawd

    Joined:
    Sep 22, 2018
    Posts:
    21
    It is fine to access something like this through Monobehaviour and then set the values with EntityManager. Here's an example where the player in my game shoots with a mono component and damages entities.

    Code (CSharp):
    1. void DamageEntity(GameObject entityGO){
    2.         var entity = entityGO.GetComponent<GameObjectEntity>();  
    3.         var target = entity.EntityManager.GetComponentData<Health>(entity.Entity);
    4.         target.value -= 15f; //-weapon_dmg
    5.  
    6.         if(target.value <= 0 ){
    7.             entity.EntityManager.AddComponentData(entity.Entity, new DeadUnit{});
    8.             entityGO.layer = 14;
    9.         }
    10.         else{
    11.             entity.EntityManager.AddComponentData(entity.Entity,new DamagedAnimation{});
    12.         }
    13.         entity.EntityManager.SetComponentData(entity.Entity,target);
    14.     }
     
    Held0fTheWelt likes this.