Made menu music keep playing across all scenes but game
this was way more annoying than it should have been. Wanna know why? Godot.
This commit is contained in:
1 parent
ac55503bab
commit
cbefe77af7
4 files changed
+59
-20
No files matched your search
@@ -0,0 +1,43 @@
|
||||
using Godot;
|
||||
|
||||
public partial class MusicManager : Node {
|
||||
private AudioStreamPlayer _player;
|
||||
private Node _lastScene;
|
||||
private AudioStream _menuMusic;
|
||||
|
||||
public override void _Ready() {
|
||||
_player = new AudioStreamPlayer();
|
||||
AddChild(_player);
|
||||
|
||||
_menuMusic = GD.Load<AudioStream>("res://Audio/Music/Menu.mp3");
|
||||
|
||||
_player.Stream = _menuMusic;
|
||||
_player.Play();
|
||||
|
||||
_lastScene = GetTree().CurrentScene;
|
||||
}
|
||||
|
||||
public override void _Process(double delta) {
|
||||
Node currentScene = GetTree().CurrentScene;
|
||||
|
||||
if (currentScene != _lastScene) {
|
||||
OnSceneChanged(currentScene);
|
||||
_lastScene = currentScene;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSceneChanged(Node newScene) {
|
||||
if (_player == null) return;
|
||||
if (newScene == null) return;
|
||||
|
||||
if (newScene.Name == "Game") {
|
||||
if (_player.Playing)
|
||||
_player.Stop();
|
||||
} else {
|
||||
if (_player.Stream != _menuMusic || !_player.Playing) {
|
||||
_player.Stream = _menuMusic;
|
||||
_player.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user