#archived-dots
1 messages Β· Page 72 of 1
to have an rng per system and just send it to the jobs
I would really post your problem to the forums, maybe there is an interesting answer π
ah wait I did not read to finish
ah ok well the stuff he proposes at the end with one rng per thread is pretty much perfect I'd say
the copy by value thing is interesting, I didn't think about that. You actually do have to initialize one Random per OnUpdate then.
But I think one rng per thread is probably best practice, that is also what I always read when doing multithreaded random things as I remember now that I read that thread π
but this all does not explain the bevahiour that you got, so if you are going to post, still notify me
Seems like the seeds should probably not come from time or whatever you use, but instead something like position. Depends on what you're trying to accomplish (I kinda skimmed the conversation, so might've missed something)
no you should not reseed every time you need a random number
Ah, you're doing parallelization where you want a (potentially) massive number of jobs to each get a random value?
yes exactly Hodhandr
Best random I've got so far
but I'm seeding inside the job:
Random rand = new Random((uint)(index + DateTime.Now.Ticks % (uint.MaxValue - 1)) + 1);
IJobForEachWithEntity
Ok so you have an index. Good
yeah but he already tried sending an pre-seeded rng to the job and that got weird results somehow
So what you can do:
upon making the job pass a seed that you gen random each job creation. Inside the job, make a new random using seed and using the index
allthough that is what should get the actual random beheviour whereas reseeding often somehow works but does not have to
sending a pre-seeded random into the job got me the most unrandom result so far, I assume it's a thread safety issue
Did you use the index as wrll?
no that is also not the right practice to gerenating actual random numbers @solar ridge
Otherwise everey thread had the same random number genned each next
yeah, I used time + index
this is the basis for what I did: index + DateTime.Now.Ticks
it got me the most random results so far
still don't entirely like it cause DateTime is not a high resolution timer lol
Right. All that would change is when you do Time call. It would be on create instead of everey frame. Every frame would be using rand.next passed to the job
Have you posted your full code of the non random problem?
yes
Okey doke. Give me a moment to find it as I am on mobile
...
this is the way that I want to do it, but I think using the same instance is not thread safe, hence my very unrandom spawning.
The commented out way is what I tried next, it passes in a random element to add to the seed
yes, the commented way
I used the 'seedoffset' + index
it was better, but it was not completely uniform and I can't explain why
@solar ridge @minor sapphire @tawdry tree here check also the first answer who recommends to never seed every time you need some random numbers https://stackoverflow.com/questions/20636859/random-number-generator-why-seed-every-time
it really just does not give you the right distribution actually
It could work sometimes but the rng are not developed to be seeded repeatedly, it is just not how they work
what if the seed is random each time?
also not
in theory an rng could always start with the number 1, and only "on average" give you the right distribution
so then the only alternative is really to pre-generate a list of random numbers in a list and pass it into the jobs...
Wrong math library though. I have had no issue reseeding using the new systems
a computer cannot generate real random numbers, it can just generate a sequence of numbers that has the right statistical properties
Soaryn, using Mathematics.Random?
There might be some overhead with seeding, though if you just want the appearance of random (and/or the ability to control it) you might instead want to consider some pseudorandom noise (perlin noise or something) instead.
@solar ridge yeah I guess the use the mersenne twister algorithm that can cope with some stuff, but statistically you will probably still not actually sample the right thing
I what I mean is that stack overflow assumes Java and c++ normal libs
but nvm I mean just do what works guys, just wanted to tell you that this is not the right way to get the actual random numbers you want
Noise could benefit you yes
justsomequestions, I think passing in the random instance to a bunch of threads is worse
If you're generating a giant thingy and don't actually need it to be random (protip: you don't. If you do you know why I'm wrong), noise tends to be better
most of them use mersenne twister today I think anyways
noise is more expensive though
Youd gen noise noise once really and just keep pulling the next point
But keeping track of that next point in parallel at times could be painful
But you could skip rows (assuming a 2d texture like noise for instance) for each thread
Even in your working system Id use ticjs as seed in onCreate
Rather than 1
in my working system I using ticks + index in the job itself
correct
Fair
I don't like this either though lol
but it works for this case
according to a thread I read, Mathematics.Random returns similar values for seeds close to each other, so this is not a great method heh
I could like hash the index and use that though
You could also see how true that is...
They shouldnt be .. but if they are do a different operation for index
Ie bit operations 
ya
The one way I've actually used random* (noise, actually) is in terrain gen, where the inputs would be some manner of position, which parallelizes really easily (and deterministically).
If you need to make them distinct, I've read the suggestion to multiply with some semi-high prime number (as long as you know you don't overflow, or can deal with that)
The forum is a dangerous place. There are a lot of intelligent and clever people... then there are those ... who think they are 
yes true I'm writing a test now haha
Care to test new Random(0) to confirm if it crashes?
I know new Random had issues in the past
Cant remember the exact reason
Likely state related
Yeah. So carwful witj modulos then :-)
.... my phone typing is getting worse
Guess it is time to get out of bed and do some dev then 
exactly hehe
Flawless logic
here is a very good example of why one does not seed every time if one wants random numbers https://stackoverflow.com/questions/976993/issues-with-seeding-a-pseudo-random-number-generator-more-than-once
actually harder to find than I thought, many stackoverflow threads actually did recommend seeding often which is kind of wearing
Those results don't look very random to me...?
exactly
π π
I'm modifying test
are you guys even listening to me
thats prety much what I am telling you and what the thread said
@junior fjord yes but I'm just looking for the best way for me to get a random distribution
Oh, I thought he was doing next/next/next, not new/new/new
Let him do science here
var r = Random(1)
for i=1,100
r.nextFloat()
is your best bet
Guys I fully expect that doing next next next would produce a nice set
^^
they will behave random
He is wanting to know first index of each random seed
I know
how different that first entry is
I cannot easily do next next next using unity ecs, this is the issue
but the thread you send had a solution
I mean the forum thread
one rng per computer thread
We have looped in conversation...
or post the thing you had to the forums, because it actually should work. Maybe there is some very subtle mistake or it even is a bug
He was not using ECS, he was using jobs though
Which in all intents and purposes of the use case is the same
can't you access thread id in the ecs jobs too? I mean they are the same jobs
ECS Foreach Entity is a forloop with magic on it
right lemme recheck that thread
it is the last entry
maybe a bit overkill but I think that is the best practice forever solution
[NativeSetThreadIndex]
private int threadIndex;
does this get set by unity or something?
Should
yes in that version it did, I am not sure how it works nowadays (if it still is the same)
The index is the same thing
as I believe it is chunk index
Err
scratch that
Right... ForEach ... single burst not a loop 
One moment let me see if I have my random for that one
That thread ID should at least still work. My concern is that the same thread would process both
On the slim chance that it does
what do you mean? Each thread will only access its own rng
this is really weird cause I am using .ScheduleSingle, I thought this one thing is running on one thread WTFFFFF
my mind is destroyed right now lol
Ah
still I'm gonna try this just to see what happens
Wait.. if you are doing single you should only need one seed...?
Unless that doesnt get written back to whilst the job is running π€
yeah exactly, it implies that the original "pass in the system's intance of Random" SHOULD have worked
Well...
actually... you wouldnt make the new Random in the job then
Is the thing
You'd pass the fully setup Random if running single thread
yes, the original code had the system instantiate random OnCreate
yes I also think the original should have worked
I really have no clue why it doesn't
for me a pretty similar system works
it seemed the most intuitive, and I still have no idea why that didn't work given it 'should' be running on one thread with ScheduleSingle()
allthough I did not have such a nice way as you to see if they are actually very random
you seem to be very opposed to just posting on the forums, right? π
I'm gonna follow through with this test then try original again lol
yeah man, I'm on a role of tests right now haha
π do what you have to do
... uh
Hmm Im trying to think in the pastebin. Did you try changing the state in main thread at all?
IE in the update rand.Next
ah @minor sapphire I maybe know why the original did not work
did you runt he system multiple times to get that output or all positions in one run?
As I know you;d have issues if you have an int for example in a job and wanted that result
So for each pass over rand.Next or what ever just to make the state change
Basically what you have but at line ~61 where Rand= rand, that is always the same state
so the first 100 ships would be the same as the next 100 ships
@junior fjord the system does run multiple times. The OnUpdate is called for 20 seconds
OnCreate() is called every frame??
maybe try seeding the Random once in OnUpdate
since the rand is passed by value and therefore copied
Dont necessarily need to reseed (though that should work) you just need to increment the state
so each time you start the job it will start with the same rand
so maybe once per OnUpdate could change things, and then pass that in to the job
but I am on team one rng per thread overall, I think that is a pretty neat solution
LOL
It's a struct, it's passed by value
the state is the same every time!
I need to pass it by ref yeah?
Yea which is what @junior fjord may have just stated π
LOL i missed justsomequestions saying the same thing haha
ah yes, sorry did not read what you wrote @solar ridge
Mmmm not sure you can do that with job... 'twould be better to merely take what you have and just call rand.NextWhatever
Befor making the job
so that the state is dif each job schedule
otherwise, while you have a rand, it is the same rand state each run
Er
No reseed
Just to be safe
You could even have a randSeedMaker 
if you only call rand.NextWhatever make sure however to not make it rand.NextFloat, elseways you always get the same random sequence just offeset by 1
and just do Rand= new Random(rand.nextUint)
it is better maybe to make
rand = new Random(rand.NextInt) or something like that
yeah π
Yeah that was why I back pedaled to reseeding π
Always good ideas in theory, mine are.. but not always implementation
yeah @solar ridge what you says should work
sorry if you stated that earlier and I missunderstood
This would explain why the index didnt really work
once per onupdate should generate enough random numbers per seed to be actually a bit random
Yep
IndexOutOfRangeException: Index 4 is out of range of '4' Length.
https://pastebin.com/SwrTeVi8
LOL
ok I'm gonna read what I missed from you guys lol
maybe threads are not stared to be indexed by 0 but by 1?
Ah you neednt do that now (Shouldnt need to)
Crashing I think was the answer 
yeah thread index started from 1
I am by no means an expert but I think I have 2 cores (~ processors) and 4 threads
ah ok than that was it
no, this is a good result
nice π
I see no non-uniformness
this method should work in all scenarios and be the lest amount of seeding as far I can see
but I'm gonna scroll up and read what I missed lol
I can try original version passed by ref too
yes I also think overall this is the best method. I would even maybe not create the array in the system but in a static class at the beginning of the game
Lol just don't forget to dispose that on app quit
Ohh... hmmm jobs that are using it would need to be completed before disposing on app quit 
yeah but I have a few of the static helper data classes, I just always put an Initialze and Destroy on them which I call from my Bootstrap monobehaviour
That would... be fun to handle
I'm 2 days into ECS so this is new to me haha
does the app quit without quitting its jobs first?
π
Found that out the hard way
lol
I think helpers right now are a ... slightly misguided solution for somethings that are memory stored. Not bad ideas mind you, but just early
well if it gets disposed before jobs complete... the jobs will just go and die in the background yeah? lol
Likely should wait to see what changes before making a full on rando Helper
Shiny not quite
but don't you have the same problem if you store stuff in systems?
Depends on the job. And then in some cases they wouldnt dispose things in the background correctly if crashes
or is ondestroy called later than their jobs for the systems?
@junior fjord I can complete the job before appquit
I manually call complete with the handle
then dispose
afaik random isnt thread safe
ah yeah ok, so one needs to keep track of all jobs that use some nativearray and complete them before you dispose
that is probably easier in systems that in static classes
@wintry birch the end solution now was to also have one random per thread
oh yeah sorry i didnt read everything, just saw something about a static random
yeah I'm not even sure how to pass rand by ref for the original method but because it's not thread safe I don't really wanna go that way anyway
so I think this one per thread is the best
yes, afaik
but wtf did I just put in my code lol using Unity.Collections.LowLevel.Unsafe;
... out of curiosity...
needed for [NativeSetThreadIndex]
Would you have been able to add a Random to your spawner and make it NOT readonly? 
my randoms are not readonly so yes it is possible
I'm not sure what you mean
Each spawner has their own random with random seed
so when you go to spawn you'd just do spawner.random.Next
but my system did not run in parallel actually I think since it only had one entity to process
it was my first system and very misguided for the jobsystem π
so dunno if that would have had runtime issues
That random would be passed by ref and would be updated each next
Something to profile in the future
How do I actually pass this rand by ref, it's not a method paramater
Well if you store the rand on the spawner it would already be done
That is why I thought of it
Only things passed by ref are the entity components you are filtering
the spawner has the main thread component OnUpdate and the job thread component
So if the spawner had a random that was genned when it was created
IJobForEachWithEntity<ShipSpawner, LocalToWorld, Rotation>
Then assume spawner was something like
ShipSpawner {
LiterallyAnyFields Blah;
Random Rand;
}
So when you'd access the spawner you get a rand you can use for it as well π
ohhhh in the component data
Potentially could be bad
no bad idea, how to seed lol
but that way you again have only very few random values per random seed
i did a bit of research and what people recommend is having a global locked thread to generate random seeds, and having 1 rand per thread, using those random seeds
Potentially could be good
i mean locked random
why
That was sarcasm π
I recommended that from the get go
Btw @junior fjord "Random Number Generator based on xorshift."
"Designed for minimal state (32bits) to be easily embeddable into components."
yes if you reuse it it is fine
just saying you should not only seed it, spawn your entity using 2-3 random numbers and then throw it away
Not really a problem in this case
you will be again seeding for only 2-3 random numbers
yes seeding is always just setting state
Regardless though, I'd be interested in seeing spawners have a built in rand field in their component data
As long as the spawner is not a shared component we should be fine
as the rand would be reused constantly, no reseeding needed, and it would be updated per use
yes, if you reuse the same rand in the same component you are completly fine
How many bytes is a uint 
most of the times I guess 4
Because that would be the cost of storing the rand
which would be cheaper than most NativeStrings
32 bits, yeah 4 bytes 
@wintry birch do you still know where you read that? I don't think I fully understand it
let me find it again
here it is
As noted by some commentators, there is another potential problem in using different instances of Random that are thread-exclusive, but are seeded identically, and therefore induce the identical sequences of pseudorandom numbers, because they may be created at the same time or within close temporal proximity of each other. One way to alleviate that issue is to use a master Random instance (which is locked by a single thread) to generate some random seeds and initialize new Random instances for every other thread to use.
@junior fjord
https://stackoverflow.com/a/3049478
yeah that's essentitally the technique that was used
the system had its own master rng, used to generate seeds for the thread rngs
@wintry birch ah ok thanks
I would have just seeded every one with Seed(time + threadindex) but yeah maybe even using another rand for seeding makes it even more unpredictable
and if similar seeds actually result in similar sequences as @minor sapphire suggested it is probably not even a good idea to do it how I suggested
what about ThreadStatic? its recommended in the docs
@wintry birch ah but btw they are talking about Random.Next from standard library, I was talking about random from Unity.Mathematics
that stuff does replace a lot of non-threadsafe stuff with threadsafe counterparts I think
so there is a chance that Random from there is threadsafe too but who knows π
Kind of. Most of it assumes you won't write at the exact same moment (in most cases the job scheduler should have that handled) however, I dont know exactly what would happen if 2 threads started competing for "next"
Likely nothign bad
BUT WHO KNOWS
Note: this example uses IJobForEach.ScheduleSingle(), which performs the Job on a single thread. If you used the Schedule() method instead, the system uses parallel jobs to process the entities.
So my spawner is theoretically running on a single thread. So I thought, I can just instantiate a RNG once per frame in OnUpdate (rather that one per thread in an array), using a master RNG to generate the seeds, and pass that in to my single running thread.
lol, it did not like that idea
Seems this one per thread thing is important EVEN THOUGH I'm using ScheduleSingle for this job, supposedly forcing it to run on a single thread
whatever, I'm gonna put back the one per thread array π
is the master rand locked?
no, I better go test that
but it's only accessed from main thread anyway
I'm only accessing it on OnUpdate
Meh I'll go lock it anway lol
oh no I think I understand why, it's cause my master rand is in in util and it's a bloody struct passed by value lol
silly mistake
.... you made a matrix scene... neat
haha yep pretty much
hey what would be the right way to prevent a system running for the first second or so
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
if (Time.realtimeSinceStartup < 1f)
{
//return JobHandle.;
}
can't return null, and can't see any obvious property to return in the JobHandle struct
or could create a JobHandle, mark it complete, then return that I suppose
that seemed to work
if (Time.realtimeSinceStartup < 3f)
{
var jh = new JobHandle();
jh.Complete();
return jh;
}
Reutn input deps
You COULD return a new job handle but lets assume the system had info it needed
I have a little trouble with SCD understanding and i need your advice, guys. I have units in my project and each unit refers to some fraction. I need only to store index of a fraction per unit. So with fact that usually unit doesn't change his fraction and i have not so much fractions i thought that use SCD for storing fraction index is a good idea. But i need to access unit's fraction to often and it can't be done in jobs directly. The only way is to prepare SCD values as array in main thread and transfer it to job.
The question is:
Is there enough benefit in this situation to use SCD?
@frosty siren I would first just go with a normal component and later decide if you want to have it as a shared one
the shared component also will change memory layout since the archetypes are divided by shared component data value
it could very well be that it is better to later have all archers in the same archetype, since the most heavyweight system will be on their behaviour
if you have the fraction as shared you have them sorted by fraction already
you could also maybe then have two shared components but maybe there are only 10-20 archers of a specific fraction and then your chunks would get rather small
but I am also more of an beginner
but for my project I for now make everything a normal component and will decide in the end what I make shared
and to effectively iterate on the scd values I guess you'd just use the chunk iteration, since then each job only has one chunk and therefore one scd value (but I've never done scd, so that is just a guess on how one circumvents your idea of preparing arrays)
@junior fjord thank you very much, your answer is very useful!
@frosty siren happy to hear that π
@frosty siren ah and I think it is faction not fraction on english btw π
I'd like my JobComponentSystem to interact with a single Entity/GameObject. I'm trying to pass the relevant values (just position right now) to each job, but how do I set that in the System in the first place?
I hope that made sense.
I saw somewhere that you can use a static variable for the System's class? Set that from a Monobehaviour?
hm, thereβs no trick to cloning a world or copying entities to a new world, is there? i know you can transfer entities between worlds, but id like the copy. found an old official post saying they intended to eventually support world cloning but π€·
@left spindle I don't fully understand your question, can explain a bit more?
Just stumbled across a post by 5argon in case anyone else finds it interesting - https://gametorrahod.com/tag-component/ - pretty much mirrors my own experience
@junior fjord yeah, it's faction but from my language to english fraction is also faction but with another context. Will rename my structs)
@amber flicker None of these occur if you are using EntityQuery overload of EntityManager where it operates on everything in a chunk equally.. Do you understand what that is about?
@junior fjord I'm not totally sure - perhaps worth asking him on the forum. I wondered if it was simply saying that using an EntityQuery would supersede any tagging? If somehow using e.g. RequireComponentTag was more efficient than constructing an EQ, I'd certainly like to know π
@amber flicker yeah it would interest me too.
Also this, If you could do per-chunk EntityManager action it is a no brainer to use tags instead of if the value. (So try to design the game this way, maybe cleverly using ISharedComponentData that things you want to tag are always grouped.) seems to be something I'd like to understand what he is trying to say
does he mean if I could just tag the whole chunk in one go and the entity manager would realize there is no reallocation necessary?
I think they're saying essentially if you can early out on a chunk, you should
From my understanding (which could be flawed), that would still basically be equivalent to chunk iteration and checking a bool for the chunk
If anyone else wants to chip in with different opinions/interpretations please do
hmm I still don't really understand it. I mean tagging basically means that the EM just gets you the chunks that have a specific boolean condition enabled
which other per-chunk EntityManager action is meant here?
I'm just reading up in the background here
super interesting info
take-out for me seems to be to tag if you'll do a lot of work afterwords, but don't tag if work is not so big, and by work I guess I mean amount of filtering of entities
what is the EM btw?
entity manager I guess, answered my own question there
if anyone here has thoughts on this thread it would be of great help to me: https://forum.unity.com/threads/shared-data-copy-it-store-it-in-the-ecs-or-in-nativearrays.667111/
I think about this kind of problem in many settings and normally go with option B but I am not sure if that will maybe end up in some kind of problems later
If there are many TaxPayments and few TaxGroups does it make sense to make TaxGroup a shared component data added to entities that have a TaxPayment?
yeah but that is just one example problem of a few. If I'd always go with shared components I'd have very small chunks at the end.
Overall my humans for example have the following attributes that already all show similar "problems" to taxes and could be shared:
- Their Profession
- Their TaxGroup
- Their Faction
if I would do a shared component for all of these I'd only have a chunk of for example "wealth (high tax-group) gold miners in village xyz" which are I guess like 2-3
I'd rather decide in the end which of these components should be shared
but if you think that is a bad approach please tell me
Just my opinion but... I think you're right to be mindful of breaking up chunks too small but it's a bit hard to answer these kind of questions without fully getting your head around the relationships between the data and what really needs to know about what. In this case, if Profession, TaxGroup & Faction rarely changed, I might wrap them all up as Enums in a single CitizenStatus IComponentData or smth
sorry, a shared component data
Actually I take that back...
I think your chunks wouldn't be that small because presumably your systems would also be logically separated - i.e. the TaxSystem shouldn't need to know about the Faction or Profession otherwise that data should be in TaxGroup right?
yeah most of the systems only need one of these
I'd say the most heaviweight systems are the ChooseProfessionSystem and ChooseProductionSystem
these need to know about taxes and Profession+Taxes respectively
because they basically calculate which profession is the most worthwile and which good is the most worthwhile (which is pretty much determined by taxes and nutrition)
but the chunks would still be small.
I want to support many small villages (100-200 humans) and a few larger cities (1000-2000 humans) and I guess overall there will be maybe 50-100 professions at most (but I can't put these in enums since I read them from textfiles)
and per city there could again be like 5 different taxgroups to tax the wealthy more than the poor (hopefully π )
so you'd be down to pretty few if you have everything as shared component
so at most 2000 per city, divided by 5 tax groups are 400 and then by professions are like 8
and these are the largest chunks, for the villages they are much smaller
Hmm I think I would have components on each human with e.g. their profession (which is just an int) that the ProfessionSystem would use to index into a static (well, shared amongst systems) NativeArray containing the data you need
yes that is what I am doing and what I tried to describe as Option B in my thread, thank you π
i hear job system has some overhead so it's not recommended to use on things that take less than 0,05ms
but i'm using Entities.ForEach() and it seems to be causing 0,05ms overhead (with just one entity and one component) per time that method is used (e.g. twice in same system or once in two systems is both 0,10ms)
is there a better way to iterate components on the main thread?
ofc could be this is just editor overhead, i'm not sure how to check build since i'm looking at entitydebugger
@hollow sorrel I'm trying to use ForEach, but I can't seem to get access to it. Whenever I try all i get is "foreach". Is there something I'm missing
yeah it's Entities.ForEach() now
Is this is correct way of managed allocation inside execute
Nativelist<int> one;
Execute
{ one = new Nativelist<int>(Allocator.Temp)}
I am using 19.1
Is it a good practice to use additional component to mark changes on concrete entity? I mean we can simply skip chunks that have no changes with version comparison but can't detect changes per entity.
It is giving me error that container is not assigned, DO I also need to create container and assign it via scheduling job in update method even though I need some temp allocations inside jobs ?
@neat magnet i think it will always produce an error if u will not assign variables of your job, so u need assign "one" NativeList in main thread. But u can allocate NativeList inside execute.
public void Execute() {
var one = new NativeList<int>(Allocator.Temp);
}
Yes, but if u sure that u will not break up race conditions
Thanks @frosty siren
Small clarification: u can only use [NativeDisableParallelForRestriction] for fields declared in jobs not for variables inside Execute()
it's obvious, but still
struggling to find information on what the "Convert And Inject Game Object" conversion mode in the "Convert To Entity" MonoBehaviour does. anyone able to point me in the right direction or tell me?
@frosty siren I just have a bool directly in the component that tells me if it changed for now
@junior fjord but if i have system that just read component value and not bool part it's better to separate, isn't it?
yeah, actually can't think of any reason why one should not seperate
you are probably right
@junior fjord I guess my major problem is that I don't know how to get separate entities to interact with each other.
Is there a good way to get the index of an element inside of a NativeArray?
@vale gorge do you mean searching for an array entry by value?
@left spindle you could check the examples on github, they make a pretty god tutorial
I have the NativeArray element, but I need to know it's index value within the NativeArray.
I am going through them, the Hello_* ones?
yes
I don't see any ones where entities interact with other entities.
yes, so you have the value and want the key. Why not just do
if(array[i] == value) break;
use i
what do you mean by interact?
do you mean physics?
Yeah, that's one way. I was just making a grid of items, then I wanted to shoot a ball into them, and determine which item it's closest to, as an exercise.
that is a pretty advanced example for a beginner exercise
@junior fjord I would, but it is a very large array and causes a massive hit to performance
the physics stuff is pretty new and not as well documented. There are physics examples and you could do that but if you want to learn about ECS I'd start with something simpler first
else you'd have to grasp how ECS works and how the brand new early-acess-y physics-ecs works in one go
I know how to make things interact with themselves. I've gone through the tutorials. (It's awesome!)
Now I'm trying to move onto something more advanced.
@vale gorge yeah if you explain a bit more how you got around to having that problem maybe I have an idea, but if your array is not sorted or anything I don't think there are more efficient algorithms than that
NativeArray does not have this functionality implemented
Like GetComponent<> but for ECS...
Coming from OOP it's a major change in thinking! Pretty cool though.
yes it has a steep learning curve but it is rewarding
but interaction basically happens in the systems now, you don't have these event like functions
I am basically programming a simulation so I did not need to learn about physics yet, but I'd say you should first have a very solid understanding of the ecs itself before moving to using the physics stuff
or try go ahead and read the physics examples in the repository
they even have shooting stuff etc., its just more complicated
The physics samples in the repo throw errors on my machine, unfortunately.
do you have Entities version 30 and the newest unity?
they worked for me like a week ago
That's another thing, it keeps wanting me to "upgrade" to .24-timefix or something.
hmm, sorry I've never heard of that
you need the Entities package version 30 and the hybrid renderer
and probably also the mathematics package
Here's the error when I try to open the project
I have NO idea where to get the nuget
@junior fjord so what it is is I am rebuilding a mesh. But I want it to keep vertex color data for the vertices that have not changed positions sense the last rebuild. I was using a Dictionary where they key was the position and the value was the color.
But to stop the game from freezing I am moving it to a job. So I can't use a dictionary now. So instead I have native arrays that store the old positions, and colors, and set that stores the new positions and colors. And check to see if the positions are the same and if so I copy the color over. But I can't get the right index for the color unless I can get the index for the corresponding position.
Hope that makes sense, and I appreciate your help. π
as least how I understand that
But...I don't know how to do that, it's not in the package manager.
yeah sorry, I'd post on the forum to ask
I have no clue
hmm is there no way to alter the rebuilding mesh algorithm to somehow keep track of which positions change instead of doing it afterwards?
i have a trouble. when i start my project unity editor not responding after "Resolving packages". Do u know what it is and what i can do to solve it?
UPD: here is solution https://answers.unity.com/questions/1428939/unity-201720f3-resolving-package-error.html
UPD 2: solution worked only a day ago but now any steps from post doesn't change anything
Hey everyone, I opened the DOTS sample scripts in Rider and I get these error messages, but on Unity they don't show up. Any ideas how to get rid of them?
But really for graphics and physics I am the wrong guy, I am basically writing a simulation
@safe gate I have the same problem and did not get it resolved
are you also getting it in a IJobChunk?
Sadly no.
@junior fjord too bad. I'm glad I'm not the only one at least
does unity still compile though? I am rewriting a lot and did not have my thing in a compile-ready state the last few hours
Looks like a Hashmap could work for what I want. But I can't seem to find them. I have using for Jobs, Entities, and Collections. But can't seem to find them.
Are they only in 2019.1?
Aha! It's under "Mono Cecil" in the package manager, not nuget
Another problem that I run into is the "ArgumentException: Object at index 0 is null" happens every time I hit play, for instance, in the physics example "Hello World"
That and a LOT of instances of "Internal: JobTempAlloc has allocations that are more than 4 frames old"
Sometimes in the editor, and I can't get it to stop. I have to load a new scene.
I can't learn a thing from the physics samples...
Any ideas why this
[NativeDisableParallelForRestriction]
public NativeArray<Color> newColors;
[NativeDisableParallelForRestriction]
public NativeArray<Vector3> newPositions;
public NativeArray<Element> old;
public void Execute (int i)
{
newColors[i] = Color.white;
Vector3 pos = newPositions[i];
// This line specifically.
Element point = old.Where(p => p.Position == pos).FirstOrDefault();
if (point.Color != Color.clear)
{
newColors[i] = point.Color;
}
}
Is getting me this error IndexOutOfRangeException: Index 0 is out of restricted IJobParallelFor range [256...319] in ReadWriteBuffer.
Does the old need the [Native...] attribute too?
Stupid... yeah.... thank you.
I even looked to make sure I had the attribute on stuff...
Hooray I helped!
Has anyone else had issues getting entities rendering quads to sort in any predictable way?
Transparent?
Yes
Nothing I do seems to work. If they arent entities, and just gameobjects, they sort fine. Its a 3D scene with quads moving along Z.
Otherwise it seems like the latest created entity is always rendered on top.
Opaque seems to work fine
Before I do a bunch of work trying to reimplement something. Am I right in thinking that you can't use a variable that is a class in a job?
Even if it is all self-contained with in the job.
I am creating nativelist like this in 19.1 var p = new NativeList<int>(Allocator.Temp);
IS there any way I can read and write on this list
Unity showing error The native container has been declared as [WriteOnly] in the job, but you are reading from it.
All I'm doing is spawning entities over a bunch of seconds at the start of the game. None of this archetype have been destroyed yet, only spawned. And yet the chunk utilisation for this archetype looks like this. I would have expected chunks to be filled. Wonder why not...
interesting, over time as I spawn more and more, the histogram improves, but it always starts out not so great
meh whatever
@minor sapphire do you know how to read that chunk utilization? I never get it
I think so
you want tall bars to the right
y axis (162) is number of chunks
x axis is chunk size
tall bars to the right means you have many chunks where each of them is full (highly utilised)
my graph shows I have a heaps of chunks that are only partially utilised
which is strange considering I have not removed any entities of that archetype when I took that pic
Guys, can I perform an entity query from within a job?
oh it's a class so probably not π
@minor sapphire thanks that makes sense
ok if I have jobs A and B scheduled from one system where B depends on A, can I pass data from job A to B?
Should be able to
Is there a way to batch EntityCommandBuffer.Instantiate and EntityCommandBuffer.SetComponent calls?
queuing up all these commands to be executed one by one seems unfortunate
You can't use reference types within a job at all correct?
@vale gorge thats correct
Well shoot. Alright, thanks.
@minor sapphire are u sure u are not in case, when u use SCD which changes data layout?
@neat magnet if I understand u correctly u just need to not mark your list at all and u be able to read and write
Entity queries are done main thread. If you are wanting to use it with say a Foreach job, you can pass it when scheduling
To answer an earlier question from @minor sapphire
Is there some equivalent of a callback for when a component is removed via EntityCommandBuffer.RemoveComponent?
I want to execute some code whenever a certain component is removed, but I remove it in a job so I can't call the main thread
You could create a message entity?
how does that work?
As in, an entity whose sole purpose is to signal to another system that something has happened
ok. so have a system that just constantly iterates over the message entities?
and do something if there is one?
Yeah, that's one way to do it
Alternatively, send in a nativecollection (typically array) of size one to the job which potentially removes the component. Then, if it removes the componenet, you change the content of the array.
Usually a NativeArray<bool>[1] (or int)
That requires you to use system dependencies and to make sure the previous job finished (change or no change) before you access it, though
when you say send in a native array, do you mean create another system that has the UpdateBefore attribute and somehow modifies the current system that removes the component?
sorry, im not really familiar with interaction between systems/jobs yet
So the nativecollection-as-message method means you have two or more systems, where SystemA has read/write access to a nativecollection.
It sets the values as it needs to according to whatever needs you have.
Then other systems can set that system as a dependency, run SystemA.Complete(), and then access the nativecollection to read the values. These systems should probably only have read access, so that you can use the output from SystemA in multiple other systems. That or you end up making a chain, if that's something you might want.
ie. SystemA does stuff, then SystemB takes that as input to do something else and has an output etc. Pretty sure in you want systemB to make its own collection, though, just in case. To make sure you write once, and then only read.
ok that makes sense
The entity-as-message method is simpler, but has up to one frame of lag, and you need to do some trickery if you want multiple consuming systems. - The initial system would need to run before everything else, and you'd need a MessageDeleter system that ran after everything else. That or make sure the last two things that happen in a frame is to delete old messages and then make all the new ones that have been 'queued' somehow.
IsystemComponents I beleive are your ideal scenario for when an entity is removed
Ah.... scroll locked.... 
any idea when ECS is gonna come out of preview?
i think around this summer for the core parts
hey so i was just doing som stuff with jobs and realised it would be great if i could have a job wait in its execution. so do something, wait a bit, do another thing etc etc. is there a way to do that?
@solar hill no idea why you would want that but you can run a for loop from 0 to 100000 to simulate waiting
@toxic walrus well say you wanted to start a process running and have it run in the background then after a predetermined amount of time do something again.
i allso have some more abstract questions about the "correct way to implement certain things. for example caching data in the system that a job is running on. for example, if you needed something to have a timer or multiple stages or something would it be better to cache that data in a hashmap on the system that uses it or to keep it in a different component attached to the entity?
i feel like using a different component would lead to having tons of tiny annoying components say a "current step "component and a "timer" component, just to name a few.
My dilemma comes from having seen most of the info on ecs say to just use little components whereas the unity demo for BOIDS stores a lot of data in the system. Is that only really smart ina scenario where you have a lot of things doing similar stuff using SharedComponentData?
@solar hill just applying some logic to your questions...
caching a timer on the system, or on the object - decide this simply by figuring out whether each entity needs and independent timer, or are they all running from a central one? If its from a central one, send the data into the system, if its independent, place a new component value.
In the boid example, the system needs the central timer,
because, for example, the entire school of fish is moving in time, wiggling sideways to look like they are swimming.
I have a similar question, more to do with where to break the system down. I have,in the simplest description, intentions to write a movement system and a rotation system, and i need drag calculations, should i have four systems, one each for the movement/rotation then one each for drag?
What would you think, faldor?
hmmm interesting i think if we are looking at a system that may be used on a huge number of entities it is worth taking into account the individual cost of getting a component. i think the "best practices" would probably tell you to break it into 4 systems. However from a performance standpoint having your movement and drag be done in the same system would eliminate the need for essentially identical entity and component retrieval between each system and its associated drag system.
Besides, when really thinking about it, drag is an essential component of movement. I would say that there is not a strong argument for separating them.
The cost of getting a component - they are gonna be paying the cost anyways, and for your timer, it's data is basically a float (i guess), so its not a huge concern. If the component system is using the chunks method rather than direct entity management, this wont be much of a concern.
As, in the chunks method, the chunks are already sorted into the collections of data that match into the same groups.
well that's not entirely true, getting the entity architype twice for two different systems, using essentially identical data, is less efficient. Getting the list of entities for a specific combination of components does have a tiny cost. That cost might be apparent when iterating over potentially millions of moving entities . It really does depend on the scope of the number of entities this system will deal with
You are referring to my question now, i guess? I will give a quickidea of the usage case.
i was to you question in both my answers
My game is a space based mmo, with self contained solar systems of planets orbiting suns and with many interconnected solar systems, connected by starlanes.
The ecs systems involved in the movement will exist in two forms, one of them is client side, it runs a single solar system at a time, the other is server side and will run the movements of a large number of ai ships in all the systems simultaneously.
well i think your game is probably a prime example of a time when you may want to consider the cost of searching for entities twice when you don't have to.
I am hoping for 2000+ systems, with half of them possessing a number of ships they'll send places, so i imagine we are talking something like 5000 simple background entities, the server's movements will consider time and space, but not really collision, whereas client moves will be fully collision calculated.
So, the main question about the drag applies mostly to the client systems. But still the general idea, "How many small steps should i break the system down into" applies a lot.
It seems your generalised answer is, "Be careful not to break the steps down too small if the same selection of components is gonna be searched"?
yeah. well i think the best way would be to just run your own tests. I'm pretty sure that someone on the unity forums made a post about the cost of getting entities and it was small, but noticiable when getting lots of them
Ok, cool, was this a new post?
The archetypes system seems to be there specifically to avoid this cost of iterating to match entitys?
no, fairly old. tbh it may not be very relevant, ecs has changed a hell of a lot
Thats what im thinking, you know what an archetype is?
im not sure.... maybe. though you do still have to consider the cost of initialising a job, its not much but again, its there.
An archetype is a search shortcut, basically.
When it sstarts it basically scans your entities
the archetypes are automatically generated, it looks through your entities and any groups containing the same components, are then sorted into archetype groups
When you request the entities with the specific components for your system, its likely an archetype already exists making the get faster
If it doesnt exist already as an archetype, im not sure if it adds a new one - it would make sense that it did - but i do know that it can look at another archetype as containing this one.
So, before you even run a single system, ecs (at keast the newest one) already has your entities sorted into piles, and labelled piles at that.
hmmm.... this makes me want to run my own tests now.
However, im new to learning this, that might be wrong, but thats how i understood it
yeah it does make sense... though you would think you would end up storing a huge amount of useless data if you made archetypes of every combination, regardless of whether it is used or not.
Well i imagine that the archetypes themselves might be somehow considering what a sensible combination is, maybe it looks at your systems or something, im not sure.
Rather than every combination. It wasnt 100% clear what defined the archetypes, it was just clear that the sorting was already done. Do you use the jobchunks?
You can send two different jobs to act on the entities, one does just one entity, the other does a chunk of enemies (chunk is their chosen term) but basically a group with same requested components
Altho i was just thinking... For every job added to the heap, there will be a small cpu pause somewhere at some point when it calculates. So, if you make any job chunk too complex, then surely you will end up with a system which has fluctuating timings? In some ways, the pause between jobs coming more often in spite of their being more jobs might help the system stay smoother?
IsTrigger: if this flag is enabled, the collider is treated as a "detector" rather than as a physical body. This means it cannot receive forces from a collision, instead, it will raise an event to signify that an overlap occurred. For example, you can use this to determine when your player enters a specific region.
EnableCollisionEvents: this is similar to the previous flag, but still allows the body to push other bodies normally. The events that the simulation raises, then can be used to determine how objects are colliding -- this would, for example, allow you to play sound events.
This makes it sound like I can access some kind of events to know when things collided. Has anyone seen an example of how to do this?
In ecs?
ya
Im about to code my own cos their physics explanations are lacking.
It took me a hile to find out even that i needed to download it as a package hehe
is it from the new unity-physics package?
I think it might be about this:
Unity.Physics.SimulationCallbacks.Phase.PostCreateContacts
At this point, we have performed the low-level collision information between every pair of bodies by inspecting their colliders. You can access this through Simulation.Contacts where the data is in the format of a ContactManifold containing some shared properties, followed by a number (ContactManifold.NumContacts) of ContactPoint contact points.
yes
But my own use case, being able to remove the background automatic physics is very useful. Calculating physics in space is quite easy tho.
That looks like the thing - where are you getting that from? Looks like usefull information there
Damn yoo!
Hehehehe thanks for the link ( @minor sapphire )
I wish id known that was there sooner
so it looks like as the simulation runs its various stages, data becomes accessible
so perhaps you would add a system that queries for that data and then raises events in ECS however you like to implement that
Makes sense. (Thanks @solar hill for your thoughts too :) )
Im thinking of a method which should enable me to completely eliminate the possibility of fast movement resulting in missed collisions, tho. With the speed of stuff in my game the entire suite of unity physics has no way to work it.
I mean, 900 units per second is enough to make bullets jump through ships frequently, but, its space! Bullets should be real fast! And some of my ships exceed 1200 units per second... So they cant be hit when they travel more than a ship length per frame.
Here is an interesting question tho...
To collect the entities our system works on, we request some data components. Lets say, part of the processing that needs to be done is done by one system, and later, another system needs to use that data, but it isnt in the selection chosen by the components for the system, and its entity-specific. Is there a standard way that ecs would pass this information?
Would you for example, add components for the second system, and make the first system output its data to the entities components directly, so when scooped up by the second system it gets the data that way? Of course, taking appropriate steps to make sure that the first system always runs before the second
@solar hill "As you create entities and add components to them, the EntityManager keeps track of the unique combinations of components on the existing entities. Such a unique combination is called anΒ Archetype. The EntityManager creates anΒ EntityArchetypeΒ struct as you add components to an entity. You can use existing EntityArchetypes to create new entities conforming to that archetype. You can also create an EntityArchetype in advance and use that to create entities." - ecs package docs, unity docs website
So, it seems like it does track every unique combination, but, it does so by those that exist, not those that could exist.
I see, that does make sense. Though I would still like to compare a two system Vs one system solution to ascertain how much overhead there is when pulling the data from those entities.
is there a better way to quickly enable and disable systems for profiling than commenting them out?
@junior fjord you can disable them during runtime in the entity debugger, but not sure if you can do so before hitting play
yeah I just went with commenting out as always π
but I started to put systems and components together in the files so I now have to be careful about which parts to comment out
ah I can just manually disable enable the systems from my bootstrap mb
does anyone have any clue how come Time.Delta time always returns exactly 0.1f (i.e. 0.10000000001) when I am in debugging mode?
err that doesn't sound right π
yeah thats so weird π
if I am not debugging, my first frame is 0.02, then one is again exactly 0.1 and afterwards everything is 0.02 (I know, could be better :P)
if I am debugging, Time.DeltaTime ist just always 0.1
took some time to understand that that was causing some of my problems
when you say debugging, do you mean profiling or..?
I mean using rider and attaching a debugger to the thing
ah I see
I have a timing system that just counts up Time.DeltaTime and increases some variables like currentTimeStep for other systems
I had duration of a TimeStep set to 0.1f and every frame it made a time step and said durationLastTimeStep = 0.1f
I had to set duration of a TimeStep to 0.5f to realize that actually the Time.DeltaTime did always return exactly 0.1f π
but no idea why or how I could debug that
sounds like a nightmare if you get that in just a standard empty project... good for finding race conditions etc though π
wow trying to work through collisions in 2nd preview of physics is quite an adventure
kind of like solving a puzzle
but I'm learning bit by bit so if anyone is looking at collisions and collision events let me know, maybe we can compare ideas
@minor sapphire what do you mean by 2nd preview?
did they release it in the near past and is there documentation?
or is it still the same thing that the samples are using?
no nothing since april 8
I'm just making the point that it's a challenge working with such an early preview
yes of course, just wanted to make sure I did not miss anything
will chunk change version if i get access in IJobChunk to some DynamicBuffers and then i call AsNativeArray() at one of them?
@frosty siren I don't know, I imagine it depends on if you got the dynamicbuffer as readonly
btw with rider it is really easy to jump to the source code of the ecs which is often very readable
@junior fjord
Note that for efficiency, the change version applies to whole chunks not individual entities. If a chunk has been accessed by another Job which had the ability to write to that type of component, then the change version for that component is incremented and the DidChange() function returns true.
So what the benefit of using chunk skipping in case when i haven't accessed to component and i have no need to compute it and in case when i have accessed to component and i have no way to skip chunks which was really rewrited not just accessed?
I understand that i can have many archetypes with same component and different logic for this archetypes but one algorithm that work with this same component and in this case chunk skipping will have benefit i guess
Is the chunkIndex the same as an index(from say IJobParallelFor) for using EntityCommandBuffer.Concurrent and chunk jobs? or should I be using non concurrent EntityCommandBuffers with chunk jobs?
from within a IJobForEach, can I change component data for another entity? It does seem to go against the idea of tight memory access though. Trying to implement an event system and getting a bit stuck hmm
something like this?
private struct MyJob : IJobForEach<RandomComponent>
{
[NativeDisableParallelForRestriction]
public ComponentDataFromEntity<CustomComponent> CustomComponentFromEntity;
public void Execute(ref RandomComponent ac)
{
// get component from another entity...
var customComponent = CustomComponentFromEntity[ac.Entity];
customComponent.Value = 42;
// set component of another entity...
CustomComponentFromEntity[ac.Entity] = customComponent;
}
}
protected override void OnUpdate()
{
var job = new MyJob
{
CustomComponentFromEntity = GetComponentDataFromEntity<CustomComponent>()
};
job.Run(this);
}
@minor sapphire
Oh I haven't seen customComponentFromEntity before
this is just how I named my variable^^
Oh on mobile and it wrapped lol
H thougt I tried that. It said I wasn't allowed to write into that container...
ah
ah yes, you need to add the attribute [NativeDisableParallelForRestriction] on the variable
I forgot to add it on my example x)
Ohhh I see
So it's doable, but I'd have to careful about what's happening to that component in parralel?
yep
you can also run the job without it being in parrallel by using .ScheduleSingle() instead of .Schedule()(in case you have some problems with parrallelism...)
Oh I see Ty!
Super helpful man Ty. Now I can't wait to get back to work tomorrow heh
π
hey all. is there like.. somewhere to keep up with the current state of the ECS? i keep ending up with different examples from different stages in its development. The samples don't seem to cover everything and the docs for up to date stuff are generally empty
I'd love to play around, but it ends up being such a headache
experiment and ask questions, also check out the pinned message here
i am experimenting.
here's a question. i'm trying to make a movement system using an IJobForEach, if I use LocalToWorld, can i set the value of it to move an entity?
No, it's the Position component you should change
At least, I'm pretty sure the LocalToWorld component is, in fact, there to help you convert the local position into a world position.
there isn't a position component anymore i believe.. it's Translation. but my entities don't have that, just a localtoworld component, which seems to have translation, rotation, and scale rolled into one
so if i try to query entities via translation it doesn't seem to do anything?
lol ok nevermind it's Translation.
I believe I need to spend some time to actually understand the Entity Debugger correctly.
Hmm, merging them makes sense, and they renamed it too, huh.
you could modify the localtoworld directly, but its not as convenient as modifying the translation
Should i call EntityManager.GetArchetypeChunkBufferType<T>(bool) to get type every OnUpdate() or just once? The question is: "what information contains ArchetypeChunkComponentType/ArchetypeChunkBufferType/etc?"
i do it ever frame and it seems to work fine, but I have wondered about caching it
Do u dispose your native container every OnUpdate() or initialize it once and then just clear it?
eh nativecontainer for what?
does LocalToWorld.Position return a world space pos? It seems to be giving me world 0,0,0 despite my object being at y -50
what is the correct way to get a world space position from an entity, plus say 10 units along its local y direction
hmm I must be misunderstanding something
perhaps the transform system hasn't run or something? But that doesn't make sense cause translation.Value has the correct position
maybe I just misunderstand the purpose of LocalToWorld
that is perhaps all local position and direction information
oh but it's actually correct within a different system...
must be something about timings when considering spawned entities
Is it in world pos (0,0,0)?
maybe I should explain more.
I have a ship, spawned by a ship spawner. The ship spawner's transform is in the pic above, at -50, and rotated.
The ship spawns as expected, at -50, and rotated as expected.
The debug output for the ship spawner looks good.
SP in the debug output for 'Ship Spawner'
The spawned ship has a Weapon component
it is trying to spawn a bullet. The rest of the debug info is from the weapon system. It thinks the ship is not at -50, but rather at 0,0,0
and it thinks it is not rotated
but, at the very bottom
the TRANSLATION component is correct, showing -50
So, for some reason, the LocalToWorld component does not agree with the Translation component...
are you trying to spawn a bullet as a child of the ship in terms of auto transforming?
yes
I would like the bullet to spawn at the position of the ship + say 10 units in its forward direction
childing is for when the bullet would be part of the ship
no wait, I would not want the bullet to be a child of the ship
what you want is to spawn the bullet at ShipPos+(Ship.forward*distanceFromShip)
I want the bullet to be it's own entity not childed
Vector math, it can be tricky!
But yeah, there's a reason why it givens you the forward vector
yeah, I agree with your math there, but in the weapon system (on the ship entity), the LocalToWorld.Forward is incorrect.
wait wait
lemme just confirm that
If it's rotated in some combination of 90 degrees, then you probably just messed up the rotation of the ship model/sprite in the first place
no it's incorrect.
in this image, the first four 'sp' lines are the Ship Spawner
https://cdn.discordapp.com/attachments/497874303463850004/571606057567125545/unknown.png
the ship spawner is rotated roughly 45 deg
the next 5 lines are the ship's weapon system showing the LocalToWorld and Tranlsation component values
they SHOULD match the values of the Ship Spawner
Notice that the Translation.Value does match
but the LocalToWorld values do not
the ship spawner is a direct ConvertToEntity object in the unity hierarchy. The ships are spawned after hitting play. I wonder if that is related
The forward.Y float seems to just be a rounding error. 1E-8 = 0,00000001
In fact, so does the up.Z
yes, I'm just treating it like 0
For all intents and purposes, the direction vectors seem to be ~=Quaternion.identity, aka world rotation
yes precicely
Meaning you simply don't apply the rotation?
Could you share your system code? Just the spawning bit
The ship itself is rotation 45 degrees
Ship Spawn
https://pastebin.com/bHY5xYE3
this is working as expected
This is the weapon system
https://pastebin.com/iSznqAPc
This is where the LocalToWorld component is giving me unexpected values
Oh god I have to go to dinner, I'll be back soonish
So if I understand correctly the ship spawner spawns a ship in a random nearby location? And that works as intended, correct?
The line where you set the owner shared component look like an odd way to do that to me, but I haven't touched those in a long while so not sure what the 'proper' way is. If it works, it works.
It seems pretty clear to me that the values sent to the weapon job are incorrect. If I understand IJobForEachWithEntity<TEntity, T1, T2 [...]> the other values will be taken from the TEntity entity, meaning you get the (local) location and rotation of the weapon, not the ship.
In short, to fix this you need to find out how to either get worldPos and worldRot from the wep's components, or get the other components from the correct source. Might still need to covnert to worldSpace, though.
The weapon is just a component on the ship entity
It's not its own entity, so it doesn't have its own transform or pos or anything
Hmm
I feel like maybe the transform sysm has not run yet to convert translation and rotation into localtoworld
I couldn't see that system in the system list so not sure when I should schedule my systems...
Perhaps I could try waiting a frame before firing
I'll try that when I'm back at comp
Perhaps I should stop using Localtoworld and use the the translation and rotation components instead.
it does indeed seem fine if I don't use values in LocalToWorld immediately after a spawn.
so likely issue is the transform system didn't have a chance to update LocalToWorld yet. It must be waiting for my logic to update Translation, Rotation and Scale before applying to LocalToWorld for rendering
I guess in this case one should use Translation and Rotation if you want up-to-date values if perhaps you are relying on information updated from a movement or rotation system, and indeed immediately after a spawn
wonder if it's a bug though
that makes sense, im not sure when localToWorld is updated, i would assume you cant use it until next tick. Maybe u could set a group dependency on the system to get yours to run directly before it.
Hi all, can i PM someone asking some noob question about ECS? Or some links? i haven't started learning it but my main concern if i even need for some things im doing
@stoic monolith if no one has replied you can pm me. I'm new myself but getting the hang of it.
And now for a question of my own.
Anyone know if there is a SharedComponent version of ComponentDataFromEntity? Or in other words, how do I get shared component data from a referenced entity within a job?
Do particle systems work with ECS? My instantiated entities have child objects with particle systems that don't seem to be working
I don't think there's ECS particle systems yet, no, so you would need to spawn in gameobjects with particles separately. Or find another way to achieve whatever you're trying to do.
you can jobify shuriken particles tho
and visual effects graph is still mostly gpu I think
@gusty comet
Well yeah, but you can't turn a particle gameobject into an entity; you need some other method.
yeah, I guess you still need gameobject today for this stuff still
just pointing out the job system possibility in case that was one reason to ask about it
Overall I see a few alternatives:
-Use gameobjects for the particles
-Use some shader (or as Olento suggest, VFX graph)magic
-Olento suggest Shuriken particles via jobs, never touched that so can't say
Depends on what you're trying to accomplish, really.
I don't really suggest any of those, just pointing they exists π
I don't have first hand experience on jobified particles more than trying they worked on some early preview build + have tried VEG samples
The only one I can actually get behind of those is the gameobject method, as you (Pyrrhic) presumably know how it works. It introduces complexity as you need to have hybrid objects, and won't give you the (full) ECS performance, but it's probably the path of least resistance.
Again, depends on what you're trying to accomplish; a lot of things you would think of using particles for can sometimes be done through other means for a no worse result.
Starting to the get the hang of things. 8k ships in total, about 4k bullets at once. Everything has a sphere collider. 60-70fps. I'm not sure that I've done it well though. Gotta wrap my head around things like sync points and try and understand why I don't see a lot of activity in jobs threads in profiler.
hmm I need to check vsync lol
hmm it's off, fewer ships have more FPS
Do you have a gif/video of that? Even just a couple seconds would be interesting
@minor sapphire this is open source and works pretty well https://github.com/NickeManarin/ScreenToGif/releases
I used it today
and I am also interested in a video
but how come you don't see job activity in the profiler? thats odd
I see it, my problem was actually why the systems still also took that much time in the main thread
Oh I do I guess I'm just not yet understanding where the work is
allthough the update method did nearly nothing besides crating the jobs
yes I have a lot of main thread action
do you use the buffers a lot?
dynamic buffers? native collections?
no I meant the command buffers
oh right
I do a fair bit yes
every time a ship is destroyed or created, I'm using the buffers
plus a couple of other things
for component adding/removing you could also try to just add the components with some default values beforehand
thats a thing I changed at some point which gave me the opportunity to burst compile more systems
anyone here doing ludum dare?
ok I got a 20ish mb recording lol
hmm
where to upload...
here we go
really wanna figure out what the bottlenecks are
heh, it's 1 week of ECS learning
gotta get used to reading profiler timeline with jobs
is this with the new ecs unity physics?
really nice demo
might be the time to whip out my old bullet hell project again
yes that's the new physics
everything has a sphere collider on it as triggers
in my trigger system I query all the collisions generate from untiy physics and look for anything with the 'HandleTrigger' component, and attach some trigger event into to a Dynamic Buffer along with a 'TriggerInfoTag' so systems can query for that and know they have some stuff to handle
the 2D colliders aren't available yet so that's all with sphere colliders
but look at the cost of the physics step here
due to a bug where kinematic colliders cannot raise collision/trigger events with other kinematic colliders, I made all bullets dynamic but adjusted properties so they would act as kinematic
on the forums it was confirmed as a bug
with everything as kinematic, not needing to run any physics forces code, I wonder how much cheaper it will be
Yeesh, that's kind of a critical bug since the whole point of kinematic is to NOT have that physics cost...
lol yes
Also, the thingy (can't really call it a game) looks pretty cool, do you have controls for how many ships it will spawn or how fast, or are those compile-time?
that's on my to-do list π
at the moment it's controlled in the ConvertToEntity monobehaviours
spawners, max ships per spawner, spawn rate, bullet fire rate, bullet life time...
I even have a blue team lol
disabled currently
can add more spawners with other models and stuff
didn't expect the performance to be that good,
also, is it true that the unity physics don't rely on state? (from prev frames etc?)
I am waiting for the 2d physics too, but I would only need 2 shapes: circle and line,
so that would be 3 resolvers, however, do you think it is easy/possible to whip those 2d solvers up and integrate it?
I think I will just wait for them to integrate it since they will eventually do so
but I'm using physics layers right now to get around lack of physics filtering editor controls that will come later
if you rely on their collision detection... it's slightly annoying right now
you have to go and get their collisions collection which gives you pairs of colliding colliders
then get the entities for those
and decide what to do with them
I think this would just be the ecs way, one system puts out the collision pairs and you decide what to do with them? doesn't sound bad, but hopefully it is a bit more convenient in the future?
Doesn't the new physics package already support/use ECS?
Physics is, as it turns out, complicated, and consists of a rough pass ('which objects are in the general vicinity of each other'), a fine pass ('Which objects actually collide') and finally applying all the forces. The package does all of that, and I suspect that whether it uses/supports ECS or not, you're not going to make anything more performant before it does, at least not outside the simplest possible implementation (no force, just 'are these boxes overlapping')
Yep, it's on ECS
when those burst jobs are complete (the physics system) you are left with a collection of colliding pairs
then you use it. So my system loops through the pairs to create a dictionary of <entity, List<collision info>>
then adds collision info to entities that are tagged with a 'trigger handler' component
then other system can notice that there is a trigger/collision event
and use the data
I use a dynamic buffer to list the collision events
cause an entity can participate in more than one collision in a frame
has anyone successfully compiled a uwp project with the latest burst+entities?
in fact is there any concrete info on what platforms dots can be built to currently? I dont know if I am wasting my time with uwp nor do I want to waste time trying other platforms if its currently unsupported
It's just normal code with a particular framework backing it, I'm pretty sure it should work everywhere the Unity runtime usually works. Haven't heard anything about that not being the case, and it seems like a caveat they'd mention.
Either way, super easy to test, just load up a sample project and build UWP and see if it runs.
was getting a burst compiler error when building, ugh building for uwp is just my nightmare for some reason
yep, empty project with burst builds, when I add all the other dots packages, errors out
bleh
is UWP officially supported for Burst?
hmm The UWP build will always compile all four targets (X86, X64, ARMv7 and ARMv8)
maybe im lacking the armv7 and v8 stuff for building.. but thats strange
Hey! This may seem rather trivial, but if you're looking for a raycast from the Camera.ScreenPointToRay() to work with the new physics colliders, here is the stripped down version from one of the examples provided on github: ```//Standard Unity Engine Ray
UnityEngine.Ray unityEngineRay = Camera.main.ScreenPointToRay(Input.mousePosition);
//Converted Unity Physics Ray
float rayDistance = 100f; //move this eventually
Unity.Physics.Ray ray = new Unity.Physics.Ray(unityEngineRay.origin, unityEngineRay.direction * rayDistance);
RaycastInput raycastInput = new RaycastInput() { Ray = ray, Filter = CollisionFilter.Default };
float3 mousePosition = float3.zero;
if (m_physicsWorld.PhysicsWorld.CollisionWorld.CastRay(raycastInput, out Unity.Physics.RaycastHit hit))
mousePosition = hit.Position;```
Hello ! Is there an ETA on Unity DOTS Animation ? https://bintray.com/unity/unity-staging/com.unity.animation
I think https://discordapp.com/channels/489222168727519232/497874303463850004/563862129007132673 is the closest we got
Thank you ^^ "Nice and vague" ^^
Hi guys, what is currently the best practice way to draw and animate 2D sprites with the ECS system?
no best practice at the moment i believe since 2d hasn't been touched? There was a repo where you can draw sprites somewhere with ECS somewhere i think
usually what I have seen done is that you use mesh renderers with a quad and use the sprites as texture
you can batch them up and pass materialpropertyblocks if you have a lot of the same sprites, that should slightly differ in some attributes, like color tint
Right, so just ignore the 2D renderer and use the 3D one on a plane. Will also have to figure out a way to animate them
@cunning perch I found this repo https://github.com/paullj/unity-ecs-instanced-sprite-renderer/blob/master/README.md
yeah i think that was it
Alright I will give that a shot.
Is it actually even worth trying to do it at the moment if you are not rendering 10k+ sprites on screen?
I wouldn't know, but ref. the earlier question, the simplest way to do 2D is to make quads with the sprite as a texture. Without looking at the above repo, I suspect that's what it does (though with a bunch of magic for perf)
yes it is doing something like that, but using a bunch of unsafe stuff though, and deprecated/obsolete functionality
I think for the moment I will just use a hybrid approach and hopefully by the time I am running into performance issues there will be natively supported solutions
10k sprites went okay last time I tested
not sure when they will provide native sprite rendering with jobs?
the issue is that the posted repository does the stuff in the main thread I think, so it is not jobified
correct, although I think you should be able to
hey guys, where can i find the megacity repo?
and how are you guys bootstrapping? do you have a bootstrap script?
@stark cloak thanks
The link's also at the bottom of https://unity.com/megacity
yeah i thought there was a public repo
what would cause a gameobject (image) that is masked to not show up until i make it inactive then active again?
actually I think it might have something to do with this Shape2D plugin
@quick stream not sure. OnEnable / OnDisable are called when that happens. Maybe those methods are modifying the mask.
I haven't used ECS in a while, do we still need a Bootstrap script?
good point, I'll check
hmm doesn't seem to be the case
ok so if I put a breakpoint on the mask-related code in the shape plugin... it works
so there's some weird async stuff going on
@final fox the dots roadmap talk mentions late this year for animation preview package(but its still vague)
it actually doesn't appear to be directly related to the plugin, weird
try isolating as much of your code as possible (commenting out) and locate the root cause. @quick stream
native rendering ?
is that an engine? if so, no
what do you mean "an engine". You mean Unity right?
I mean a plugin that's very large and functions as the foundation of my game
seems to be something related to rendering, if the game loads when it's in the background, it's fine
only happens when i have it open
you can attach a debugger to the plugins if you have their debugging informations
i don't even know if it has anything to do with the plugin
only thing the plugin is doing with these objects is changing their visibility, sometimes alpha
try disabling everything except the object that's getting masked - including non-related code
works fine when there's no unity mask component
you can also use a frame debugger
that will show you all the draw calls for the frame
there's also a renderman extension for unity
Is this in VS?
it's built in to Unity
huh
renderman you can download
Unity->Window->Analysis-> frame debugger
neat
it will give you full profiling information
this is more than neat
i had no idea this existed, it's so useful
you can backtrack
unfortunately it doesn't help when the thing isn't being rendered
oh!
it is being rendered
maybe this will work
ok, there is literally no difference between the properties of when it's visible, and when it's not
actually, one number is different, and it explains the issue
thank you so much for telling me about this tool
np, glad it helped
i wish VS debugger wasn't so buggy
it freezes and i have to crash unity
for something that messes up this much there really should be a panic button
(it just froze again just from trying to inspect a material object)
So it seems I still have an issue
the problem was that I'm changing a material shader property through code but this isn't causing the actual material to update
you might want to go to the #archived-shaders channel
Alright, thanks
not really sure how it cant be hybrid
but personally got a player input system just feeding input on the main thread into a player input component
yeah i agree, thanks
A new release of Tiny Editing tools built on top of Unity.Entities is coming soon. " ```
oo exciting
considering the three year plan, I seriously hope the new ECS editor will use it's own scene view
and not reuse the gameobject world's scene view
if they allow both at once, you can still do hybrid and have editor for both
I asked about this from Joachim ~6 months ago and he said there's no plan for such approach
but I hope they reconsider
having only pure ECS editor or only gameobject scene editor is serious limitation
I feed that they've hardcoded scene view so badly that they don't want to consider such thing
but if they did work to decouple the scene view, it would also allow them to do things like prefab editing in a separate window while still keeping the main scene showing the active scene
probably biggest questionmark on such approach is how to deal with hierarchy panel and inspector, as you can freely place those on main layout now and they'd have to be duplicated for each additional view
I proposed a solution for that on the forums (put a toggle to swap between DOTS and Gameobject editors on the fly)
I don't expect them to do that but it would solve so many issues
just really hope audio isn't too far off in the horizon
https://jacksondunstan.com/articles/5214 (Free Performance with Unity.Mathematics)
https://jacksondunstan.com/wp-content/uploads/2019/04/UnityMathematics.png
For these particular types and operations, weβre saving about 8-17% of the CPU time by switching to Unity.Mathematics.
most here probably use the new math lib by default already, still nice to soo someone trying to benchmark the difference
by here, I mean people who use Burst in jobs
@safe lintel That's nice ! That's enough for me π Thank you !
Is it normal to get null refs when seeing GameobjectEntity in inspector?
at UnsafeUtilityPatched.cs:15
I'm super new, trying hybrid..
I think you may get null refs when a selected entity is destroyed?
This is really just from the sample package.
They're editor window calls though, and seems harmless but spams all the time i click something else in inspector
oh yes I get some of that kind of stuff too
what are the use practices of Mathematics.Random? I've tried to create a single Random object that i can send to all jobs.. but when i use it, i get random values.. but those values are always the same for each job
am i meant to instantiate a new random inside each job? i was under the impression (from other random classes) that the best way to get good random results is to use a single instance across all elements
iirc this was a topic last week in this chat - and the consensus best practice seemed to be to have a random in each job, whose initial seed is determined by a global random