Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Unity freezes after play

Discussion in 'Entity Component System' started by Klusimo, Jan 4, 2021.

  1. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Hil, I have this code revolving around dynamic buffer and when I hit play, Unity freezes for pretty random amount of time but when I do the calculation again, it works perfectly until I stop and play again. When I opened profiler, to my surprise, it was the editor which caused the lag spike. I dont know what to do with it, do you have any ideas?

    The code that causes the issues (tested):

    First one that adds dynamic buffer to entity:
    Code (CSharp):
    1.                     var tileData = ECB.AddBuffer<TileData>(entity);
    2.  
    3.                     for (int x = 0; x < chunkSize; x++)
    4.                     {
    5.                         for (int y = 0; y < chunkSize; y++)
    6.                         {
    7.                             tileData.Add(new TileData
    8.                             {
    9.                                 TilePosition = new int2(GetTileFromWorld(chunk, new float2(0, 0), 1) + new int2(x, y)),
    10.                                 WorldPosition = GetTileCenter(GetWorldFromTile(new int2(GetTileFromWorld(chunk, new float2(0, 0), 1) + new int2(x, y)), 1, new float2(0, 0)), 1)
    11.                             });
    12.                         }
    13.                     }
    Then one that actually sets the values, but only when needed (that is the ChunkTick bool):
    Code (CSharp):
    1.         Entities.ForEach((int entityInQueryIndex, Entity entity, ref ChunkData chunkData, ref DynamicBuffer<TileData> tileData, in Translation translation) =>
    2.         {
    3.             float2 position = new float2(translation.Value.x, translation.Value.y);
    4.             float2 chunkPosition = GetWorldFromChunk(GetChunkFromWorld(position, chunkSize, new float2(0, 0)), chunkSize, new float2(0,0));
    5.             var worldSeed = chunkData.WorldSeed;
    6.            
    7.             if (chunkData.ChunkTick == true)
    8.             {
    9.                 for (int i = 0; i < tileData.Length; i++)
    10.                 {
    11.                     var Tile = tileData[i];
    12.  
    13.                     var heig1 = math.abs(noise.cnoise((Tile.WorldPosition / PerlinSeeder(worldSeed)) / 1000));
    14.                     var heig2 = math.abs(noise.cnoise((Tile.WorldPosition / PerlinSeeder(worldSeed)) / 10000));
    15.                     var heig3 = math.abs(noise.cnoise((Tile.WorldPosition / PerlinSeeder(worldSeed)) / 1000000));
    16.                     var heig4 = math.abs(noise.snoise((Tile.WorldPosition / PerlinSeeder(worldSeed)) / 1000));
    17.                     var heig5 = math.abs(noise.snoise((Tile.WorldPosition / PerlinSeeder(worldSeed)) / 10000));
    18.                     var heig6 = math.abs(noise.snoise((Tile.WorldPosition / PerlinSeeder(worldSeed)) / 1000000));
    19.  
    20.                     Tile.Height = (heig1 + heig2 + heig3 + heig4 + heig5 + heig6) / 6;
    21.  
    22.                     tileData[i] = Tile;
    23.                 }
    24.  
    25.                 chunkData.ChunkTick = false;
    26.             }
    27.         }).WithBurst().ScheduleParallel();
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,652
    Which Unity version, which packages version. Why you use
    .WithBurst()
    it's not necessary if you on SystemBase, if not - then you on old package version and\or using something like ComponentSystem.
    If you on 2020.2 - do you see any dialogue with the process? (and burst compilation at borrom right corner)
    Your editor in release mode or debug mode?
     
  3. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Editor version: 2020.2.0f1
    Entities package version: 0.16.0 preview.21
    I am using SystemBase and why shouldn´t I use burst?
    No errors/logs are written when it happens.
    Editor on release mode.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,652
    Because it's by default bursted. You don't need to write WithBurst. ILPP will compile it to IJobChunk with the required attribute. You can disable it by WithoutBurst, but don't need to enable it as it's enabled :)
    I'm speaking about popup dialogues and background tasks window, not logs :)
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,541
    Try to disable burst and see what haapens.
    Anny consol messages?
    Try add debug log.

    Do you have full stack error enabled?
     
  6. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Oh good to know, I just assumed that if there is the option to do withburst, I have to toggle it, thanks!

    I dont know what you mean by popup dialogues and background tasks windows, so I assume there weren´t any, althought I do get "Enter Safe Mode" when I launch the project.

    Disabling burst made no difference. No console messages and it works normally after the initial lag spike. I tried debugging with logs and found out the code that caused it, its the one above, otherwise I found nothing. I am not 100% sure what is "full stack error".
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,541
    Sorry, I worded it wrongly. I meant Full Stack Trace.

    upload_2021-1-4_16-3-54.png

    But if you have nothing in console, that may make no difference.

    Another thing is, do you have Enter Play Mode enabled?

    upload_2021-1-4_16-4-59.png
     
  8. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    I have full stack trace enabled, but as you said it makes no difference without errors.

    I have enter play mode experimental enabled, but now I tested it without it and no difference...

    I also remembered one thing, I had the same problem with older versions of unity and entities package, so I think we can be pretty sure its something with the code.

    Testing further, when I disabled all those math functins it severely lowered, I´d say about half the time and when I disabled the function adding data into the buffer, it lowered further, near innoticable, but still there.

    It is almost logical it is some "too complex calculation", but when I do the very same complex operation again after the first lag spike, it works perfectly... I´ll have another look into profiler.
     
    Last edited: Jan 5, 2021
  9. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    So I somehow fixed it. I first noticed I had disabled burst (I mean in the editor option) so I enabled it, it didnt work (maybe even prolonged the lag I dont know), but then I disabled the two options bellow "enable play mode (experimental)", the reload scene and reload domain and voila.. fixed. Wierd especially because I had these 2 options disabled before and it still occured, but maybe it is the combination of these 2 disabled + enabled burst (I dont know how long I had it disabled to be honest, I thought it was on the whole time). Well if it happens again ill write another post or something.
     
  10. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76