#Issue with CreateAdditionalEntity

1 messages · Page 1 of 1 (latest)

quaint peak
#

So I have this Baker that creates two entites, one for the simulation part, one for the visual part

private class MatchActorAuthoringBaker : Baker<MatchActorView>
{
    public override void Bake(MatchActorView view)
    {
        var visualEntity = GetEntity(TransformUsageFlags.Dynamic);
        var logicEntity = CreateAdditionalEntity(TransformUsageFlags.Dynamic);
        
        AddComponent<MatchActor>(logicEntity);
        SetComponent(logicEntity, new MatchActor
        {
            Health = view.Settings.Health,
// sets a few fields...
        });

        view.LogicEntity = logicEntity;
        AddComponentObject(visualEntity, view);
    }
}

But when I try to access the Logic entity through the View later, it always throws the exception

protected override void OnUpdate()
{
    var grid = SystemAPI.ManagedAPI.GetSingleton<GridTerrainView>();
    foreach (var (mov, turnActionEntity) in SystemAPI.Query<RefRO<ProcessedTurnActionMovement>>()
        .WithEntityAccess())
    {
        if (!TryGetActorView(mov.ValueRO.Entity, out var viewEntity))
            throw new InvalidOperationException("No matching view entity found for movement action.");
        // some more code
    }
}

bool TryGetActorView(Entity logicActorEntity, out Entity viewEntity)
{
    foreach (var (view, entity) in SystemAPI.Query<MatchActorView>().WithEntityAccess())
    {
        if (view.LogicEntity == logicActorEntity)
        {
            viewEntity = entity;
            return true;
        }
    }

    viewEntity = Entity.Null;
    return false;
}

Why is this comparison not working? Is there anything special to be aware when working with CreateAdditionalEntities?

tawdry ivy
#

why is this using AddComponentObject?

#

actually why are you adding the authoring component back onto the entity is the better question. I don't think that's usually allowed

quaint peak
#

it's also an IComponentData in this case

    public class MatchActorView : MonoBehaviour, IComponentData
    {
        [field: SerializeField] public int TeamId { get; private set; }

        [field: SerializeField] public MatchActorSettings Settings { get; set; } = null!;

        public Entity LogicEntity { get; private set; }

        private class MatchActorAuthoringBaker : Baker<MatchActorView>
        {
            public override void Bake(MatchActorView view)
            {
                var visualEntity = GetEntity(TransformUsageFlags.Dynamic);
                var logicEntity = CreateAdditionalEntity(TransformUsageFlags.Dynamic);
                
                AddComponent<Team>(logicEntity);
                SetComponent(logicEntity, new Team { Id = view.TeamId });
                
                AddComponent<MatchActor>(logicEntity);
                SetComponent(logicEntity, new MatchActor
                {
                    Initiative = view.Settings.Initiative,
                    Health = view.Settings.Health,
                    MaxHealth = view.Settings.MaxHealth,
                    ActionPoints = view.Settings.ActionPoints,
                    MaxActionPoints = view.Settings.MaxActionPoints,
                    MovementSpeed = view.Settings.MovementSpeed
                });

                if (view.Settings.Health >= 0)
                    AddComponent<IsAliveTag>(logicEntity);

                view.LogicEntity = logicEntity;
                AddComponentObject(visualEntity, view);
            }
        }
    }
quaint peak
#

It looks like the issue is that the additional entity generated during baking is not the same one present in the game world, but I'm still not sure why is that

bright sky
#

AddComponentObject(visualEntity, view); that shouldn't work at all

#

by the time you are in runtime this will be null

bright sky
#

baking world serializes entities in editor time

#

runtime world deserializes entities into their own ids

quaint peak
#

and is there any way to grab the actual entity during baking?

quaint peak
bright sky
quaint peak
#

oh well
and is there a proper way of linking the two objects then?

bright sky
#

well, first of all - authoring objects don't exist after baking

#

they are merely editor only data

#

that never should make it into builds