Search Unity

Single dynamic buffers

Discussion in 'Entity Component System' started by Parsley1, Oct 8, 2019.

  1. Parsley1

    Parsley1

    Joined:
    Oct 8, 2019
    Posts:
    1
    So i'm making a game that sits on a 2d grid. I have a singleton entity with dynamic buffers on it to represent the grid. But i'm having trouble with how to actually use these buffers in jobs without the safety system complaining constantly.

    As an example, i use a grid based lighting system. The map has a dynamic buffer that stores the light level at each grid. I use an ijobforeach to iterate over all entities with a light emitter and a position and write the strength of the emitter into the corresponding entry in the buffer. A 2nd job then spreads the light to adjacent grids, decreasing its strength by 1 each step. Similar to how minecraft lighting works.

    The dynamic buffer isn't on the entities being iterated on, it's on a seperate singleton entity (the map). so i have to pass it to the job separately when i create the job. i.e:

    Code (csharp):
    1.  
    2. var seedHandle = new SeedEmittersJob
    3.             {            
    4.                 mapBlocks = EntityManager.GetBuffer<S_MapBlock>(mapEntity),
    5.                 mapLight = EntityManager.GetBuffer<S_MapLight>(mapEntity),
    6.                 lightSpread = lightSpread
    7.             }.ScheduleSingle(emitterQuery, inputDeps);
    8.  
    I've tried a few ways of doing it but they're all either blocked by the safety system or really cumbersome and limiting.

    If i use entitymanager.getBuffer (like above) to get the buffer from the singleton entity it seems to be counting that as a read of the buffer occuring at the time its called (as opposed to when the job runs), so i can't use that to give the buffer to a job because the safety system complains about any other jobs using the buffer not being completed.

    If i use getbufferfromentity() then it passes in an array of buffers instead of just the one. I then have to pass in the mapEntity as well so the job can get the array at runtime (which makes it a bit more inconvenient to use). It also means that i can't then use the buffer in any parallel jobs as the safety system will complain about the container not being suitable for paralel jobs. I can't use [NativeDisableParallelForRestriction] to get around this because it will apply to the BufferFromEntity that the job is provided with, the safety system will still throw errors when i read from the buffer itself.

    If i give up on using buffers, just keep nativearrays on a map system and do world.getexisting to grab the the system then the safety system doesn't recognise any job dependencies between systems that use the same arrays like it does with systems that access the same entities and its constant 'you must add a depndency or call complete' errors every time 2 systems use the same array.

    There must be a proper way of doing this that i'm missing. "have an array of stuff" isn't exactly a weird edge case and none of the obvious methods work well (if they work at all) with the safety system.
     
    Last edited: Oct 8, 2019