Search Unity

Question Is there a way to use Debug.Log within a foreach loop in a SystemBase and not get errors?

Discussion in 'Entity Component System' started by BitPax, Dec 13, 2020.

  1. BitPax

    BitPax

    Joined:
    Oct 4, 2019
    Posts:
    89
    How are people checking real time data changes in a foreach loop in a SystemBase?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    I assume that is burst error.
    Just disable burst in menu, or remove burst attribute above the job.
    In case of Entities.Foreach, just add .WithouthBurst ()
     
    BitPax likes this.
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Lieene-Guo, BitPax and Lukas_Kastern like this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    It works, but it will throw error, if string is not fixed. Something along with line
     
    BitPax likes this.
  5. Zec_

    Zec_

    Joined:
    Feb 9, 2017
    Posts:
    148
    If you're looking to burst logs and are building the logged string from variables, you must use string interpolation or string.Format instead of concatenation. And you may only interpolate the types specified in the burst documentation (also linked above): https://docs.unity3d.com/Packages/c...html#partial-support-for-strings-and-debuglog

    I suggest reading the documentation, but here's a short example. You may not write this:
    int myVariable = 10;
    Debug.Log("MyVariable equals " + myVariable);


    You may however write this:
    int myVariable = 10;
    Debug.Log($"MyVariable equals {myVariable}");
     
    Last edited: Dec 13, 2020
    NagaChiang and BitPax like this.
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    It will not throw any errors if you do it properly (using string interpolation or format) as explained by Zec_
    Not doing it properly is an error and your job won't burst compile.
     
    BitPax likes this.
  7. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    Note, starting from Burst 1.4:

     
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    This was re-enabled 2 previews later in [1.4.0-preview.3] - 2020-08-06

     
  9. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    Okay sorry I missed that one in the change log. Both the 1.4 and 1.5 documentations are still listing it as a known issue, so I didn't even bother using it much.