#How to add a managed component to an Entity?

1 messages · Page 1 of 1 (latest)

sand sequoia
#

Hi, the tutorial here https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/components-managed-create.html
tells me, that if I want a managed component (I want it to point to a GameObject that I copy the position of the entity to every frame), I just need to do public class ExampleManagedComponent : IComponentData instead of using a struct.

But when I then do AddComponent, I get an error as this expects a component with only unmanaged data. Does anyone know how to proceed?

cursive granite
#

try AddComponentObject?

open ferry
#

Kind of on this topic, in 1.0 is there even much difference between a class ICD and just a class being attached to an entity

cursive granite
#

what would the latter mean if it wasn't an icd

open ferry
#

I can attach
Class : MonoBehaviour
to an entity

cursive granite
#

oh yeah we have a special case for unityengine.object

#

to make that also work with addcomponentobject i believe

#

i think if it's literally just a class that's neither icd nor unityengine.object then it won't work

#

i don't totally remember how exactly it works though 🙂 i've just been through a lot of code that has an "oh yeah if it's unityengine.object do this" case

open ferry
#

i'm just curious why the interface is needed, i guess it's just for typemanager optimizations?

#

so it's known ahead of time?

cursive granite
#

yes, we register everything that inherits from icd up front in a big table and give it a type index

#

and you probably don't want us registering every type in the whole world that has nothing to do with us

open ferry
#

anyway side note
for class ICD it's
AddComponentData

#

still using old name

#
        /// <summary>
        /// Adds a managed component to an entity and set the value of that component.
        /// </summary>
        /// <remarks>
        /// Adding a component changes an entity's archetype and results in the entity being moved to a different
        /// chunk.
        ///
        /// **Important:** This method creates a sync point, which means that the EntityManager waits for all
        /// currently running jobs to complete before adding the component. No additional jobs can start before
        /// the method is finished. A sync point can cause a drop in performance because the ECS framework might not
        /// be able to use the processing power of all available cores.
        /// </remarks>
        /// <exception cref="ArgumentException">The <see cref="Entity"/> does not exist.</exception>
        /// <param name="manager">This entity manager.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="componentData">The data to set.</param>
        /// <typeparam name="T">The type of component.</typeparam>
        public static void AddComponentData<T>(this EntityManager manager, Entity entity, T componentData) where T : class, IComponentData, new()```
and they're added as extension methods
cursive granite
#

right, somebody has a proposal to try to rename everything to be more consistent

#

i'm open to opinions on whether that's worth the upheaval

open ferry
#

AddComponentObject works but says
/// The method also works for adding managed objects implementing IComponentData, but AddComponentData is the preferred method for those objects.

cursive granite
#

heh, TIL

open ferry
#

personally i'm a fan of consistent naming

#

and while it's breaking it's literally a 30 second find/replace fix

cursive granite
#

i have a suspicion that your tolerance for that might be on the high side

open ferry
#

definitely true

cursive granite
#

but still, opinion noted

open ferry
#

i thought the unity updater could handle this very basic renaming though