+1 vote

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?

in Engine by (66 points)

2 Answers

0 votes

From Resources

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

by (889 points)
0 votes

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.

by (7,925 points)

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?

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.