Bakers unable to override system changes

Context
I am making a terrain generator that combines layers to generate terrain. Whenever a layer changes, I want to process it in my LayerChangedSystem and update the corresponding chunks.

The first time I perform a change, the layer GameObject is baked to an entity, the LayerChangedTag is added, and the LayerChangedSystem processes the layer. After that, the LayerChangedSystem removes the LayerChangedTag from all processed layers.

If I add a LayerChangedTag to the entity during the baking, I can successfully process the entity, but only once. After the system has disabled the tag, the baker seems incapable of enabling it again. Same problem if I try to add/remove instead of enable/disable. It only works once, even when all other components are updating fine.

Question

Is this a bug or an intended feature? Are there any better ways to mark entities as changed to process them in systems?

Below is the relevant code:

public class LayerBaker : Baker<Layer>
{
    public override void Bake(Layer authoring)
    {
        Entity entity = GetEntity(TransformUsageFlags.Dynamic);

        // Add other components

        AddComponent(entity, new LayerChangedTag());
        SetComponentEnabled<LayerChangedTag>(entity, true);
    }
}
[WorldSystemFilter(WorldSystemFilterFlags.Editor)]
public partial struct LayerChangedSystem : ISystem
{
    EntityQuery layerQuery;

    public void OnCreate(ref SystemState state)
    {
        layerQuery = SystemAPI.QueryBuilder()
            .WithAll<LayerChangedTag>()
            .Build();
    }

    public void OnUpdate(ref SystemState state)
    {
        if (layerQuery.IsEmpty) return;

        var layers = layerQuery.ToEntityArray(Allocator.TempJob);

        // Process layers

        for (int i = 0; i < layers.Length; i++)
        {
            // Disable LayerChangedTag because we are done processing the changed layer
            state.EntityManager.SetComponentEnabled<LayerChangedTag>(layers[i], false);
        }

        layers.Dispose();
    }
}

After some more testing, this is what I have:

Bakers seem unable to override ANY change made by systems. If my system disables the LayerChangedTag, my baker will not be able to enable it. If my system removes the LayerChangedTag, my baker will not be able to add it back. Even if I add a DidChange field to the component and update its value in my system, the baker will never be able to override that again.

What is going on? Are bakers totally incapable of overwriting system changes? How am I supposed to mark entities for update for a single frame?

Bakers only run once, during baking. I don’t think they even execute at runtime. By the time your systems run, the bakers have already executed.

What about live baking/incremental bakes? Every other component that depends on the Layer is updated fine when I change values, just not the components that have been altered by systems.

Also, I am running this in the editor.

I see. I’m not familiar with systems running in the editor. Hope somebody could chime in.

1 Like