Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Captain_Pineapple · Jul 25, 2019 at 08:01 PM · componentsystemdots

[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer

Hello there,

i'm currently trying to learn using the ECS and i currently struggel with the following problem: Given a job on an Archetype i want to create more of these entities under some conditions. So far i could not manage to get the entitycommandbuffer to work, which you need since one cannot create more entities from a job.


My Struct setup looks as follows:

 struct CollisionJob : IJobForEachWithEntity<NodeStruct, CubeCollisionData>
 {
     public EntityArchetype _NodeArchetype;
     public EntityCommandBuffer _entityCommandBuffer;
     //public EntityArray entityArray;
     public void Execute(Entity e, int i, [ReadOnly] ref NodeStruct Node, [ReadOnly] ref CubeCollisionData colObject)
     {
         ////Divide:
         if (!Node.split)
         {
             for (int j = 0; j < 8; j++)
             {
                 _entityCommandBuffer.CreateEntity(_NodeArchetype);
             }
         }
         Node.split = true;
     }
 }

  protected override JobHandle OnUpdate(JobHandle inputDeps)
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         return (new CollisionJob { _NodeArchetype = NodeArchetype, _entityCommandBuffer = entityCommandBuffer}).Schedule(this, inputDeps); 
     }
     return inputDeps;
 }

So far i have tried creating an entityCommandBuffer and a Concurrent EntityCommandBuffer in the main Thread using

 World.Active.EntityManager.World.GetOrCreateSystem<BeginSimulationEntityCommandBufferSystem>().CreateCommandBuffer().ToConcurrent()

which only results in an error telling me that the commandbuffer was not initialized when i try to use it...

Some insight on this is highly appreaceated since i cannot find any valid information on this...

Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Captain_Pineapple · Jul 26, 2019 at 12:28 PM

Well for those who are intrested:

solution was the following:

     protected override void OnCreate()
     {
         entityCommandBuffer = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
     }


Create a "Startfunction" for your System class. There have it instantiate a fitting 'CommandBufferSystem'. In my case an 'EndSImulationEntityCommandBufferSystem'. From what i understand they are only different in their time of execution. Then change the OnUpdate call as below:

 struct CollisionJob : IJobForEachWithEntity<NodeStruct, CubeCollisionData>
  {
      public EntityArchetype _NodeArchetype;
      public EntityCommandBuffer _entityCommandBuffer;
      
      public void Execute(Entity e, int i, [ReadOnly] ref NodeStruct Node, [ReadOnly] ref CubeCollisionData colObject)
      {
          ////Divide:
          if (!Node.split)
          {
              for (int j = 0; j < 8; j++)
              {
                  _entityCommandBuffer.CreateEntity(_NodeArchetype);
              }
          }
          Node.split = true;
      }
  }
   protected override JobHandle OnUpdate(JobHandle inputDeps)
  {
      if (Input.GetKeyDown(KeyCode.Space))
      {
          return (new CollisionJob { _NodeArchetype = NodeArchetype, _entityCommandBuffer = entityCommandBuffer.CreateCommandBuffer().ToConcurrent()}).Schedule(this, inputDeps); 
      }
      entityCommandBuffer.AddJobHandleForProducer(colJob);
      return inputDeps;
  }


This worked for me. Let me know if i missed something in here.

Cheers.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get a component data from an entity inside a JobComponentSystem? 1 Answer

Does the Parent component get removed from Entity if its Value is not set? 1 Answer

Project Tiny no DisplayInfo Singleton 0 Answers

2D Animation does not start 1 Answer

Legacy particle system doesn't work on Unity 4.0 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges