Search Unity

Question How to Bake IEnableableComponent to start false?

Discussion in 'Entity Component System' started by CodeMonkeyYT, Nov 6, 2022.

  1. CodeMonkeyYT

    CodeMonkeyYT

    Joined:
    Dec 22, 2014
    Posts:
    125
    Apparently the only way to modify a IEnableableComponent is through SetComponentEnabled

    How do I access that in the Baker.Bake(); function? How do I start an entity with enabled = false?

    Somewhat related, how can I access the Entity where the Baker is running on? How can I access EntityManager from Baker?
     
    Walter_Hulsebos likes this.
  2. Kmsxkuse

    Kmsxkuse

    Joined:
    Feb 15, 2019
    Posts:
    306
    The process right now is a bit rough but possible.

    1. Make sure the subscene is closed in the editor. The enabled state will not save if its open.

    2. Optional: Create a temporary baking component to indicate which entities with enabled component should have their component enabled. Use [TemporaryBakingType] on the component to ensure it gets stripped at the end of bake automatically.

    3. Create a baking system. Basically a system with baker world flag. And do a standard for each or ijob querying for either the enabled component or your temporary baking component type and set it enabled there.

    Basically, make a system that does it. There's no way for an enabled component to be enabled from a baker as of 1.0 exp 13. There will be one with 1.0 preview though.
     
  3. Pek1ng

    Pek1ng

    Joined:
    Dec 5, 2021
    Posts:
    4
    You can do this

    Code (CSharp):
    1. public struct Target : IComponentData, IEnableableComponent
    2.     {
    3.  
    4.         public float3 Position;
    5.  
    6.         public Entity Entity;
    7.     }
    8.  
    9.  
    10. public class PlayerAuthoring : MonoBehaviour
    11.     {
    12.         public class PlayerBaker : Baker<PlayerAuthoring>
    13.         {
    14.             public override void Bake(PlayerAuthoring authoring)
    15.             {
    16.                 AddComponent<Target>();
    17.                 SetComponentEnabled<Target>(GetEntity(), false);
    18.             }
    19.         }
    20.     }
     
  4. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700
    How do you do this for a component that isn't added by your own Baker? I have trouble with that a lot. :)
     
  5. topher_r

    topher_r

    Unity Technologies

    Joined:
    Jun 14, 2019
    Posts:
    36
    Can you share why you want to do it with a different Baker? What is the setup you have?
     
  6. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700
    Actually my questions is regarding modifying *any* ComponentData (enableable or not) in a Baker that isn't the Baker.
    In particular, the problem is with PhysicsBakers, but also whatever bakes RenderMesh / MaterialMeshInfo.

    Example: I have an entity that's on a physical trajectory at start, currently, my Baker has to modify the PhysicsBody Component's InitialLinearVelocity on the Authoring object, it can't add its own PhysicsVelocity or change an existing PhysicsVelocity.

    This is exacerbated when the ComponentData is actually an IEnableable.