Search Unity

Exception when loading localized string from LoadAssetsAsync callback. (Bug in Unity?)

Discussion in 'Addressables' started by Chris_Entropy, Apr 13, 2022.

  1. Chris_Entropy

    Chris_Entropy

    Joined:
    Apr 11, 2011
    Posts:
    202
    I have the following problem: I want to load several Addressable assets with a label. So I use
    Addressables.LoadAssetsAsync<> with a list of asset label references and a callback method. I also add a callback to the handle like so:
    Code (CSharp):
    1.            
    2. AssetLabelReference label = new AssetLabelReference
    3. {
    4.     labelString = Globals.ChapterLabel
    5. };
    6. var handle = Addressables.LoadAssetsAsync(label, onLoadedChapter);
    7. if (handle.IsDone)
    8. {
    9.       onLoadedAllChapters?.Invoke(handle.Result, true);
    10. }
    11. else
    12. {
    13.       handle.Completed += operationHandle => HandleAddressableLoaded(operationHandle, onLoadedAllChapters);
    14. }
    15.  
    The problem arises, when the callback "onLoadedAllChapters" is triggered. I then try to iterate over all the loaded objects and get a Localized string with an ID stored in them. The code for loading the string is the following:

    Code (CSharp):
    1.        
    2.         public static string GetLocalizedString(string tableReference, string key)
    3.         {
    4.             if (!TryGetStringTable(tableReference, out var table))
    5.             {
    6.                 return "#ERROR#";
    7.             }
    8.  
    9.             var entry = table.GetEntry(key);
    10.             if (entry == null)
    11.             {
    12.                 Utils.LogError("There was no entry in table: " + tableReference + ", with key: " + key);
    13.                 return "#ERROR#";
    14.             }
    15.             return entry.GetLocalizedString();
    16.         }
    17.  
    18.         private static bool TryGetStringTable(string tableReference, out StringTable table)
    19.         {
    20.             table = LocalizationSettings.StringDatabase.GetTable(tableReference, LocalizationSettings.SelectedLocale);
    21.             if (table == null)
    22.             {
    23.                 Utils.LogError("There was no localization table: " + tableReference);
    24.                 return false;
    25.             }
    26.  
    27.             return true;
    28.         }
    29.  
    As I work with several localization tables in my project, one for each of the loaded "Chapters", I first look up the Localization Table for the corresponding Chapter and then get the string from it.
    When I execute the code in this order, I get the following exception:
    Exception: Reentering the Update method is not allowed

    It seems to work at first, if I react to the onLoadedChapter callback, and load the strings one by one, as this callback is executed each time a single asset is found. But when I load another scene, and then come back to this scene, I am again confronted with the Exception mentioned above.

    This all goes away, if I instead run the loading in a coroutine and use handle to yield until everything is loaded. But I might encounter a situation, where I can't rely on coroutines. Any suggestions what I could do? Is this a bug, and should I report it as such?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,299
    This happens when you are inside of an Addressables completion event and addressables tries to invoke another completion event, usually caused by loading something inside of the callback with WaitForCompletion.

    From looking at your code I suspect it's coming from GetTable which uses WaitForCompletion,. calling this in a callback can cause this. If you call WaitForCompletion on your initial part instead of using the Completed event then the issue should fix itself. Alternatively use coroutines or defer the completition action so that it happens outside of completed.
     
    Last edited: Apr 13, 2022