• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

How to play sound more than 1 Time without Leaks?

Level 3
Joined
Dec 14, 2023
Messages
35
Hi Hive,

i have a sound i wish to use more than once and plan to use a lot of sounds for atmosphere. how can i play a sound without the risk of to many leaks?
Only know to destroy a sound to play it once or to play a sound everytime its needed but without understanding of leakproblems that could occur.
Could someone give me an example trigger how to properly set it up in GUI via Custom Script?
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,567
If you want a looping sound that plays forever then you don't have to worry about leaks. Simply attach the sound to an object and make sure that looping is enabled in the Sound Editor. Under the Sound category you'll see actions for attaching sounds. On second thought, you might not even need to attach it to anything, but you may need to use a 3D sound that to avoid the whole sound conflict issue.

Regarding sound conflicts, here's a system that will fix all of these problems for you (see my last post):
Here's an example of playing a Looping sound using this system:
  • Test Looping Sound
    • Events
      • Player - Player 1 (Red) types a chat message containing test as An exact match
    • Conditions
    • Actions
      • Set VariableSet SM__Sound = Abilities/Spells/Human/Banish/BanishLoop1
      • Set VariableSet SM__Sound_Is_Looping = True
      • Custom script: call SoundManagerPlay()
The demo map and it's code is also attached below:
vJASS:
library SoundManager initializer Init
    // SM__Sound = string
    // SM__Sound_Player = player
    // SM__Sound_Unit = unit
    // SM__Sound_Point = point
    // SM__Sound_Volume = integer
    // SM__Sound_Pitch = real
    // SM__Sound_Is_Looping = boolean
    // SM__Sound_Cutoff = real
    // SM__Sound_Last_Played = sound
    private function SoundPlayGeneral takes nothing returns nothing
        local sound snd = CreateSound(udg_SM__Sound, udg_SM__Sound_Is_Looping, false, false, 10, 10, "DefaultEAXON")
        call SetSoundChannel(snd, 0)
        call SetSoundVolume(snd, udg_SM__Sound_Volume)
        if udg_SM__Sound_Player != null then
            if GetLocalPlayer() != udg_SM__Sound_Player then
                call SetSoundVolume(snd, 0)
            endif
        endif
        call SetSoundPitch(snd, udg_SM__Sound_Pitch)
        call StartSound(snd)
        if udg_SM__Sound_Is_Looping == false  then
            call KillSoundWhenDone(snd)
        endif
        set udg_SM__Sound_Last_Played = snd
        set snd = null
    endfunction
    private function SoundPlay3DUnit takes nothing returns nothing
        local sound snd = CreateSound(udg_SM__Sound, udg_SM__Sound_Is_Looping, true, true, 10, 10, "DefaultEAXON")
        call SetSoundChannel(snd, 0)
        call SetSoundVolume(snd, udg_SM__Sound_Volume)
        if udg_SM__Sound_Player != null then
            if GetLocalPlayer() != udg_SM__Sound_Player then
                call SetSoundVolume(snd, 0)
            endif
        endif
        call SetSoundPitch(snd, udg_SM__Sound_Pitch)
        call SetSoundDistances(snd, 600., 10000.)
        call SetSoundDistanceCutoff(snd, udg_SM__Sound_Cutoff)
        call SetSoundConeAngles(snd, 0., 0., 127)
        call SetSoundConeOrientation(snd, 0., 0., 0.)
        call AttachSoundToUnit(snd, udg_SM__Sound_Unit)
        call StartSound(snd)
        if udg_SM__Sound_Is_Looping == false  then
            call KillSoundWhenDone(snd)
        endif
        set udg_SM__Sound_Last_Played = snd
        set snd = null
    endfunction
    private function SoundPlay3DLocation takes nothing returns nothing
        local sound snd = CreateSound(udg_SM__Sound, udg_SM__Sound_Is_Looping, true, true, 10, 10, "DefaultEAXON")
        call SetSoundChannel(snd, 0)
        call SetSoundVolume(snd, udg_SM__Sound_Volume)
        if udg_SM__Sound_Player != null then
            if GetLocalPlayer() != udg_SM__Sound_Player then
                call SetSoundVolume(snd, 0)
            endif
        endif
        call SetSoundPitch(snd, udg_SM__Sound_Pitch)
        call SetSoundDistances(snd, 600., 10000.)
        call SetSoundDistanceCutoff(snd, udg_SM__Sound_Cutoff)
        call SetSoundConeAngles(snd, 0., 0., 127)
        call SetSoundConeOrientation(snd, 0., 0., 0.)
        call SetSoundPosition(snd, GetLocationX(udg_SM__Sound_Point), GetLocationY(udg_SM__Sound_Point), GetLocationZ(udg_SM__Sound_Point))
        call StartSound(snd)
        if udg_SM__Sound_Is_Looping == false  then
            call KillSoundWhenDone(snd)
        endif
        call RemoveLocation(udg_SM__Sound_Point)
        set udg_SM__Sound_Last_Played = snd
        set snd = null
    endfunction
    private function Reset takes nothing returns nothing
        set udg_SM__Sound_Player = null
        set udg_SM__Sound_Unit = null
        set udg_SM__Sound_Point = null
        set udg_SM__Sound_Is_Looping = false
        set udg_SM__Sound_Cutoff = 3000.0
        set udg_SM__Sound_Pitch = 1.0
        set udg_SM__Sound_Volume = 127
    endfunction
    function SoundManagerPlay takes nothing returns nothing
        if udg_SM__Sound_Unit == null and udg_SM__Sound_Point == null then
            call SoundPlayGeneral()
        elseif udg_SM__Sound_Unit == null and udg_SM__Sound_Point != null then
            call SoundPlay3DLocation()
        elseif udg_SM__Sound_Unit != null and udg_SM__Sound_Point == null then
            call SoundPlay3DUnit()
        else
            call SoundPlayGeneral()
        endif
        call Reset()
    endfunction
    private function Init takes nothing returns nothing
        call Reset()
    endfunction
endlibrary
1713464220667.png

Note: The variable names have two underscores __ after SM. You must use these EXACT variable names!
 

Attachments

  • SoundManager 1.w3m
    21.2 KB · Views: 4
Last edited:
Top