[1.0.0-exp.8] Baker not adding LinkedEntityGroup?

Hi there, i had some troubles instancing some entities of my scene, due to the children were not copied. I found out, that the EntityManager.Instantiate Method uses the LinkedEntityGroup, that was not there.

My entities get a Disabled component directly in the Baking, so they are not processed by the usual system, that adds that afterwards. The Entities just have Parent component and children buffers, but no LinkedEntityGroup.

As a workaround, i could do that before instanciating them on runtime, but it felt better to have that in the bake.
So my first question: Is this a possible bug, or is there any reason, why the LinkedEntityGroup is not part of the default Baking?

As a workaround, i tried to create a BakerSystem, that fixes this. I found, that there are no Children in the Baker, so i tried to figure out how to make my System running after the ones who creating the child buffers, but i couldn’t figure out where this is. (seems not visible in the Systems Window? or is this done separate?)
So my second Question: Any Docu about the BakerSystem with some useful examples?
Ps.: My first try was to use EntityManager.CreateQuery(…).toEntityArray() That failed with a nullpointer, so seems at least need some docu what can be used there :slight_smile:

However, my Workaround for that was now to add also the childs by myself, is there a better way?

[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
partial class FixLinkedGroupEntityBakingSystem : SystemBase {
    protected override void OnUpdate() {
        Entities.WithStructuralChanges().ForEach((Entity entity, in Parent parent) => {
            var children = EntityManager.GetOrAddBuffer<Child>(parent.Value);
            // To see when we don't need the workaround anymore, we add this logging.
            var c = children.GetEnumerator();
            while (c.MoveNext())
                if (c.Current.Value == entity) {
                    Debug.Log("Seems we don't need to do this anymore. Child aready exists.");
                    return;
                }
            children.Add(new Child() { Value = entity });
        }).Run();
        Entities.WithStructuralChanges().WithNone<LinkedEntityGroup>().WithAll<Child>().ForEach((Entity entity) => {
            var buffer = EntityManager.AddBuffer<LinkedEntityGroup>(entity);
            foreach (var linkedChild in EntityManager.FindAllChilds(entity, Allocator.Temp).DisposeToSimpleArray())
                buffer.Add(new LinkedEntityGroup() { Value = linkedChild });
        }).Run();
    }
}

LinkedEntityGroup is only added to Prefabs. (GameObjects that have the prefab flag and are at least touched once via the GetEntity method)
They are not available in subscene gameobjects that have a hierarchy. There you can find Parent/Child. No idea, why, really.

The baking process adds the internal LinkedEntityGroupBakingData and is processed at a later stage. One that runs after a BakingSystem.

Sidenotes:
LinkedEntityGroup has quite the peculiar design. I think it should be changed so we can control it if it’s added or not. Sometimes it is needed on subscene gameobjects or prefabs. Sometimes it’s not. (That’s debatable. I like setting up my own references. It’s great for not worrying about destroying a hierarchy of entities.) There’s really no general case where it’s needed all the time outside of destroying. A lightweight Parent/Child approach is more reasonable though and still quite fast.
I’ve been writing baking systems that removes LinkedEntityGroup as it’s even added on flat prefabs. That should not be! :smile:

Also, LinkedEntityGroup should live outside the chunk and have an [InternalBufferCapacity] of 0. It’s very likely it’s only used rarely, for init and destroy so it really has no purpose of using up precious chunk memory. It also shouldn’t have a default cap of 8.

There is a Linked Entity Group Authoring script you can attach to scene Game Objects.

3 Likes

Year, its a big mess ^^ My main pain point for that hierarchy is, that when i change something in the hierarchy, it takes usually 1 frame to update that. Destroying the entity before unities update system is running, let entities left over or bring errors for not existing entities. So i already added a lot of utils methods for that purpose to make it more stable and keep always a consistent values.

For me its similar. when i see my usages, i usually use parent and child, just need always keep track of that LinkedEntityGroup. Maybe it’s going to be obsolete soon?

Right, there is a LinkedEntityGroupAuthoring, that fixes that. But why that’s not a default O.o

It seems even stranger. I have now a Prefab added to the subscene, and inside of that it seems to be added. But there i DON’T want it, due to its a part of another entity… What is going on here :face_with_spiral_eyes: