#Enable/Disable Enableable Components in Jobs

1 messages · Page 1 of 1 (latest)

gilded patrol
#

Hello everyone. In the https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/components-enableable-use.html is said that I can

enable and disable components on jobs running on worker threads without using an entity command buffer or creating a sync point.
But when I tried to do that Unity errored (attention to the screenshot) with the messages, says that in the job Job.Complete was called. As I understand right it means not the sync point, but just completing the needed dependencies. But still because of that error I cannot not enable/disable components on jobs. So where am I wrong here?

Code for this jobs is:

{

    [ReadOnly] public CollisionWorld collisionWorld;
    public RaycastInput raycastInput;
    public NativeReference<bool> raycastResult;

    public void Execute()
    {
        raycastResult.Value = collisionWorld.CastRay(raycastInput, out RaycastHit raycastHit);
        if (raycastResult.Value)
        {
            World.DefaultGameObjectInjectionWorld.EntityManager.SetComponentEnabled<SelectTag>(raycastHit.Entity, true);
        }
    }
}

public partial struct DeselectAllUnitsJob : IJobEntity
{
    public NativeReference<bool> raycastResult;
    public void Execute(in SelectTag selectTag, in Entity entity)
    {
        if (raycastResult.Value) 
            World.DefaultGameObjectInjectionWorld.EntityManager.SetComponentEnabled<SelectTag>(entity, false);
    }
}```
odd raptor
#

ah, you can not use World.DefaultGameObjectInjectionWorld.EntityManager this in a job like that

#

EntityManager is for the main thread

gilded patrol
#

So how can I enable/disable enableable components in jobs then?

odd raptor
#

you can use pass in EnabledRefRW<T> to the execute method or use ComponentLook or EntityCommandBuffers

#

you should forget World.DefaultGameObjectInjectionWorld even exists

#

it should never be used inside of entities

#

systems have their own World property (or ISystem has it on state)

gilded patrol
#

so only SystemBase.EntityManager/World or SystemState.EntityManager/World? No other options should be used as it is extremely not efficient?

odd raptor
#

what if you have 2 worlds?

#

you would be using the wrong world in your system

#

systems are associated with a specific world

gilded patrol
#

So that is the only reason for not using World.Default.... right? In efficiency they are not so different, right?

#

Asking just for understanding

gilded patrol
odd raptor
#

from most to least

odd raptor
#

statics dont really belong in entities

gilded patrol
#

So accessing them is slower?

odd raptor
#

speed is not a consideration here (for World)

gilded patrol
#

Okay, got it

odd raptor
#

they will probably all be a magntidue different

gilded patrol
#

Can you also please help with these questions:

  1. Can I get EnabledRefRW without ComponentLookup or IJobEntities or idiomatic foreach?
  2. EnabledRefRO only let me to check if the component is enabled and check the data inside it, right?
odd raptor
#
  1. no, it's specific to those. if you are not in a job you can just do entitymanager.isenabled or something
  2. only lets you see enabled state