#[Solved] [Entities 1.0.0-pre.44] Baking problems

1 messages · Page 1 of 1 (latest)

urban nebula
#

Hi, I am migrating my existing project to entities 1.0 and I am running into issues with the new baking workflow, I've got the following file StatisticComponentDataAuthoring.cs

using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;

namespace Simulator.Statistics
{
    public struct StatisticComponentData : IComponentData
    {
        public long Step;
    }

    public class StatisticComponentDataAuthoring : MonoBehaviour
    {
        public long Step;
    }

    public class StatisticComponentDataBaker : Baker<StatisticComponentDataAuthoring>
    {
        public override void Bake(StatisticComponentDataAuthoring authoring)
        {
            var statistic = new StatisticComponentData
            {
                Step = authoring.Step
            };

            AddComponent(statistic);
        }
    }
}

And I want to use the created entity in a system like this:

        protected override void OnStartRunning()
        {
            base.OnStartRunning();
            _statisticEntity = SystemAPI.GetSingletonEntity<StatisticComponentData>();
        }

This is however giving me an error on runtime:

It just doesn't make sense to me at all.

#

[Entities 1.0.0-pre.44] Baking problems

heady root
#

you have to wait for the scene to load before calling GetSingletonEntity

#

you can RequireForupdate<StatisticComponentData>

urban nebula
#

Thank you so much

urban nebula
#

Alright now it is no longer throwing an exception but it seems to be not running anymore.. I've added a quick log statement to the OnUpdate of said system but no messages in the console :s

heady root
#

and the scene is actually loading? can you see the entities from it in the entities window?

urban nebula
#

Sorry but what is the entities window? The system shows up in the systems window

heady root
#

window ->entities -> components

urban nebula
#

Its there

heady root
#

that's pretty weird

#

i can't actually see the image of the error message you posted originally, btw, because discord didn't work

#

maybe you could click and hten highlight and paste instead?

urban nebula
#
using System;
using System.Collections.Generic;
using System.Linq;
using Simulator.Configuration;
using Simulator.Curves;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;

namespace Simulator.Statistics
{

    public class Statistic
    {
        public EntityQuery Query;
        public string Name;
        public float Value;
        public Dictionary<string, object> StatisticBag = new Dictionary<string, object>();
        public Action<StatisticSystem, Statistic> Init;
        public Action<StatisticSystem, Statistic> PreAggregator;
        public Action<StatisticSystem, Statistic, Entity> Aggregator;
        public Action<StatisticSystem, Statistic> PostAggregator;
    }

    [UpdateInGroup(typeof(StatisticSystemGroup))]
    public partial class StatisticSystem : SystemBase
    {
        private SimulationConfigurationComponent _simulationConfiguration;

        private List<Statistic> _statistics = new List<Statistic>()
        {
            Metrics.StatisticStep,
            Metrics.AverageEnergy,
            Metrics.NumberOfBoids,
            Metrics.NumberOfFoodSources,
            Metrics.TotalFoodAvailable,
            Metrics.Polarization,
            Metrics.Expanse
        };

        private Entity _statisticEntity;
        private StatisticWriter _statisticWriter;

        // Please close your eyes before looking at the next 3 lines of code
        public new EntityQuery GetEntityQuery(params ComponentType[] componentTypes)
        {
            return base.GetEntityQuery(componentTypes);
        }

        protected override void OnCreate()
        {
            base.OnCreate();
            var id = DateTime.UtcNow.ToString("yyyy-MM-dd-HH-mm-ss");
            _statisticWriter = new StatisticWriter(id, _statistics.Select(s => s.Name).ToArray());

            RequireForUpdate<StatisticComponentData>();
        }

        protected override void OnStartRunning()
        {
            base.OnStartRunning();
            _simulationConfiguration = SystemAPI.GetSingleton<SimulationConfigurationComponent>();

            _statisticEntity = SystemAPI.GetSingletonEntity<StatisticComponentData>();

            // Init all statistics
            foreach (var statistic in _statistics)
            {
                statistic.Init?.Invoke(this, statistic);
            }
        }

        protected override void OnUpdate()
        {
            Debug.Log("StatisticSystem.OnUpdate()");
            // Pre-aggregator
            foreach (var statistic in _statistics)
            {
                statistic.PreAggregator?.Invoke(this, statistic);
            }


            // Aggregator
            foreach (var statistic in _statistics)
            {
                var query = statistic.Query;
                var aggregator = statistic.Aggregator;
                var entities = query.ToEntityArray(Allocator.TempJob);
                foreach (var entity in entities)
                {
                    aggregator?.Invoke(this, statistic, entity);
                }
                entities.Dispose();
            }

            // Post-aggregator
            foreach (var statistic in _statistics)
            {
                statistic.PostAggregator?.Invoke(this, statistic);
            }

            // Create list of all statistic values
            var values = new List<float>();
            foreach (var statistic in _statistics)
            {
                values.Add(statistic.Value);
            }

            _statisticWriter.Write(values.Select(x => x.ToString()).ToArray());

            var step = EntityManager.GetComponentData<StatisticComponentData>(_statisticEntity).Step;
            EntityManager.SetComponentData(_statisticEntity, new StatisticComponentData { Step = step + (long)_simulationConfiguration.MaxSimulationSpeed });
        }
    }
}

This is the full class

#

Alright, 2 seconds.

#

The error is gone now by the way, but ill get it back

#
InvalidOperationException: GetSingleton<Simulator.Configuration.SimulationConfigurationComponent>() requires that exactly one Simulator.Configuration.SimulationConfigurationComponent exist that match this query, but there are 0.
Unity.Entities.EntityQueryImpl.GetSingleton[T] () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/Iterators/EntityQuery.cs:1447)
Unity.Entities.EntityQuery.GetSingleton[T] () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/Iterators/EntityQuery.cs:2657)
Simulator.Statistics.StatisticSystem.OnStartRunning () (at Assets/Scripts/Statistics/StatisticSystem.cs:62)
Unity.Entities.SystemBase.Update () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/SystemBase.cs:415)
Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:703)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:708)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:667)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/SystemBase.cs:418)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:703)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:661)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/SystemBase.cs:418)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ScriptBehaviourUpdateOrder.cs:526)
#

If I switch the following lines in the file:

_simulationConfiguration = SystemAPI.GetSingleton<SimulationConfigurationComponent>();

_statisticEntity = SystemAPI.GetSingletonEntity<StatisticComponentData>();

I get a slightly different stacktrace

InvalidOperationException: GetSingleton() requires that exactly one entity exists that matches this query, but there are 0.
Unity.Entities.EntityQueryImpl.GetSingletonChunk (Unity.Entities.TypeIndex typeIndex, System.Int32& outIndexInArchetype, Unity.Entities.Chunk*& outChunk) (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/Iterators/EntityQuery.cs:1349)
Unity.Entities.EntityQueryImpl.GetSingletonEntity () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/Iterators/EntityQuery.cs:1297)
Unity.Entities.EntityQuery.GetSingletonEntity () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/Iterators/EntityQuery.cs:2641)
Simulator.Statistics.StatisticSystem.OnStartRunning () (at Assets/Scripts/Statistics/StatisticSystem.cs:63)
Unity.Entities.SystemBase.Update () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/SystemBase.cs:415)
Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:703)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:708)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:667)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/SystemBase.cs:418)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:703)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ComponentSystemGroup.cs:661)
Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/SystemBase.cs:418)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.44/Unity.Entities/ScriptBehaviourUpdateOrder.cs:526)
heady root
#

i wonder if you actually have more than one

#

for example is that thing maybe on a prefab or something

urban nebula
#

It is not on a prefab

#

Just a plain Gameobject

heady root
#

if you click on it in the component window iirc you get a list of entities it's on

#

yeah click, and then in the inspector go to Relationships

urban nebula
#

Says its on 0 entities

heady root
#

well that's good actually

#

means the error is correct

urban nebula
#

Also makes sense that the system isnt updating 😂

heady root
#

yeah

#

do you actually have the authoring on a gameobject in the subscene?

urban nebula
#

I think so, that would be this right?

heady root
#

no

#

that authoring component

#

does that gameobject live in a subscene?

urban nebula
#

I dont think it does, it just lives in the scene root

#

I don't quite know what subscenes are to be honest, Ill go look it up

#

Hierarchy looks like this

#

If that helps

heady root
#

ah

#

subscenes are the only time that authoring components get baked into entity components

urban nebula
#

Oh okay

heady root
urban nebula
#

Ill have a look at that, thank you =)

urban nebula
#

Or did I just miss it in the baker docs

heady root
#

i think you just stopped reading after Convert data with Baking

#

since the next section is Subscenes

#

that said, we should scream more loudly that you will not get anything converted whatsoever if you don't put your crap in a subscene

urban nebula
#

Yeah probably, I mean subscenes sounds like something I would need if I wanted to make something big and im only really building a POC

grizzled sigil
#

Kind of needed if you're making anything as you can't bake any other way

urban nebula
#

To follow up on this - In previous versions it seemed to be fine to spawn entities from the Start() function in a monobehaviour, this seems broken now - I'd probably have to go for a system which does it instead right?

#

I assume that this has to do with the "waiting for a scene to load" thing

heady root
#

i'm not as sure about that; what breaks about it?

urban nebula
#

I see that in the boids sample they create the boids in a system build specifically for that - I was doing it in the start function of a monobehaviour used to work before, broken now

#

I was basically creating a prototype and then instantiating it multiple times

grizzled sigil
#

are you trying to instantiate from an entity within a subscene?

urban nebula
#

Not from an entity from a regular monobehaviour

#

both are in the same subscene

grizzled sigil
#

I'm confused then

#

Gameobjects in subscenes are converted to entities

#

They no longer exist at runtime*

urban nebula
#

I have not written a custom baker or anything for it

grizzled sigil
#

if it's in a subscene, the gameobject doesn't exist at runtime*

urban nebula
grizzled sigil
#

(* is because there is this whole companion thing for certain types but I'm just going to ignore that for this convo)

#

Any gameobject put in a subscene will be converted to an entity, at bare minimum just its transform

#

If you put components on it that aren't converted, they will just be ignored

urban nebula
#

Alright so my issue was that I put my monobehaviour into a subscene, which then converted it to an entity and I guess that it didnt fire the regular monobehaviour events anymore

grizzled sigil
#

Correct!

urban nebula
#

so it was never executing the boid spawning portion, I've now moved to the root of the scene and it seems to fire again

#

The entities exist now, they just dont seem to show up visually in the editor, probably some issue with the material or mesh or something

grizzled sigil
#

in game or scene?

#

or both?

urban nebula
#

Both

#

I can see them in the entities inspector though

#

Probably going to have to read up on the graphics package, something must have changed

#

this upgrade sure is taking a lot of time though

grizzled sigil
#

take peace in the knowledge that at least when you're done, you'll have a more stable API going forward

urban nebula
#

Got it to work, was missing a parameter in RenderMeshUtilities.AddComponents: MaterialMeshInfo materialMeshInfo even though it has a default parameter, I had to set it to MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0) which seems like a default parameter to me anyway