How to properly use ResourcePreloader?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By leiget

Does anyone know how to use the ResourcePreloader node properly? I want to preload all textures and whatnot in my scene, but I’m not sure if I’m using it correctly. I can’t find any tutorials or info on it, really.

  1. Do I just add the resources to the preloader node?
  2. Can I just add a whole scene and it will take care of the rest, or do I have to put in every single texture, sound, ext. for it to be preloaded?
:bust_in_silhouette: Reply From: aozasori
  1. Just add the resources via the ResourcePreloader window.

  2. I have been utilizing ResourcePreloader for reusable ui widgets, which are saved as .tscn files. As far as i can tell, everything a scene requires is preloaded. (In Video Mem tab of the debugger, unique textures associated with the preloaded scenes are present before they are used.)

When it comes time to instance a preloaded scene:
var widget = get_resource(widget_name).instance()

If you need to add a scene at runtime you can add it with the ResourcePreloader’s add_resource function.
Usage example:

func load_and_instance(path):
    var scene
    var name = path.get_file().basename()

    if preloader.has_resource(name):
        scene = preloader.get_resource(name)
        scene = load(path)
        preloader.add_resource(name, scene)

    return scene.instance()

So do you have to instance a scene like you’ve shown, or can I instance a scene to my current scene, then add that instanced scene to ResourcePreloader?

leiget | 2018-02-11 17:23

You can’t add instanced scenes. Updating answer with additional information.

aozasori | 2018-02-12 20:23