+1 vote

I find this very strange as to everything works fine, except when I add a scene in AutoLoad. My _ready is only called twice when I put my TitleScreen scene in AutoLoad, and somehow when I change scene everything goes back to normal.

in Engine by (34 points)

Why you put TitleScreen in AutoLoad?
If you want it to start at the beginning of the game, you should put it as the main scene, not as an autoload.

Are you sure you're not instancing it twice? (Having it autoload and it being on your scene hierarchy, for example)

1-I put it in autoload because after searching a lot in godot forums and other medias, it seens the only way to put a music that doesn't reset between scenes. Even with my workaround the music doesn't goes smoothly when switching scenes.

2-I don't really get what instancing twice means. If you could make a different explanation it would be really helpful, thanks.

Instancing- It's a "copy" per say of your scene/script, by autoloading it and having your title screen you have two copies of your script running at the same time, don't autoload the script, this could have so many errors.

2 Answers

0 votes
Best answer

[Closing this thread]

by (34 points)
0 votes

To fix your issue there is a few functions you can use with the audio player to resume the music exactly where you want it to.

First, you need to get where your music will stop at when you transition scenes

var music_time = get_position_playback() #returns the amount (in seconds) of where the audiostream is at

You can then use another function to resume the play of the music at a specified position.

$AudioStreamPlayer.play(music_time) # will resume the music at the value of music_time

And I repeat from my earlier comment, don't autoload the script for your titlescreen, create a new script, autoload that and hold the music_time variable in that script so you could call on it between scenes.
For example: You have a script named Global.gd autoloaded and there is a single statement in the script var music_time, to call on it you would write this statement before you transition scenes:

Global.music_time = get_position_playback()

Then in your other script for the scene you transition to you would write:

if Global.music_time != null: # check to make sure you don't get an error
     $AudioStreamPlayer.play(Global.music_time)
else:
     $AudioStreamPlayer.play() #starts music from beginning
by (96 points)

As I already said, that was exactly my workaround. It kinda works, but as I said, it isn't smoothly. Every time I switch scenes, the delay needed to completely switch them makes the music stutter, or basically stop till the scenes fully loads. It isn't a very big delay, but it's extremely annoying, and sounds horrible.

Would it be sensible to autoload an AudioStreamPlayer script and have music be handled in a more global context? (make it a singleton basically) Or would it be a bad practice?

Thank you very much, Lazarwolfe! Your suggestion worked out perfectly, and it's kinda funny how it's so simple yet most people like me didn't realize it at all. Also thank you Moldor2 for explaining what instancing is.

Be careful though, I don't think that autoloading many things is a good practice, it might be a good idea to look into a solid workaround, since the singleton pattern (the fancy name for autoloading) is a bit of an object oriented programming malpractice, as far as I know.

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 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 webmaster@godotengine.org with your username.