Search Unity

Official New SubScene & ConvertToEntity workflows

Discussion in 'Entity Component System' started by Joachim_Ante, Mar 4, 2019.

  1. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Hello,
    I Need some heelp with the conversion workflow. How would one get the reference to an entity on another entity from the inspector.

    Setup :
    In my hierarchy i have two game objects A and B both with the convert to entity component. Now I had a proxy to the game object A and add a serialized field of type game object. In the inspector I populate this field with the game object B.

    Goal :
    When converting A I want to set the Entity created by the convertion of B inside a custom component data on A.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  3. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Hi, thanks for your answer but when I use this, It just create a new empty entity instead of giving me the reference to the enntity created when converting the object from the hierarchy.

    My code to reference the game object

    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. using UnityEngine;
    4.  
    5.  
    6. public class TestRefEntityFromHierarchy : MonoBehaviour, IConvertGameObjectToEntity
    7.  
    8. {
    9.  
    10.  
    11.     [SerializeField] private GameObject go;
    12.  
    13.  
    14.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    15.  
    16.     {
    17.  
    18.        Entity e = conversionSystem.GetPrimaryEntity(go);
    19.  
    20.  
    21.  
    22.        EntityRef refEntity = new EntityRef()
    23.  
    24.        {
    25.  
    26.            entity = e
    27.  
    28.        };
    29.  
    30.  
    31.       dstManager.AddComponentData(entity, refEntity);
    32.  
    33.     }
    34.  
    35. }

    The game object used as reference :

    upload_2019-4-28_23-6-34.png

    The game object referencing the first one :
    upload_2019-4-28_23-6-42.png

    The hierarchy :
    upload_2019-4-28_23-6-46.png


    Entity debugger results :
    upload_2019-4-28_23-6-49.png

    Entity 1 : is the empty entity created by
    Code (CSharp):
    1. Entity e = conversionSystem.GetPrimaryEntity(go)
    Entity 0 : it's the TestGoConvertRefToEntity

    Entity 2 : it's the Player
    upload_2019-4-28_23-6-56.png
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Could you create a small project folder that reproduces the issue?
     
  5. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Here you go :
     

    Attached Files:

  6. Ferazel

    Ferazel

    Joined:
    Apr 18, 2010
    Posts:
    517
    I fully understand that this GameObject to entity conversion workflow is going to be needed by a lot of hybrid projects going forward. I also hope/request there will be some love given to the pure ECS Editor workflow too. I thought project tiny's approach to component data creation was interesting for example. I always envisioned that entities would co-exist with GameObject in the sense of the object hierarchy workflow within Unity. Even though entities don't necessarily have transforms, I still want to be able to see them "exist" in the same space as my GameObjects.

    Are there any near-term plans of getting entities to exist in the same Unity workflow as game objects or is the primary focus to keep them separate and have converters? I'm guessing there are reasons I'm not considering for this decision, but as a casual learner/follower of ECS it is a little disappointing the future of coding within the engine seems shoe-horned in with bootstrappers and converters right now. Which was fine a year ago when things were much less mature, but it seemingly hasn't gotten better from a newbie's perspective.

    Having said that, I DO appreciate you taking the time to get things right. The recent API changes are a good example of this in my opinion.
     
    Last edited: Apr 29, 2019
    FROS7 likes this.
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    We are working on both approaches.
    A new release of DOTS Editing tools (Primarily for Tiny use cases) built on top of Unity.Entities is coming soon.
     
    Last edited: Apr 29, 2019
  8. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    I'll continue old topic here as I don't want to gravedig the old one :) I asked last december:
    I got answer:
    Now that conversion workflow is there, it still doesn't answer my original underlying question:
    Once we get full ECS editor, how can we do hybrid? Considering DOTS roadmap is planned to 2022 atm, it doesn't seem like pure ECS will be in scope of anything but Tiny for a long time right now. If we could edit both scene types at once on the same project, it would make the ECS/DOTS editor super useful from day one already.

    Just to make sure, I don't mean there would need to be anything like gameobjects showing on DOTS editor, I'm mainly asking if it's possible to have both editors present on the same project to some extent?

    For example even if you could only view one side at the time, this would solve the issue with hierarcy/inspector duplicating. You could add for example a button on editors top bar (where the play/step/pause, local/global etc buttons are) to swap the editor between DOTS and gameobject scene on the fly. That would probably be easiest way to achieve the both editors since I feel the duplicate side-by-side ecs & gameobject scene views with hierarchy and inspector panels isn't going to happen due to technical limitations.

    Edit: It could be a dropdown too if it would double as option to select different ECS Worlds with it. In case of dropdown, ability to hotkey these would be crucial. Imagine a dropdown menu where first entry is the old gameobject scene and then following entries would be existing ECS worlds.
     
    Last edited: Apr 29, 2019
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Yes. Thats the goal of the new DOTS editor.
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    They need to be in the same "ConvertToEntity group" meaning they need to be in the same hierarchy with a single ConvertToEntity. I'll log a bug about giving warnings when this goes wrong.
     
    WAYNGames and FROS7 like this.
  11. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Ok, so basicly create an empty with the convert entity and do all my entities under it so that I can reference anything from anything.
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    yes
     
    WAYNGames likes this.
  13. astrogee

    astrogee

    Joined:
    Feb 4, 2018
    Posts:
    16
    @Joachim_Ante Thank you for answering questions so dutifully in this thread; as a newbie, I appreciate that very much. Also as a newbie, I struggle to keep up with what is currently the best workflow when starting a project from scratch, especially if I want to do pure ECS. While I believe I have a good enough understanding of ECS as a concept, I'm not sure at all how do implement stuff.

    I had a look at all the samples and I'm confused by the use of GameObjectEntity/CopyTransformFromGameObject/etc in Boids and ConvertToEntity in HelloECS. Both samples seem to be hybrid ECS(?).

    So I guess my question is this. Is it possible to do pure ECS in Unity 2019.1, or do I have to wait for that "Entity Editor" I keep reading about? If possible, what is the workflow and is it even advisable to start using it now?

    Sorry if these questions are very basic - I am just a happy amateur with limited Unity experience :D
     
  14. Knightmore

    Knightmore

    Joined:
    May 11, 2012
    Posts:
    227
    Is it possible to use IConvertGameObjectToEntity for another custom world seperate to the default world? Couldn't find any reference to convert a gameobject for a specific worlds entity manager.
     
  15. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    GameObjectConversionUtility.ConvertGameObjectHierarchy(GameObject, DestinationEntityManager);
     
    JesOb and Knightmore like this.
  16. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Hi I'm myself playing with the ECS convertion workflow. I took a look at project tiny but it does not support 3d (yet ?).
    As doing pure ECS, appart from setting things up diferently, I don't see a difference at runtime between "Pure" ECS and "converted" entity.
    I made a basic Pong game with the full DOTS stack and new input system . At runtime I have all my "game object" as entity and they are managed through job systems (except for the camera and UI). It builds for windows and works well but not for html5. You can take a pick at the code here if you want (it stil lis under work and needs refactoring and comments)

    I'm also curious to know if there is any diference at runtime.
     
    Last edited: May 18, 2019
  17. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    Something is not clear for me.
    Can Subscenes be used as save games?
    I need to save all the entities to a file and later load it to continue the exact same game.
     
    NotaNaN likes this.
  18. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Hi, like I said I made a Pong game and I wanted to host it on a git page so build in HTML5. Problem is the build is successful and does open as local host but It contains only by UI not any entities.
    Also as said previously It work fine with a windows build.

    I'm using 3D game objects converted through the conversion workflow and an orthographic camera and my jobs are using the burst compiler and new unity physics (not havok).

    Does any one else has this issue ? I know (more like assume) ECS is compatible with html5 (e.g. project tiny) so I don't see what I'm missing...
     
  19. psuong

    psuong

    Joined:
    Jun 11, 2014
    Posts:
    126
    It's possible to do a pure approach, but the problem I see are limitations of what's available - especially if you want to use already built in features that are effectively MonoBehaviour-like (e.g the Animator). The conversion workflow is a very good path into a pure workflow, mainly because the systems you write will be purely on the entities with minimal usages of pure references. I think the key here is to think about the kind of game you want to try and prototype and figure out your structure of data first, once you get the hang of it, working in an ECS workflow can be quite simple.

    GameObjectEntity still exists (and yes it is a Hybrid workflow :) ), but the recommended workflow is the converted one. (I remember seeing @tertle ask the question somewhere in this thread and @Joachim_Ante said "Yes" to it). The boids example was their earlier iterations and the HelloECS samples in the Github repo are their more updated ones.
     
  20. fps_bill

    fps_bill

    Joined:
    Aug 14, 2015
    Posts:
    4
    Is there / will there be a way of discarding stuff from getting converted that's inside a sub scene? Maybe a way of post processing the results?
     
  21. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    [WorldSystemFilter(WorldSystemFilterFlags.Default | WorldSystemFilterFlags.EntitySceneOptimizations)]

    See LodRequirementsUpdateSystem for example.

    You can run systems that run during the bake step, in this case LodRequirementsUpdateSystem runs in both the default world and during conversion.

    In that system you can do whatever, including deleting any entities you like.
     
    FROS7 likes this.
  22. fps_bill

    fps_bill

    Joined:
    Aug 14, 2015
    Posts:
    4
    Thanks! I'll check it out
     
  23. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    Is there any way to create references to converted in-scene game objects? Like, I need to access entity of another converted game object.. While it's being converted? Somewhat like that. Referenced prefab declaration does not seem an option, since that reference is not a prefab
     
  24. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Answer from joachim does work but you need to have them in the same convert group. So create an empty with the convert entity component and then place your other object as child of this one ( no need for convert entity on the childs)
     
    Sarkahn and Orimay like this.
  25. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    Very cool, thank you!
     
  26. foolmoron

    foolmoron

    Joined:
    Jun 22, 2013
    Posts:
    41
    Is there an issue with converting prefabs in webgl?

    I have this basic code that instantiates an entity that was converted from a prefab, and sets a component. The prefab has a Translation component on it and this works on every platform, except in webgl it crashes on the SetComponentData line with "memory access out of bounds".
    Code (CSharp):
    1. var e = World.Active.EntityManager.Instantiate(entity);
    2. World.Active.EntityManager.SetComponentData(e, new Translation { Value = new Vector3(Mathf.Lerp(SpawnPositionMin.x, SpawnPositionMax.x, Random.value), Mathf.Lerp(SpawnPositionMin.y, SpawnPositionMax.y, Random.value), Mathf.Lerp(SpawnPositionMin.z, SpawnPositionMax.z, Random.value)) });
    I changed the code so that it checks if the Translation component exists, and adds it if doesn't. That shouldn't be necessary at all since it should be in the prefab, but it avoids the crash. BUT then it doesn't render the entity at all. The physics examples don't render in webgl either.

    So I'm wondering if there is a larger issue here, where the prefab conversion just isn't working in webgl. The prefab should have Translation, and a Mesh Renderer, and all these other components, but it just doesn't? And that causes crashes on SetComponentData, and meshes not rendering.
     
    coldasfrost979 likes this.
  27. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Hi, I created this script and add it to all the game object I convert to entities.
    It just set the name of the entity to the name of the game object. It help me a lot to find things in the entity debugger.

    Any chance It would be added as part of the default convert to entity process ? Or is it a bad idea ?

    Code (CSharp):
    1.     /// <summary>
    2.     /// This allow to automatically rename the entity wit the name of the game object on conversion.
    3.     /// </summary>
    4.     public class NameEntityOnConvertion : MonoBehaviour, IConvertGameObjectToEntity
    5.     {
    6.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    7.         {
    8.             #if UNITY_EDITOR
    9.             dstManager.SetName(entity, name);
    10.             #endif
    11.         }
    12.     }
     
  28. astrogee

    astrogee

    Joined:
    Feb 4, 2018
    Posts:
    16
    @WAYN_Group @psuong Thank you for answering :) I'll dig in and use the converted flow then.
     
  29. rubenpvargas

    rubenpvargas

    Joined:
    Jul 18, 2012
    Posts:
    34
    So i have been trying to grasp how the megacity streaming work, I have been trying and testing the subscenes and it seems to work. But does the scenes load and unload automatically? Or do I have to handle that?
     
  30. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    By default all subscenes in a scene load and unload when the parent scene is unloaded.

    When making a streaming game you want to manually control when what subsection is loaded. It's a very important decision to control, and is not a decision a game engine can make for you.
    Just sample code that you can customize it to the specific needs of your project.

    In the megacity there is a simple script that triggers scene loads of the high detail sections based on distance.

    Have a look at StreamingLogicSystem.cs in megacity. But again, this is highly game dependent.
     
    hippocoder likes this.
  31. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    In the ConvertGameObjectToEntitySystem.cs there's a comment mentioning "@todo: Support prefab to prefab referencing recursion..." - I'm glad this is on the list, is there an eta on it? It would really open up a lot of doors for flexibility.
     
  32. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Will be in the next release.
     
    temps12, Orimay, FROS7 and 2 others like this.
  33. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    Using the subscene system, is it allowed to create new entities during convert? I tried this, with and without 'DeclareReferencedPrefabs' (which i don't think is relevant here, but it's worth a try)
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3.  
    4. using Unity.Entities;
    5. using Unity.Collections;
    6. using Unity.Mathematics;
    7. using UnityEngine;
    8. using Samples.HelloCube_02;
    9.  
    10. namespace ProxyTest
    11. {
    12.         [RequiresEntityConversion]
    13.     public class TestProxy : MonoBehaviour, IConvertGameObjectToEntity
    14.     {
    15.         public GameObject rotationSpeedProxyPrefab;
    16.         // Start is called before the first frame update
    17.        public void DeclareReferencedPrefabs(List<GameObject> gameObjects)
    18.         {
    19.             gameObjects.Add(rotationSpeedProxyPrefab);
    20.         }
    21.         public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    22.         {
    23.             dstManager.AddComponentData(entity, new TestComponentData { });
    24.  
    25.             Entity prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(rotationSpeedProxyPrefab,
    26. dstManager.World);
    27.             int count = 4;
    28.  
    29.             NativeArray<Entity> kids = new NativeArray<Entity>(count, Allocator.Temp);
    30.             for (int i = 0; i < count; i++)
    31.             {
    32.                 dstManager.SetComponentData(kids[i], new RotationSpeed { RadiansPerSecond  = 20*i });
    33.             }
    34.             kids.Dispose();
    35.         }
    36.     }
    37.  
    38.     [Serializable] public struct TestComponentData : IComponentData {}
    39.  
    40. }
    41.  
    and get an error:
    ArgumentException: The entity does not exist
    Unity.Entities.EntityDataManager.AssertEntityHasComponent (Unity.Entities.Entity entity, Unity.Entities.ComponentType componentType) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/EntityDataManager.cs:266)

    ArgumentException: The entity does not exist
    Unity.Entities.EntityDataManager.AssertEntityHasComponent (Unity.Entities.Entity entity, Unity.Entities.ComponentType componentType) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/EntityDataManager.cs:266)
    Unity.Entities.EntityDataManager.AssertEntityHasComponent (Unity.Entities.Entity entity, System.Int32 componentType) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/EntityDataManager.cs:274)
    Unity.Entities.EntityManager.SetComponentData[T] (Unity.Entities.Entity entity, T componentData) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/EntityManager.cs:1391)
    ProxyTest.TestProxy.Convert (Unity.Entities.Entity entity, Unity.Entities.EntityManager dstManager, GameObjectConversionSystem conversionSystem) (at Assets/ProxyTest/TestProxy.cs:26)
    Unity.Entities.ConvertGameObjectToEntitySystem+<>c__DisplayClass0_0.<OnUpdate>b__0 (UnityEngine.Transform transform) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities.Hybrid/GameObjectConversion/ConvertGameObjectToEntitySystem.cs:35)
    Unity.Entities.EntityQueryBuilder.ForEach[T0] (Unity.Entities.EntityQueryBuilder+F_C`1[T0] action) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/EntityQueryBuilder_ForEach.gen.cs:192)
    Unity.Entities.ConvertGameObjectToEntitySystem.OnUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities.Hybrid/GameObjectConversion/ConvertGameObjectToEntitySystem.cs:28)
    Unity.Entities.ComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/ComponentSystem.cs:570)
    Unity.Entities.ComponentSystemBase.Update () (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/ComponentSystem.cs:169)
    Unity.Entities.ComponentSystemGroup.OnUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/ComponentSystemGroup.cs:369)
    UnityEngine.Debug:LogException(Exception)
    Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/Stubs/Unity/Debug.cs:25)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/ComponentSystemGroup.cs:373)
    Unity.Entities.ComponentSystem:InternalUpdate() (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/ComponentSystem.cs:570)
    Unity.Entities.ComponentSystemBase:Update() (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities/ComponentSystem.cs:169)
    Unity.Entities.GameObjectConversionUtility:Convert(World, World) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities.Hybrid/GameObjectConversion/GameObjectConversionUtility.cs:77)
    Unity.Entities.GameObjectConversionUtility:ConvertScene(Scene, Hash128, World, ConversionFlags) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Entities.Hybrid/GameObjectConversion/GameObjectConversionUtility.cs:131)
    Unity.Scenes.Editor.EditorEntityScenes:WriteEntityScene(Scene, Hash128, ConversionFlags) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Scenes.Editor/EditorEntityScenes.cs:54)
    Unity.Scenes.Editor.EditorEntityScenes:WriteEntityScene(SubScene) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Scenes.Editor/EditorEntityScenes.cs:37)
    Unity.Scenes.Editor.SubSceneInspectorUtility:RebuildEntityCache(SubScene[]) (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Scenes.Editor/SubSceneInspectorUtility.cs:242)
    Unity.Scenes.Editor.SubSceneInspector:OnInspectorGUI() (at Library/PackageCache/com.unity.entities@0.0.12-preview.31/Unity.Scenes.Editor/SubSceneInspector.cs:85)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr) [/spolier]
     
    Last edited: May 13, 2019
  34. joseph-t83

    joseph-t83

    Joined:
    Mar 28, 2014
    Posts:
    22
    You need to CreateEntity(EntityArchetype, NativeArray<Entity>)
     
  35. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    We are introducing a dedicated DeclareReferencedPrefabsSystemGroup that is made for exactly this and API's to do it. The whole system now also gives error messages when using at the wrong time.

    This will be in p32 later this week.
     
    FROS7 and thelebaron like this.
  36. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    In your specific example. You should remove
    1. Entity prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(rotationSpeedProxyPrefab,
    2. dstManager.World);
    And you can use conversionSystem.CreateAdditionalEntity to create an entity representing the source game object being converted.
    You can call conversionSystem.GetPrimaryEntity on the referenced prefabs child objects etc.
     
    Sarkahn, pakfront and eizenhorn like this.
  37. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Hello,

    I'm making a shooting game. I declared the bullets as prefabs with the conversion workflow on the gun (converted game object to entity). I instantiate the bullets in a system when the player shoots and then another system cast a ray from the bullet local to world position backward of the bullet by speed over time to detect if it hit something.

    All work well except that when instantiating the bullet it is first instantiated at (0,0,0) world position and the ray cast occurs before it position is set to the muzzle of the gun. this result in the bullet hitting whatever is at (0,0,0) world space in my case the floor of my level. As I have a systems that destroy both the bullet and the entity that it hit, both the floor and the bullet desperate...

    I tried changing the order of the command buffer instruction, adding run after on mys systems but nothing works. The only work around I found was either to set the floor on a different layer (but then the bullet will never hit the floor..) or setting the prefab transform in the inspector to something outside of my floor...

    Is there a way around that ?
    I did not see any solution to give an entity a translation directly on instantiation.

    Here is the code I use to instantiate my prefab :
    Code (CSharp):
    1.                 // Create the bullet
    2.                 var instance = CommandBuffer.Instantiate(index, Prefab);
    3.  
    4.                 // Place it at the end of the gun
    5.                 CommandBuffer.SetComponent(index, instance, new Translation { Value = location.Position });
    6.                 // Make it move forward
    7.                 CommandBuffer.SetComponent(index, instance, new PhysicsVelocity { Linear = location.Forward * Speed });
    8.  
    9.                 CommandBuffer.AddComponent(index, instance, new DestroyOnColision());
     
  38. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    Ok I literally was having this exact same problem and it was perplexing my for more than I'd care to admit(in a very similar setup, moving projectile that raycasts for collision). I believe its due to setting the Translation value instead of the LocalToWorld, so my assumption is that the Transform system registers the Translation's value in the next frame, so thats why it has that 1 frame of lag. My own fix was to just use the translation as the raycast origin(as in my case the entity is not part of a hierarchy), but I assume setting the position via LocalToWorld would also solve it.
     
  39. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    505
    Hi guys I had the same issue and mentioned it in a thread somewhere around here. A staff member responded. It is indeed due to the fact that the render transform system hasn't run yet to convert translation rotation and scale into the local2world component.

    I recommend having spawning occur just before the transorm system runs before you own systems do.
     
  40. joseph-t83

    joseph-t83

    Joined:
    Mar 28, 2014
    Posts:
    22
    Hi, I'm trying to implement some kind of subscene versioning. Basically, I want to be able to save some subscene version information in game when the game state is saved. Then when that game is loaded, it can check the saved versions against the current versions to do some logic, if necessary.

    I was thinking of trying to add a version component to the Entity with the SceneData component during conversion, but I don't know if it's possible to access that entity, or if it exists, during conversion. Anybody have any idea how to do this, or a good way they're doing it?
     
  41. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    How does one convert and inject a child gameobject in a hierarchy now? I have a hierarchy where the root is has a ConvertToEntity script, it gets converted and injected, and in p31 I had an additional ConvertToEntity(with Convert&Inject) on the child gameobject. Using
    Code (CSharp):
    1. childEntity = conversionSystem.GetPrimaryEntity(childGameObject)
    everything was converted for my needs.

    Now in p32, I now get a "GetPrimaryEntity(GameObject go) is a game object that was not included in the conversion. It will be ignored." warning, with or without a ConvertToEntity script attached to the child.
    I've also tried
    Code (CSharp):
    1. childEntity = conversionSystem.CreateAdditionalEntity(childGameObject);
    but it gives an error "CreateAdditionalEntity can't be called before GetPrimaryEntity is called for that game object" however using GetPrimaryEntity on a child gameobject results in the first warning of the game object not being included in the conversion.
     
    addent likes this.
  42. cordlc

    cordlc

    Joined:
    Jul 27, 2018
    Posts:
    11
    Will there be a way to more easily customize how the Conversion System processes the GameObjects? Besides my own proxy stuff, I pretty much only need one position component of some sort, so I have to delete the unwanted components in Convert() after they're already created, otherwise things look really messy. It'd be nice if there were a better way of doing it.

    It also gave me problems with the conversion automatically tagging things with Prefab, which I don't ever need. Otherwise it's a very nice solution, much cleaner than my old custom Proxy method.
     
  43. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    Ok I'm back with my spawning issue.

    I tried setting the LocalToWorld instead of the translation, here is the full system I use :

    Code (CSharp):
    1.  [UpdateInGroup(typeof(SimulationSystemGroup))]
    2.     public class ShootingSystem : JobComponentSystem
    3.     {
    4.      
    5.         BeginInitializationEntityCommandBufferSystem m_EntityCommandBufferSystem;
    6.  
    7.         protected override void OnCreate()
    8.         {
    9.                 m_EntityCommandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    10.         }
    11.  
    12.         struct SpawnJob : IJobForEachWithEntity<ShootingCapabilityParameters, LocalToWorld>
    13.         {
    14.             public EntityCommandBuffer.Concurrent CommandBuffer;
    15.             [ReadOnly] public float DeltaTime;
    16.  
    17.             public void Execute(Entity entity, int index, [ReadOnly] ref ShootingCapabilityParameters shotParam,
    18.                 [ReadOnly] ref LocalToWorld location)
    19.             {
    20.                 // Increase the cool down count
    21.                 shotParam.spawnCapabilityParameters.TimeSinceLastTrigger += DeltaTime;
    22.              
    23.                 // Spawn object only when requested
    24.                 if (!shotParam.spawnCapabilityParameters.SpawnTrigerred) return;
    25.  
    26.                 // Reset the input trigger
    27.                 shotParam.spawnCapabilityParameters.SpawnTrigerred = false;
    28.  
    29.                 // Shoot only if cooled down
    30.                 if (shotParam.spawnCapabilityParameters.TimeSinceLastTrigger < shotParam.spawnCapabilityParameters.CoolDown) return;
    31.  
    32.                 // Create teh bullet
    33.                 var instance = CommandBuffer.Instantiate(index, shotParam.spawnCapabilityParameters.Spawnable);
    34.  
    35.                 // Place it at the end of the gun
    36.                 CommandBuffer.SetComponent(index, instance, new LocalToWorld { Value = float4x4.zero});
    37.  
    38.                 // Place it at the end of the gun
    39.             //    CommandBuffer.SetComponent(index, instance, new Rotation { Value = quaternion.LookRotationSafe(location.Forward,location.Up) });
    40.                 // Make it move forward
    41.                 CommandBuffer.SetComponent(index, instance, new PhysicsVelocity { Linear = location.Forward * shotParam.Speed });
    42.  
    43.              //   CommandBuffer.AddComponent(index, instance, new DestroyOnColision());
    44.            
    45.                 // Reset the cool down count
    46.                 shotParam.spawnCapabilityParameters.TimeSinceLastTrigger = 0;
    47.             }
    48.  
    49.          
    50.         }
    51.  
    52.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    53.         {
    54.             var job = new SpawnJob
    55.             {
    56.                 CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(), // Pass in the command buffer allowing the creation of new entitites
    57.                 DeltaTime = UnityEngine.Time.deltaTime
    58.             }.Schedule(this, inputDeps);
    59.  
    60.  
    61.             m_EntityCommandBufferSystem.AddJobHandleForProducer(job);
    62.  
    63.             return job;
    64.         }
    65.     }

    Note that I set the LocalToWorld to float4x4.zero. I also tried with Identity, or simply with the location of the spawnerEntity (wich is actually what I want).

    And when I spawn my bullet here is where it end up :

    upload_2019-5-18_18-28-59.png

    And that is what I put in the transform of my prefab...
    upload_2019-5-18_18-30-4.png

    So it's like the command buffer does not execute the LocalToWorld set. but it does set the other components data...

    What am I missing ??

    Code available on my github.
     
    Last edited: May 18, 2019
  44. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    If you also set the translation to 0,0,0 what happens?
    Also I guess I solved my own problem I posted by using dstManager.CreateEntity and adding the components, but I'm unsure how conversionSystem.CreateAdditionalEntity() is supposed to be used and if what I'm doing is the correct way.
     
  45. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    If I set the translation in my job It works but it first spawn at the prefab transform so in the case of my bullet inside the ground. And as I have a logic to check collision, the bullet is destroyed because it hit the floor.
     
  46. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    I don't know how to read a localtoworld matrix visually but when if you Debug.Log(LocalToWorld.Position), does it show 0,0,0? I think the numbers you see on the inspector are from the scale being taken into account by the ecs transform system, so for setting it to 4x4.zero, I think what you are seeing is correct given there is a Scale value coming from the prefab, though I dont have too much experience with matrices so maybe someone else with more knowledge can chime in. Anyway you could try setting the localtoworld via
    Code (CSharp):
    1.  
    2. var localToWorld = new LocalToWorld
    3.    {
    4.       Value = float4x4.TRS(yourPosition,
    5.       quaternion.LookRotationSafe(yourDirection, math.up()),
    6.       prefabScale)
    7.    };
    As a side note I dont know if setting the localtoworld to a specific positional value while leaving the translation to 0,0,0 will have a cancelling effect so not sure if you need to also set the translation in addition or if the translation will be automatically updated to take that into account.
     
    Last edited: May 18, 2019
  47. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    As I said I tried multiple values to set on the localtoworld. None of them have any effect on where the entity is spawned. I did try the way you suggest although being in a job I don't have access to the prefab scale... That's an issue for another day ;)
     
  48. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    I guess I don't understand why you are setting the LocalToWorld with float4x4.zero or identity if you do not want a world space position of float3.zero.
     
  49. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992

    I tried several values all with the same result (local to world is a copy of the prefab transform). The code I gave happened to be the one with the float4x4.zero. but it's the same with the float4x4.identity or even with the LocalToWolrd of the entity that spawn my bullet (the muzzle of my gun) which is far from being float4x4.zero.

    I edited my first post to had the repository of the code; You can also check the attached project (copy of the said repository) open the SurvivalShooter scene run it and click in the game view. It will spawn a bullet (using the localtoworld of the muzzle) and you'll see in the entity debugger that the bullet sits at 0.
     

    Attached Files:

    • MGM.zip
      File size:
      137 KB
      Views:
      493
  50. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    https://i.imgur.com/CR7evrw.gif



    I just tried doing this and I didn't personally experience the bullet temporarily instantiating at 0,0,0(the scene view shows underneath the world where the bullet did spawn with the commented out part).