Search Unity

Bug Occasionally, NetCodeConfig fails to load

Discussion in 'NetCode for ECS' started by Richay, Jan 11, 2024.

  1. Richay

    Richay

    Joined:
    Aug 5, 2013
    Posts:
    122
    In the editor, every so often the NetCodeConfig scriptable object fails to load. I've noticed it seems to occur more often after a fresh boot of Unity, but sometimes it occurs at other times too. Stopping and Playing again usually fixes it. When it fails to load, the singleton ClientServerTickRate is not present in neither the server nor the client world.

    I'm not sure of the best place to store it, but I keep the scriptable in Resources.
     
  2. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    315
    Thanks for the report, Richay. Very odd.
    1. Is it properly assigned in the ProjectSettings tab even when it errors?
    2. Can you also log the value of NetcodeConfig.Global on entering playmode, please? And LMK if that's null.
    3. Are you using any AssetDatabase options that may impact this? Cache server, Source Control plugins etc?
    4. Any error/warning logs?
    I'll do some testing on 1.2.0-pre.4.
     
  3. Richay

    Richay

    Joined:
    Aug 5, 2013
    Posts:
    122
    I just added logging, and the very first time after a fresh boot of Unity, NetcodeConfig.Global is null. On a second play, all is good. The scriptable is still in the preload list in the player settings, and is linked in the netcode tab. We have no special AssetDatabase shenanigans going on afaik. Nothing in the console.
     
    Last edited: Jan 11, 2024
    NikiWalker likes this.
  4. Richay

    Richay

    Joined:
    Aug 5, 2013
    Posts:
    122
    I'm on dots 1.2.0-pre.6, Unity 2022.3.17f1.
     
  5. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    It is probably because the first time you boot fresh (arguing eh) it is a classic egg/chicken problem. Something is not initialised in the right order and the config is not loading as expected.
     
  6. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,129
    Actually I tried to use NetcodeConfig.Global at authoring baker but it's null when baking. Is that currently the known limitation? I would like official to support it since I dun want to put drag & drop this same NetcodeConfig to insane amount of authoring monobehavior.
     
  7. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    315
    I'll take a look at the bug on Monday, but optimise I'm interested in why you're adding the NetcodeConfig to any authoring components (nevermind multiple authorings).

    If it's set globally it auto applies.
     
    optimise likes this.
  8. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    315
    I've reproed this with 2022.3.13f1 (haven't tried your editor version yet), thanks for the report! And apologies! It seems like Preloaded assets are not preloaded in the Editor (lol), which means Resources.FindObjectsOfTypeAll will fail. It's an assumption I made after seeing it work. I'll check out how other assets (like Graphics / Scriptable Rendering Pipeline) get around this.

    Reproed by clearing local Library & Temp directories, fresh open of Unity, with the ProjectSettings window closed. Code:
    Code (CSharp):
    1.  
    2.         [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
    3. // Inside NetCodeConfig.cs...
    4.         private static void RuntimeTryFindSettings()
    5.         {
    6.             var configs = Resources.FindObjectsOfTypeAll<NetCodeConfig>();
    7.             ...
    8.             Global = configs.Length > 0 ? configs[0] : null;
    9.             UnityEngine.Debug.LogError($"TEST RESULT: Global={Global} | {configs.Length}: {configs.Aggregate("", (c,n) => $"{c}\n{n}")}");
    10.          }
    My asset was in
    [project]/Assets/[here]
    , so the Resources folder doesn't seem to have an impact. Fix incoming...
     
    Last edited: Jan 15, 2024
    PolarTron, Richay and optimise like this.
  9. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,129
    After the fix, can I expect using NetcodeConfig.Global at authoring and it will work properly at authoring baking time?
     
  10. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    315
    This was recently fixed, but didn't land in time for 1.2. Aiming for 1.3 instead. Thanks again for the report!

    I'm not sure what you're trying to do, optimize. What do you mean by "expect to use NetCodeConfig.Global at authoring time"? NetCodeConfig.Global is a runtime API.
     
    horeaper and Richay like this.
  11. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,129
    I would like to use it at authoring time too but from wat u say it's designed for runtime only. Then I will need to go for regular authoring way instead of just call NetCodeConfig.Global API to get the required NetCodeConfig data to bake data.
     
  12. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    894
    Yeah, I understood what you mean optimise. Indeed there should be a clear distinguish in between the authoring (the scriptable) and the actually config data (entity only) and the application of that data too (that is a third step).
     
  13. Richay

    Richay

    Joined:
    Aug 5, 2013
    Posts:
    122
    @NikiWalker seeing as the fix wasn't in pre.12, and I presume the next release won't be for another month or two, is the fix straightforward to implement? It's a regular irritance here, but I don't want to restructure the net config initialization only to revert it again when the fix lands.
     
  14. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    315
    @Richay The fix is a little involved (as we moved the NetCodeConfig out of PreloadedAssets, except in builds), but in the editor, a short term quick solution is adding this code into an Editor script (like NetCodeConfigEditor):
    Code (CSharp):
    1.         [InitializeOnLoadMethod]
    2.         private static void InitializeNetCodeConfigEditorBugFix()
    3.         {
    4.             PlayerSettings.GetPreloadedAssets().OfType<NetCodeConfig>();
    5.         }
    Essentially you just have to ensure the ScriptableAsset is initialized in the editor (i.e. have it's Awake called).

    EDIT: And apologies that the fix didn't land, it was too big of a change for 1.2. We'll ensure it's in the next minor release.
     
    Last edited: Mar 6, 2024
    horeaper likes this.
  15. Richay

    Richay

    Joined:
    Aug 5, 2013
    Posts:
    122
    That works perfectly, many thanks. I wish I had asked a month ago!
     
    NikiWalker likes this.