• 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
2
Question by komodor · Nov 26, 2020 at 01:53 PM · loopdots

DOTS ForEach in ForEach

I am looking for something like this

 Entities.ForEach((Component1 c1, Translation t1) =>
 {
     Entities.ForEach((Component2 c2, Translation t2) =>
     {
         // create job to calculate c1/c2 stuff and apply result to translations
     }).Schedule();    
 }).Schedule();


of course, i want it with burst but right now i am interested just in having this working with jobs to start somewhere if anybody can help with some example or tutorial I'd be glad

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 andrew-lukasik · Nov 26, 2020 at 06:43 PM

Make sure it won't approach O(n²) or will scale badly² otherwise.


 public struct Component1 : IComponentData{ public int Value; }
 public struct Component2 : IComponentData{ public int Value; }
 public class MySystem : SystemBase
 {
     EndSimulationEntityCommandBufferSystem _endSimulationEcbSystem;
     EntityQuery _query2;
     protected override void OnCreate ()
     {
         _endSimulationEcbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
         _query2 = EntityManager.CreateEntityQuery(
                 ComponentType.ReadWrite<Translation>()
             ,    ComponentType.ReadOnly<Component2>()
         );
     }
     protected override void OnUpdate ()
     {
         var command = _endSimulationEcbSystem.CreateCommandBuffer().AsParallelWriter();
         var translationData = GetComponentDataFromEntity<Translation>( isReadOnly:true );
         var component2Data = GetComponentDataFromEntity<Component2>( isReadOnly:true );
         NativeArray<Entity> entities2 = _query2.ToEntityArray( Allocator.TempJob );
 
         Entities
             .WithName("my_job")
             .WithReadOnly(translationData)
             .WithReadOnly(component2Data)
             .WithReadOnly(entities2).WithDisposeOnCompletion(entities2)
             .ForEach( ( in int entityInQueryIndex , in Translation translation , in Component1 component1 , in Entity entity )=>
             {
                 float3 newPos = translation.Value;
                 for( int i=0 ; i<entities2.Length ; i++ )
                 {
                     Entity otherEntity = entities2[i];
                     float3 otherPos = translationData[otherEntity].Value;
                     Component2 component2 = component2Data[otherEntity];
                     /* calculations */
                 }
                 command.SetComponent<Translation>( entityInQueryIndex , entity , new Translation{
                     Value = newPos
                 } );
             } )
             .WithBurst().ScheduleParallel();
         
         _endSimulationEcbSystem.AddJobHandleForProducer( Dependency );
     }
 }
Comment
Add comment · Show 1 · 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
avatar image komodor · Nov 26, 2020 at 09:27 PM 0
Share

so it is possible, GREAT

thank you very much

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

139 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 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

Making an animation stop once it finishes (trouble with booleans) 1 Answer

Problem with loops 1 Answer

OverlapSphere for parallel arrays 1 Answer

Infinite Length of Terrain/Loop Terrain 1 Answer

2D Dog-fighting Difficulties 0 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