Used to play/fade... sounds at certain circumstances or events in 2D or 3D simply via. code.
Nearly all games need music and soundeffects and this small and easily integrated Sfx -and MusicManager can help you play sounds in Godot for your game quick and easily.
Godot Sfx- and MusicManager implement the following methods consisting of a way to:
- Play music (see Play Music method)
- Fade in / out music (see Fade method)
- Stop all music (see Stop All method)
- Play soundeffects (see Play Sfx method)
- Play soundeffects at a random pitch level (see Play Pitched method)
For each method there is a description on how to call it and how to use it correctly for your game in the given section.
Required Software:
- Godot Ver. 2.0
The Sfx -and MusicManager themselves are version independent, as long as the AudioStreamPlayer
object already exists. Additionally the example project can be opened with Godot itself or the newest release can be downloaded and exectued to test the functionality.
If you prefer the first method, you can simply install the shown Godot version and after installing it you can download the project and open it in Godot. Then you can start the game with the play button to test the Sfx- and MusicManagers functionality.
To simply use the Sfx- and MusicManager in your own project without downloading the Godot project get the two files in the addons/AudioManager/ called MusicManager.gd
and SfxManager.gd
or alternatively get them from the newest release (may not include the newest changes) and save them in your own project. Then add them into the AutoLoad
property like shown in Adding new AutoLoad properties.
This documentation strives to explain how to start using the Sfx- and MusicManager in your project and explains how to call and how to use its publicly accesible methods correctly.
To use the Sfx- and MusicManager to start playing sounds outside of itself you need to reference it. As the Sfx- and MusicManager are a Singelton
this can be done easily when we save the script into our Autload properties like shown in Adding new AutoLoad properties. After this is done they can be simply called like this.
func _ready()
MusicManager.play(preload("music.ogg"))
SfxManager.play(preload("teleport.wav"))
To add a new AutoLoad
property, you simply need to click the add button and choose the Sfx -or MusicManager and given them the name you want to acces them with.
This section explains all public accesible methods, especially what they do, how to call them and when using them might be advantageous instead of other methods. We always assume both scripts have already been added as new AutoLoad
properties. If you haven't done that already see Adding new AutoLoad properties.
What it does: Starts playing the choosen sound.
How to call it:
Stream
is theAudioStreamPlayer
orname
we have given the sound we want to playOffset
is the point in the sound we want to start playing at
var stream := preload("music.ogg")
var offset := 0
MusicManager.play(stream, offset)
Alternatively you can call the methods with less paramters as some of them have default arguments.
var stream := preload("music.ogg")
MusicManager.play(stream)
When to use it: If you simply want to play a sound with no fading and additionaly want to ensure only one sound can be played at a time.
What it does: Start fading in and playing the choosen sound and fades out the previous sound.
How to call it:
Stream
is theAudioStreamPlayer
orname
we have given the sound we want to playPrevDuration
is the time we want to give the previous track until it's faded out completlyNextDuration
is the time we want to give the current track until it's faded in completlyNextDelay
is the time we want to wait until the next sound starts playingOffset
is the point in the sound we want to start playing at
var stream := preload("music.ogg")
var prev_duration := 1.0
var next_duration = null
var next_delay := 1.0
var offset := 0
MusicManager.fade(stream, prev_duration, next_duration, next_delay, offset)
Alternatively you can call the methods with less paramters as some of them have default arguments.
var stream := preload("music.ogg")
var prev_duration := 1.0
MusicManager.fade(stream, prev_duration)
When to use it: If you want to fade smoothly between souds, when one of them should end and the other should start.
What it does: Immediately stops all currently playing sounds.
How to call it:
MusicManager.stop_all()
When to use it: If you want to stop all sounds without fading.
What it does: Starts playing the choosen sound. (There can be multiple sounds running at the same time if the SfxManager is used)
How to call it:
Stream
is theAudioStreamPlayer
orname
we have given the sound we want to play
var stream := preload("teleport.wav")
SfxManager.play(stream)
When to use it: If you simply want to play a sound with no pitch shift and additionaly want to ensure multiple sounds can be played at a time.
What it does: Starts playing the choosen sound with a random pitch in the given range.
How to call it:
Stream
is theAudioStreamPlayer
orname
we have given the sound we want to playMinPitch
is the smallest pitch value the sound can haveMaxPitch
is the biggest pitch value the sound can have
var stream := preload("teleport.wav")
var min_pitch := 0.5
var max_pitch := 1.5
SfxManager.play_pitched(stream, min_pitch, max_pitch)
Alternatively you can call the methods with less paramters as some of them have default arguments.
var stream := preload("teleport.wav")
var min_pitch := 0.5
SfxManager.play_pitched(stream, min_pitch)
When to use it: If you play a sound a lot of times it can be advantageous to slightly change it's pitch to make it less repetitive.