I have a collection of entities, within that group I will have a variable range of them that match a criteria. I then need to choose a random one from the resulting subset.
eg:
A, 1
A, 2
B, 1
B, 2
B, 3
So I loop through looking for all instances where the first column equals “A”. Then I need to choose one of those.
Thing is, I don’t know how many matches I have until I loop through the entire parent NativeArray. So I would have to loop through multple times, the first time to get a count, and then a second time to place the entities within the new subset NativeArray.
Any thoughts?
Rant: Can we PLEASE get more precise query abilities like SQL, as this could solve SO many issues… anyway, I know, this is early… I am only asking to let staff know someone thinks this is important)
I don’t think that doing the filtering with an SQL-style or Linq-style query in an EntityQuery would be efficient, though. Unless maybe codegen is on the table, which it already is for Entities.ForEach. So you never know!
But one solution to your problem would be using NativeQueue, and 2 jobs:
Job 1 iterates through all entities and stores the valid ones in a NativeQueue (supports parallel writing)
Job 2 knows the total count, gets a random nb from that count, and dequeues until that random one is reached (this one can’t be parallelized though)
Thanks, but that isn’t really a good answer. I will give it some real thought on how I would handle it, but it’s not really gonna be a good way to get there.
The Reader/Writer might, the problem there is each entity archetype has two int values, and then an array (IBufferElementData) of float3, so I don’t know if the Reader/Writer will resolve this requirement.
Detail:
I have a traffic system, using Easy Roads 3D (nice asset, great support) Each Road has Lanes, each Lane has a series of points. Intersections can lead from one lane to another, so when the Auto gets to the end of a road’s points I need to get a list of Intersections that have the same starting Lane and Road as the Auto’s current Road/Lane. Because there can be multiple Exit Roads, and multiple Exit Lane on each road, I need the ability to get a subset of all connections.
Each Auto has it’s own set of current points (this avoids having to go to the Road/Connection points for each frame. (a post I recently made asking about duplicate data vs speed and a rep from Unity mentioned that duplicate data would be more performant) In either case, it’s not each frame that I need to get the next collection, so the multiple jobs FEELS like a nightmare of spaghetti code. I will think about it, see if I can find a way to get it working
Here’s the thing:
Multithreading comes with an unavoidable set of challenges that you’ll have to face regardless of if you’re using DOTS or any other language/engine
There’s nothing stopping you from doing non-jobified stuff in DOTS if you don’t want to deal with the challenges/limitations of multithreading. Non-jobified logic will just have the same performance as something that you’d implement using monobehaviour (maybe even a bit better)
In DOTS, the worst-case scenario is the standard every-day scenario that we had in monobehaviour
You can create any array or NativeContainer within job actually. What they said is you can pass in NativeContainer & cannot pass in array, not you can’t create them in job.
Ok, the problem I have is that I don’t know how many I have until I traverse the entire set of entities. Running the loop the first time to get a count, and then again to fill the array SEEMS expensive. Is it?
You have to manage it manually. Thereby each car is a key and has multiple Linepoints or each line end is a key and has multiple connected lines…
Something like that. This way you can access a subset really fast. You just have to care about keeping it up to date.
I’m facing the same problem here.
I’m looking for a way to generate multiple data vectors within parallel jobs and return them to the main thread which is quite tricky.
I tried the NativeMultiHashMap but it doesn’t offer any easy way to get some kind of (key: values) and using the
GetKeyValueArrays is very inefficient it eats ~0.3ms for 400 Elements.
So I’m trying to give some tries with NativeStream container hoping I can found what I’m looking for.
Unity is saying that we can store multiple arrays inside a single stream.( I hope I’m not wrongly interpreting what thay are saying)
A deterministic data streaming supporting parallel reading and parallel writing. Allows you to write different types or arrays into a single stream.
please correct me if I’m wrong.
Thanke you
I am not returning data from within the Job to the main thread. I am creating data/entities on the main thread and have used those entities within the job.
The data is created in ER3D_Traffic.cs and used inside ER3D_TrafficSystem.cs I understand this is the opposite of what you are looking for, but maybe seeing how it works in this manner could be a clue on how to get that data back.