melonJS
    Preparing search index...

    Function unload

    • Release a bundle, or free a single asset: a string is a bundle id, an object is an asset descriptor. So loader.unload("map1") releases the bundle "map1", and loader.unload({ name: "map1", type: "tmx" }) frees that one map, whichever bundles list it.

      With a bundle id (see loader.setBundle), the bundle lets go of each of its assets, and every one that nothing else still holds is freed: an asset another loaded bundle lists stays, and so does one loaded the old way (by preload / load with a descriptor), which pins it. Freeing a map or a glTF scene this way also unregisters its level, so level.load of it throws "not found" until the bundle is loaded again. Unload the bundle of the level being left once the next level is in: unloading the bundle of the level being played leaves the world as it is, but with no current level, so level.reload() has nothing to reload. Allowed while the bundle is still loading: its assets are then freed as they arrive.

      With an asset descriptor (only name and type are read), the asset is freed at once, whoever holds it: a bundle listing it reports loader.isLoaded false afterwards, and loading that bundle again fetches it again.

      Either way, freeing an image also frees the texture the renderer uploaded from it, a shader its GPU program, a font its place in document.fonts. A Sprite Fusion map ("spritefusion") frees the spritesheet loaded with it, and an LDtk project the images loaded with it, unless something else still holds them. A tile map is freed under any of its type names ("tmx", "tilemap", "spritefusion"): they are one cache. A shader handed out by loader.getShader is not tracked once handed out, so an effect that outlives a level (a HUD's, a camera's) belongs in a bundle that outlives it too, or it is freed under whatever still uses it.

      Parameters

      • asset: string | Asset

        a bundle id, or the descriptor of the asset to free

      Returns boolean

      with a bundle id, false when the bundle was not loaded; with a descriptor, false when the asset was not in memory

      synchronously, on an unknown bundle id, or a descriptor whose type the loader keeps nothing for (a type registered with loader.setParser without an unload: the game keeps those assets itself)

      // level 1 is behind the player for good: free what only it uses
      await level.load("level2", { async: true });
      loader.unload("level1");
      // free one asset, whatever loaded it
      loader.unload({ name: "avatar", type: "image" });