

The var keyword indicates that this is a local variable, and this variable will be destroyed after the script is finished. Second - if you need to change global.volume_sfx to something else, you will need to change it only in one place - this reduces the probability of errors/typos/etc. First of them - local variables work a little faster than global variables. Of course, we can use global.volume_sfx instead, rather than create a new variable, but I prefer local variables for some reasons. Vol is a variable that will be used for temporary storage volume settings. This should be a value from 0 (no sound) to 1 (full volume). It is set by the user in the settings of the game. Global.volume_sfx is a global variable, which contains volume. var vol = global.volume_sfx // global.volume_sfx must contain volume (0.1) Play_sound(snd_explode) // play sound snd_explode (no loop) the function can be called with a single parameter or two: play_sound(snd_wind, true) // play sound snd_wind with loop Square brackets usually indicate a parameter that can be skipped.
Play sound gamemaker code#
This line defines the hint which will be showed in code editor. Function checks settings of volume and plays a sound according to them. To the function, must be passed the name of the sound you want to play and the option whether the sound is looped or not. Simply, but doesn't approach if it is necessary to make separate settings for music and effects.Īnother way (that I've suggested) - create your own function that will be used to play sounds. Other method - to change the global level of volume. In this case all sounds will be stored in memory that approaches not always (audio groups do not work for stream playing). This can be done in many ways.įor example, possible to create different audio groups - one for music, one for effects and set the volume to a whole group. Any playing of a sound in game shall consider this setup. Let's imagine that there is a menu where the user can adjust the sound volume. Can you explain the script and obj_sound_enable for each row? Sorry, but I still don't understand your code. See small example with slider and sound on/off button. Or if you have slider in sound preferences then you can set any volume (for example, 0.5 for 50% of maximum volume).

So, for enable/disable sounds you need change this variable from 0 to 1 or from 1 to 0 (like global.volume_sfx = !global.volume_sfx). Variable global.volume_sfx must contain value from 0 (no sound) to 1 (full volume). This code gets volume from settings ( var vol = global.volume_sfx) and plays all sounds with this volume. Then call it when you need play any sound. When I need stop all sounds, I do: audio_stop_all() Ĭreate new script with name play_sound with first code. Var s = audio_play_sound(snd, 50, loop) // Start soundĪudio_sound_gain(s, vol, 0) // Set volume for this sound Var loop = false // need loop sound or not Var snd = argument // name of sound which need play, passed to the script

If vol = 0 exit // if volume is 0 then not need play, exit Var vol = global.volume_sfx // global.volume_sfx must contain volume (0.1) Usually I do something like this: /// play_sound(sound, )
