Search Unity

Question How to serialize an entity in IConvertGameobjectToEntity?

Discussion in 'Entity Component System' started by Stroustrup, Jun 16, 2020.

  1. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    how do you reference another entity in iconvertgameobjectoentity?

    the playerhead is a child of this gameobject with the capsule controller, how do i get the resulting entity so that i may put it into the PlayerHead component value?

     

    Attached Files:

  2. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    I’ve been doing this short of thing in a GameObjectConversionSystem
    21D603C7-55D8-4E86-8DC4-433721DA258C.jpeg
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Sorry the image is a bit screwy but I GetPrimaryEntity from any proxy go and then reference them where needed in other entities
     
  4. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    doesn't work for my case, my head doesn't have any component. its just some arbitrary child of a skinned mesh

    also why not IConvertGameObjecToEntity for custom authoring for your thing? you have the conversion system there and you can have it appear in the inspector



    im trying to drag my head into my head field on my component

    it works with something like this:
    Code (CSharp):
    1. [GenerateAuthoringComponent]
    2. public struct PlayerHead : IComponentData {
    3.     public Entity value;
    4. }
    but how do you serialize an entity with custom authoring?
     

    Attached Files:

  5. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    dots makes me so angry. why is there no documentation on absolutely anything. why don't google searches yield anything ever. how do i make a public entity field appear in the inspector, how does [GenerateAuthoringComponent] do it
     
  6. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    Well, you can take a look yourself how it is done. upload_2020-6-16_19-12-33.png
     
    Neil-Corre, lclemens and Stroustrup like this.
  7. zardini123

    zardini123

    Joined:
    Jan 5, 2013
    Posts:
    68
    Yeah that's certainly the fun that is learning preview technologies.

    Fortunately for you, there has been some very dedicated individuals who have studied and reversed engineered how DOTS works, and converting that knowledge into readable text. For example, a user on Unity Fourms of the title 5argon has a blog that really breaks apart DOTS and ECS so one can understand it for their own sake. Here is that blog: https://gametorrahod.com/tag/unity-ecs/

    In one of his most recent posts, he dives into Game Object Conversion. Essentially, there are systems to convert GameObjects into entities. Just like regular systems, these conversion systems are sorted into ComponentSystemGroups, so some conversion systems are guaranteed run before or after other conversion system. @francois85 touched on this earlier in this thread.

    TL;DR, if you want to reference a entity that is planning to be converted in the conversion process of another entity, you need to write a standalone GameObjectConversionSystem that is placed into the proper conversion group using [UpdateInGroup]. Please read more here in 5argon's blog about Game Object Conversion: https://gametorrahod.com/game-object-conversion-and-subscene/ Search for GameObjectConversionSystem to jump right to the information (but read the entire thing if you got time).
     
  8. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Thanks, that was really useful!

    For anyone else wondering about doing this... for me this wasn't obvious, but in brunocoimbra's screenshot, EntityReference is a component that you should define... something like:

    Code (CSharp):
    1. public struct HeadEntityData : IComponentData
    2. {
    3.     public Entity characterHead;
    4. }
    It's a lot of extra code for something so simple, but it got the job done. Maybe someday there will be a pure ECS editor that is just for Entities that lets you place and arrange entities and everything is entities and components instead of having to do this monobehaviour-to-entity conversion mumbo-jumbo.

    The good news is that another option is to use a conversion system instead (as zardini mentioned)... which isn't perfect, but getting better.

    A third option, which only works in some cases, is... you could attach three things to your prefab instead of one - your custom authoring component, a component with [GenerateAuthoringComponent], and a convert-to-entity script.