For the current project I’m working on I’m using NativeArrays on systems to store information about the games map. Sometimes other systems need access to these NativeArrays so I am using this to retrieve the native arrays:
All works well but unfortunately using this method to access a nativearray and using that nativearray inside a job does not update the job dependencies automatically, leading to x is attempting to write to y etc etc.
My current solution is to manually store the jobhandle of each system that accesses that nativearray, combining the jobhandles together and using that as an inputdep for scheduling. This obviously ends up in having to write a lot of pain the ass boilerplate code and ends up with very messy looking systems.
I currently feel like I have two options:
Write a custom system that holds all nativearrays used in other systems, which has functions to register usage of a nativearray and combines dependencies automatically.
Switch back to using dynamic buffers, which I used originally and shifted away from as I found the nativearrays easier and simpler to work with.
Anyone have any thoughts as to how they’ve dealt with this?
Are unity planning to change how this works at all?
Don’t do it if you can avoid it. Leads to a lot of surprise issues.
If you must (for example the collision world in unity physics), only write to the native container in the owner system then expose the job handle for writing so other systems can depend on it.
This is interesting because there was a thread recently were people were discussing whether to use native containers vs dynamic buffers for this sort of usage.
I mentioned that I wasn’t sure if Unity handles all the dependency management for sharing containers around and if your experience is accurate then it appears they don’t.
I’ve dealt with this by using DynamicBuffers, like it sounds you used too. I thought that DynamicBuffers were better suited and more explicit in usage terms for data that is a shared resource across multiple systems.
I generally only use native containers for data encapsulated to a single system.
This is being used for a very large game world and I’m after the performance of arrays vs hash maps. Changing values in arrays is much easier and quicker than hashmaps and given a grid dimension on a map it is very easy to get an array index. Also as far as I understand I’d still need to pass around inputdeps for hashmaps as they aren’t handled automatically in the component systems.
The dynamic buffer solution seems to be the correct way to go about this for me.