using load(path_to_sound) after preload(path_to_sound)

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

If I preload some sounds and I use later the command load() with a previously preloaded sound path, the sound is then loaded again or is the already preloaded automatically used?

:bust_in_silhouette: Reply From: yrtv

From Resources

When the engine loads a resource from disk, it only loads it once

:bust_in_silhouette: Reply From: Inces

load() and preload() return resource blueprints. Once you have reference to it You can use it everywhere in project, create multiple instances of it, and so on. There is no sense in loading/preloading more than once. The only difference between LOAD and PRELOAD is that PRELOAD loads resource with the compiller, that means even before your project is started. LOAD loads resource on the project runtime, in line of code it is called. In that matter PRELOAD is just better than LOAD in terms of speed, but sometimes You will have to dynamically create path to resource in runtime, and at that time only LOAD will help You.

Answering your question directly - your sound is unnecesarrily loaded twice, and sound that is currently used is the one You just called to load and used reference to.

OK, I got two answers

  1. yrtv: “When the engine loads a resource from disk, it only loads it once”
  2. Inces: “your sound is unnecesarrily loaded twice”
    What is right now?

Lebostein | 2021-10-06 08:59

Despite details both answers inform You, that there is no point in repetition of load/preload. ONCE You loaded OR preloaded resource blueprints are already prepared to be instanced, and You just need to keep reference from that 1st call, like this :

var music = preload("res/somepath")

and now it is written into your “music” variable. You are not supposed to call load or preload as void returning function

If You lose reference and instead call load or preload again - I am not sure if scene is being prepared again, but I believe it will have consequences on memory usage.

Inces | 2021-10-06 11:14

Resources are Reference counted. Resources are freed, then the counter reaches 0.
You can call load() or preload() on the same Resource as many times as you want in different scripts. Calling load() and preload() in the same script is a waste.

Both load() and preload() are aliases for ResourceLoader.load().
preload() is executed during script parsing

yrtv | 2021-10-07 03:55