Is there a way to stop background music lagging when switching collections (aka menu scenes)

Dear Defold community,

This is my first post! Now that we got that out of the way, I had a question/problem regarding audio stuttering while loading another scene.

Currently I have a menu, that consists of a splash going into an autosave notice that goes into the real menu screen. Everytime I switch the scene the music cuts out for a second while it’s switching. Is there any way around this problem? I’d like my background music to continue while the menu seamlessly switches ‘scenes’.

Thanks for the help!

1 Like

This was posted by one of the Defold team @Andreas_Tadic :

Hi Gregor,

This is expected behaviour.

When factory dynamic loading is disabled, the resources contained (in the collection or go) are loaded with the parent collection.

When factory dynamic loading is enabled, you’re expected to call factory.load/unload as needed meaning you control when the resources are loaded and unloaded in memory. Load/unload are asynchronous calls.

If you call factory.create on a factory with dynamic loading enabled without having called load (not having the resources in memory) it will create the resources needed in the create call synchronously which is why you get the stutter.

If you want to go with dynamic loading, you should load like this in your init function:

self.loaded = false
collectionfactory.load("#loaded_factory", function(self, url, result) self.loaded = true end)

Then check the self.loaded flag before creating the factory. That will remove the stuttering since the factory now has been asynchronously loaded.

Note that all this applies to both collectionfactories and factories.

5 Likes