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

Question DynamicBuffer in foreach loop

Discussion in 'Entity Component System' started by unity_jgu_81G9pIjQqQ, Mar 6, 2023.

  1. unity_jgu_81G9pIjQqQ

    unity_jgu_81G9pIjQqQ

    Joined:
    Sep 25, 2019
    Posts:
    3
    Greetings!

    I'm trying to modify dynamic buffer in the foreach loop, but getting this error:
    "Foreach iteration variable 'fmodFloatParameters' is immutable. Cannot modify struct member when accessed struct is not classified as a variable".

    But in ECS Manual it is said: "DynamicBuffer<T> type parameters in SystemAPI.Query<T> are read-write access by default.".
    Why I cannot modify it in my case? Help, please :(:(:(!

    Code (CSharp):
    1.  
    2. using System;
    3. using Unity.Entities;
    4. using Unity.Mathematics;
    5. using Weapons.Audio.FMOD.Authoring;
    6. using Weapons.Audio.FMOD.Components;
    7.  
    8. namespace Weapons.Audio.FMOD.Systems
    9. {
    10.     [UpdateInGroup(typeof(PresentationSystemGroup))]
    11.     public class FmodUpdateFloatParametersSystem: ISystem
    12.     {
    13.         public void OnUpdate(ref SystemState state)
    14.         {
    15.             foreach (var (fmodFloatParameters, floatParameters, fmodSound)
    16.                    in SystemAPI.Query<DynamicBuffer<FMODFloatParameter>, DynamicBuffer<FloatParameter>, RefRO<FMODSound>>()
    17.                        .WithChangeFilter<FloatParameter>())
    18.              {
    19.                 int length = math.min(fmodFloatParameters.Length, floatParameters.Length);
    20.                
    21.                 for (int i = 0; i < length; i++)
    22.                 {
    23.                     var floatParameter = floatParameters[i];
    24.                     FMODFloatParameter fmodFloatParameter = fmodFloatParameters[i];
    25.                    
    26.                     if (Math.Abs(fmodFloatParameter.CurrentValue - floatParameter.Value) < 0.0001f) continue;
    27.                     fmodSound.ValueRO.Event.setParameterByID(fmodFloatParameter.ParameterID, floatParameter.Value);
    28.  
    29.                     fmodFloatParameter.CurrentValue = floatParameter.Value;
    30.                     fmodFloatParameters[i] = fmodFloatParameter;
    31.                 }
    32.                
    33.              }
    34.         }
    35.     }
    36. }
    37.  
     
  2. unity_jgu_81G9pIjQqQ

    unity_jgu_81G9pIjQqQ

    Joined:
    Sep 25, 2019
    Posts:
    3
  3. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    825
    if you get your buffer as a native array (AsNativeArray()) you should be able to write to it.
     
  4. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    10
    Hi @unity_jgu_81G9pIjQqQ, thanks for raising the issue! I'm able to reproduce the error, and I'm currently investigating this and working on a fix.
     
    apkdev likes this.
  5. MiaoYuGoh

    MiaoYuGoh

    Unity Technologies

    Joined:
    Nov 12, 2019
    Posts:
    10
    Hi @unity_jgu_81G9pIjQqQ, the error you encountered is a C# compiler error. To fix the issue, you need to first assign the dynamic buffer to another variable before modifying it, e.g.

    var newFmodFloatParameters = fmodFloatParameters;
    newFmodFloatParameters = blah;


    Unfortunately my attempts to suppress the error by automatically generating new variable assignments are unsuccessful.
     
    Last edited: Mar 13, 2023
    apkdev likes this.