Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by SirDodgy · May 14, 2020 at 09:40 AM · dotsstatic variable

Acessing static variable from Entities.ForEach

I'm trying to create a grid map of my entities' positions

I'm trying to use entities.foreach to grab position data from entities and update a multi hash map like this:

 public class QuadrantSystem : JobComponentSystem
 {
     private EntityQuery query;
 
     public static NativeMultiHashMap<int, SectorData> quadrantMultiHashMap;
 
     protected override JobHandle OnUpdate(JobHandle inputDeps)
     {    
         quadrantMultiHashMap.Clear();
         if (query.CalculateEntityCount() > quadrantMultiHashMap.Capacity)
         {
             quadrantMultiHashMap.Capacity = query.CalculateEntityCount();
         }
 
         var LquadrantMultiHashMap = quadrantMultiHashMap.AsParallelWriter();
         JobHandle hashJobHandle = Entities
             .ForEach((Entity entity, int entityInQueryIndex, in Translation translation) =>
             {
                 int hashMapKey = GetPositionHashMapKey(translation.Value);
                 LquadrantMultiHashMap.Add(hashMapKey, new SectorData
                 {
                     entity = entity,
                     position = translation.Value
                 });
             })
            .WithStoreEntityQueryInField(ref query)
            .Schedule(inputDeps);
 
         return hashJobHandle;
     }


And access it in another system like this:

 [UpdateAfter(typeof(QuadrantSystem))]
 public class WorldRenderer : ComponentSystem
 {
     protected override void OnUpdate()
     {
         Debug.Log(QuadrantSystem.quadrantMultiHashMap.Count());

However unity throws an invalid operation: alt text

Which says " You must call JobHandle.Complete() on the job QuadrantSystem, before you can read from the Unity.Collections.NativeMultiHashMap". However I would have thought [UpdateAfter(typeof(QuadrantSystem))] would solve this problem no?

What am I doing wrong?

invalid.png (50.7 kB)
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
1
Best Answer

Answer by SirDodgy · May 14, 2020 at 03:26 PM

Ok I figured it out. The error actually showed the answer. I needed to call JobHandle.Complete() on the job. Here my entire class, updated with using SystemBase instead of JobComponentSystem:

 public struct SectorData
 {
     public Entity entity;
     public float3 position;
 }
  
 //[DisableAutoCreation]
 public class QuadrantSystem : SystemBase
 {
     private EntityQuery query;
  
     public static NativeMultiHashMap<int, SectorData> quadrantMultiHashMap;
  
     public const int quadrantYMultiplier = LevelGeneration.GridLength;
     private const int quadrantCellSize = LevelGeneration.GridWorldSize;
  
     public static int GetPositionHashMapKey(float3 position)
     {
         return (int)(math.floor(position.x / quadrantCellSize) + (quadrantYMultiplier * math.floor(position.y / quadrantCellSize)));
     }
  
     protected override void OnCreate()
     {
         quadrantMultiHashMap = new NativeMultiHashMap<int, SectorData>(0, Allocator.Persistent);
         base.OnCreate();
     }
  
     protected override void OnDestroy()
     {
         quadrantMultiHashMap.Dispose();
         base.OnDestroy();
     }
  
     protected override void OnUpdate()
     {
         quadrantMultiHashMap.Clear();
         if (query.CalculateEntityCount() > quadrantMultiHashMap.Capacity)
         {
             quadrantMultiHashMap.Capacity = query.CalculateEntityCount();
         }
  
         var LquadrantMultiHashMap = quadrantMultiHashMap.AsParallelWriter();
  
         Entities.ForEach((Entity entity, int entityInQueryIndex, in Translation translation) =>
         {
             int hashMapKey = GetPositionHashMapKey(translation.Value);
             LquadrantMultiHashMap.Add(hashMapKey, new SectorData
             {
                 entity = entity,
                 position = translation.Value
             });
         })
         .WithStoreEntityQueryInField(ref query)
         .ScheduleParallel();
  
         this.CompleteDependency();
     }
 }


With basesystem you call this.CompleteDependency(); instead of calling .Complete() on the job.

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

127 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

Related Questions

How to use SharedStatic? ECS Burst SystemBase 2 Answers

Get GameObject dependencies only once for JobComponentSystem 1 Answer

Can the burst compiler be used for any code? 0 Answers

ECS - entity connection 1 Answer

Unsafe.dll from Unity version conflicts with Unsafe.dll from standard collections package. 1 Answer


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