Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by 4myGod · Sep 08, 2021 at 01:52 AM · arrayscomponentsdots

How to have an array property in a component

This is the code that I want:

 public struct WorldChunk : IComponentData
 {
     public float x;
     public float y;
     public WorldCell[] worldCells;
 }


When I try to use entities with this component though in my System it gives the following error:

ArgumentException: WorldChunk contains a field of WorldCell[], which is neither primitive nor blittable.

I believe the problem is that regular arrays won't work. I found many people suggesting to use DynamicBuffers but the only tutorials I found for them were adding an array of components to an entity, not adding an array property to a component.

Any help would be appreciated, thanks.

Comment
Add comment · Show 1
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 ataboo · Sep 08, 2021 at 02:31 AM -1
Share

Is WorldCell annotated with [System.Serializable]?

https://answers.unity.com/questions/1376925/how-to-show-an-array-of-custom-objects-in-inspecto.html

2 Replies

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

Answer by andrew-lukasik · Sep 08, 2021 at 09:53 AM

How to have an array property in a (IComponentData) component?

  • Short answer: You can't

You can't. Because if possible that would break some of the technical requirements for const-size & blittable IComponentData that makes all this fast to iterate.

I know everyone's first instinct is to expect arrays nested directly in IComponentData, but this is a dead end my friend.

  • Longer answer: You can

You can convert this WorldCell[] into read-only data structure known as blob asset

 public struct WorldChunk : IComponentData
 {
     public float x, y;
     public BlobAssetReference<WorldCellsData> worldCells;
 }

Which is not a bad idea given that your data is read only & you know how to create and manage this allocation type.

Alternatively: You are free to host an unsafe pointer to an array wherever you want, but that will bring you a whole new spectrum of pain you don't even want to know.

  • Better answer: DynamicBuffer IS your array (list) property already (!)

Yup. It was designed to solve this exact problem.

 public struct WorldChunk : IComponentData { public float x, y; }
 [InternalBufferCapacity(16)]public struct WorldCellElement : IBufferElementData{ public int Value; }
 
 // OnUpdate ()
 Entities
     .WithName("iterate_WorldCells_by_WorldChunk_job")
     .ForEach( (
         ref WorldChunk worldChunk ,// chunk component data
         ref DynamicBuffer<WorldCellElement> worldCells// this is your array property
     ) =>
     {
             float x = worldChunk.x;
             float y = worldChunk.y;
         for( int i=worldCells.Length-1 ; i!=-1 ; i-- )
         {
             var cell = worldCells[i];
 
         }
     } )
     .WithBurst()
     .ScheduleParallel();
  • Chad answer (more advanced)

You're still trying to solve this problem in a somewhat OOP mindset. DOTS means that most of your problems and solutions comes down to shape of the data itself. So, in you think about it, you can reorganize this data to make the array just disappear (hide :T).

If you make all WorldCell into entities you can manually (hard part) segregate them into neat Chunks (allocation block that holds IComponentData) + refactor your WorldChunk into struct WorldOffset : IComponentData { public float x, y; } and make it a chunk component (very specific thing, single component per chunk) - then there is no managed array anymore, while making data iteration more simple than ever.

 Entities.ForEach( ( in WorldCell cell , in WorldOffset offset ) =>
     {
         
     } )
     .WithBurst()
     .ScheduleParallel();
Comment
Add comment · Show 2 · 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 4myGod · Sep 09, 2021 at 01:03 AM 0
Share

I appreciate the well thought out answer, that helps a ton. So I had initially thought to make all the cells entities and simply add chunkX and chunkY. The problem is that when looping them for rendering onto a plane it would be tough as one cell needs to know what the cell next to it looks is to figure out how to render. With an array that's simple but with entities I don't know how it would reference each other.

avatar image 4myGod · Sep 09, 2021 at 02:46 AM 1
Share

Never$$anonymous$$d, I got DynamicBuffers working, and yup they work great, I can use them just like an array for better or for worse.

avatar image
0

Answer by Llama_w_2Ls · Sep 08, 2021 at 07:33 AM

I'm not really an expert at DOTS, but to solve this issue, I had to create chunks as entities. Then use the entity manager to create a dynamic buffer of World Cells, and set the public DynamicBuffer<WorldCell> worldCells; component to that buffer.

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

128 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

Related Questions

Entities are not rendering until the bottom left point is in camera 1 Answer

Instantiated prefab missing ECS components 1 Answer

Get a GameObject from a component 1 Answer

ECS - entity connection 1 Answer

Class as a member of IComponentData struct? 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