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 Could burst screw up data that is in memory?

Discussion in 'Burst' started by Hafinator, Sep 14, 2022.

  1. Hafinator

    Hafinator

    Joined:
    Apr 19, 2017
    Posts:
    6
    Hey, I'm having an issue with incorrect data being presented around where I trigger my burst job.
    The situation is as follows. I have a bunch of spaceships which just move around using burst. They have the current speed, max speed, acceleration and rotation.

    /
    Code (CSharp):
    1. // Fill the collections with data
    2. for (int i = 0; i < AllNonCombatShips.Count; i++)
    3. {
    4.     if (AllNonCombatShips[i] != null && AllNonCombatShips[i].gameObject.activeSelf) // ship is alive
    5.     {
    6.         moveJobFinishArray[i] = false;
    7.         moveJobTargetsArray[i] = new float4(AllNonCombatShips[i].Target.position, AllNonCombatShips[i].KeepRange); // position, distance (squared)
    8.         moveJobTransformAccessArray.Add(AllNonCombatShips[i].transform);
    9.  
    10.         if (AllNonCombatShips[i].CurrentSpeed > AllNonCombatShips[i].MaxSpeed)
    11.         {
    12.         }
    13.         moveJobSpeedsArray[i] = new float4(AllNonCombatShips[i].CurrentSpeed, AllNonCombatShips[i].MaxSpeed, AllNonCombatShips[i].SpeedChange, AllNonCombatShips[i].RotationSpeed); //CurrentSpeed, MaxSpeed, SpeedChange, RotationSpeed
    14.         if (moveJobSpeedsArray[i].x > moveJobSpeedsArray[i].y)
    15.         {
    16.         }
    17.         moveJobStateArray[i] = (int)AllNonCombatShips[i].ShipState;
    18.         moveJobWaitArray[i] = AllNonCombatShips[i].WaitTime;
    19.         moveNamesArrayDev[i] = i;
    20.     }
    21. }
    22.  
    then I set the values to the job properties

    Code (CSharp):
    1. moveJob.DeltaTime = Time.deltaTime;
    2. moveJob.HasFinished = moveJobFinishArray;
    3. moveJob.TargetPosition = moveJobTargetsArray;
    4. moveJob.Speeds = moveJobSpeedsArray;
    5. moveJob.ShipState = moveJobStateArray;
    6. moveJob.WaitTime = moveJobWaitArray;
    7. moveJob.indexesDev = moveNamesArrayDev;
    8.  
    9. moveJob.Schedule(moveJobTransformAccessArray).Complete();
    10.  
    and after the job finishes, I apply the new values that were calculated in the job

    Code (CSharp):
    1.                 for (int i = 0; i < AllNonCombatShips.Count; i++)
    2.                 {
    3.                     if (AllNonCombatShips[i] != null && AllNonCombatShips[i].gameObject.activeSelf)
    4.                     {
    5.                         if (AllNonCombatShips[i].CurrentSpeed > AllNonCombatShips[i].MaxSpeed)
    6.                         {
    7.                         }
    8.                         AllNonCombatShips[i].CurrentSpeed = moveJobSpeedsArray[i].x;
    9.                         if (moveJobSpeedsArray[i].x > moveJobSpeedsArray[i].y)
    10.                         {
    11.                         }
    12.  
    13.                         AllNonCombatShips[i].WaitTime = moveJobWaitArray[i];
    14.                         if (moveJobStateArray[i] == (int)ShipStates.RequestingToDock || moveJobStateArray[i] == (int)ShipStates.Waiting)
    15.                         {
    16.                             landingPad.RequestForLanding(AllNonCombatShips[i]);
    17.                         }
    18.                         if (moveJobFinishArray[i])
    19.                         {
    20.                             ShipNextStep(i);
    21.                         }
    22.                     }
    23.                 }


    this is the part of the job that handles the speed

    Code (CSharp):
    1. if (distanceToTarget >= MAX_TARGET_DISTANCE) // target is far
    2. {
    3.     // Increase speed
    4.     Speeds[index] = new float4(curSpeed + (speedChange * DeltaTime), maxSpeed, speedChange, rotation);
    5.  
    6.     if (curSpeed + speedChange >= maxSpeed) // check if over max speed, if so, set to max speed
    7.     {
    8.         Speeds[index] = new float4(maxSpeed, maxSpeed, speedChange, rotation);
    9.     }
    10. }
    11. else if (state != (int)ShipStates.GoingBack) // target is close
    12. {
    13.     if (state == (int)ShipStates.Waiting)
    14.     {
    15.         float change = speedChange * DeltaTime;
    16.         Speeds[index] = new float4(curSpeed - change, maxSpeed, speedChange, rotation);
    17.         if (curSpeed <= 0) // check if over max speed, if so, set to max speed
    18.         {
    19.             Speeds[index] = new float4(0, maxSpeed, speedChange, rotation);
    20.         }
    21.     }
    22.     else
    23.     {
    24.         float change = speedChange * DeltaTime;
    25.         Speeds[index] = new float4(curSpeed - change, maxSpeed, speedChange, rotation);
    26.         if (curSpeed - change <= maxSpeed * CRUIZE_SPEED_PERCENTAGE) // check if over max speed, if so, set to max speed
    27.         {
    28.             Speeds[index] = new float4(maxSpeed * CRUIZE_SPEED_PERCENTAGE, maxSpeed, speedChange, rotation);
    29.         }
    30.     }
    31. }
    32.  
    The problem started happening once I added the speed calculations, and they seem to appear even before the job runs
    upload_2022-9-14_20-1-1.png

    This is the part of the code from the 1st code snippet and the breakpoint is triggered, meaning that after the job finishes the ships speed is fine (the if statements on the data allocation in the 3rd code snippet), but then when the job loads the data the current speed is higher then max speed. Could the burst indexing be faulty?
    I was thinking what if the job is running asynchronously and somehow gets triggered again right after it finishes, but then the input would be messed up right?
     

    Attached Files:

  2. Hafinator

    Hafinator

    Joined:
    Apr 19, 2017
    Posts:
    6
    Dumb me... Was removing items from collections used for looping but did not take into account the collections used for the job...
     
    sheredom likes this.