HasBankLoaded causes NullReferenceException when RuntimeManager is not initialized

FMOD Unity integration version: 2.01.01

public static bool HasBanksLoaded
{
    get
    {
        return Instance.loadedBanks.Count > 1; 
    }
}

public static bool HasBankLoaded(string loadedBank)
{
    return (instance.loadedBanks.ContainsKey(loadedBank));
}

Note that HasBankLoaded refers to instance, rather than Instance, the former of which does not lazy-init the RuntimeManager.

To work around this mistake I used the following hack:

IEnumerator Start() {
  // Wait for all master banks to load. Ths is requred for web where banks
  // are loaded in asynchronously.
  //
  // https://www.fmod.com/resources/documentation-api?version=1.10&page=content/generated/engine_new_unity/html5_webgl.html
  var banks = FMODUnity.Settings.Instance.MasterBanks;

  // NOTE: Forcing initialization of FMOD which otherwise does not start due
  // to internally referencing `instance` instead of `Instance`.
  var _ = FMODUnity.RuntimeManager.HasBanksLoaded;

  while (!_isFmodSoundBankLoaded) {
    _isFmodSoundBankLoaded = banks.All(FMODUnity.RuntimeManager.HasBankLoaded);
    yield return null;
  }

This definitely seems like an error, and only showed up (in an otherwise working project) when I removed the FMOD listener component from the scene. Previously it called AddListener which internally called StudioSystem.setNumListeners which happened to cause the system to initialize before my first call to HasBankLoaded.

Thanks for the bug report, we’ll get that fixed up.

1 Like

Thanks for the quick response @mathew.

While I’m here I’d like to give some feedback on this API too…

Would you consider renaming HasBanksLoaded to HasAnyBankLoaded, and add HaveAllBanksLoaded which does something like I’m doing here?

bool HaveAllBanksLoaded() {
  var banks = Settings.Instance.MasterBanks;
  for (var i = 0; i < banks.Length; i++) {
    if (!HasBankLoaded(banks[i])) return false;
  }
  return true;
}

“HasBanksLoaded” is a confusing name, and the method behaviour is surprising (and has dubious usefulness).

Thanks for the feedback, we’ll look into clearing that up.
Any API changes will need to be on a major version though.

1 Like