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 How properly activate an action on entities from an IJobEntity

Discussion in 'Entity Component System' started by MatanYamin, Jul 19, 2023.

  1. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109
    Hey everyone,

    My question is simple.

    When a player get close to an entity it should do something.

    I have an IJobEntity that runs on all Entities with a component that have:
    Code (CSharp):
    1.  
    2. public struct ExampleComponent : IComponentData
    3. {
    4.      public byte entityState;
    5. }
    6.  
    When the player is close to an entity, I change it's "entityState" to 1.

    and I constantly check:
    Code (CSharp):
    1.  
    2. public void Execute(in ExampleComponent data)
    3.         {
    4.             if (data.entityState == 1)
    5.             {
    6.                 // Do Something...
    7.             }
    8.         }
    9.  
    I was wondering if that approach is legit and is conventional for a situation where sometimes we need to do something with some of the entity but not all the time.

    I will be thrilled to hear suggestions on how to improve this.
    Thanks!
     
    Last edited: Jul 19, 2023
    apkdev likes this.
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    941
    IEnableableComponent was made for this.
     
    MatanYamin and apkdev like this.
  3. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109
    That exactly what I needed! I don't know how I wasn't aware of this.
    Thanks!