• 🏆 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!

[JASS] Sound code not working

Status
Not open for further replies.
Level 8
Joined
Sep 15, 2006
Messages
426
Trying to get something that plays a sound whenever a unit attacks, regardless if the sound is still playing, and it's not working. Can anyone see what's wrong here?

JASS:
function Trig_Small_Sound_Func002Func001C takes nothing returns boolean
    if ( ( GetUnitTypeId(GetTriggerUnit()) == 'H001' ) ) then
        return true
    endif
    if ( ( GetUnitTypeId(GetTriggerUnit()) == 'H000' ) ) then
        return true
    endif
    if ( ( GetUnitTypeId(GetTriggerUnit()) == 'H003' ) ) then
        return true
    endif
    if ( ( GetUnitTypeId(GetTriggerUnit()) == 'H004' ) ) then
        return true
    endif
    if ( ( GetUnitTypeId(GetTriggerUnit()) == 'H002' ) ) then
        return true
    endif
    if ( ( GetUnitTypeId(GetTriggerUnit()) == 'H005' ) ) then
        return true
    endif
    return false
endfunction

function Trig_Small_Sound_Func002C takes nothing returns boolean
    if ( not Trig_Small_Sound_Func002Func001C() ) then
        return false
    endif
    return true
endfunction

function Trig_Small_Sound_Conditions takes nothing returns boolean
    if ( not Trig_Small_Sound_Func002C() ) then
        return false
    endif
    return true
endfunction

function Trig_Small_Sound_Actions takes nothing returns nothing
    local sound s=CreateSound("usp_unsil-1.wav",false,false,false,10,10,"")  
    call SetSoundPitch(s,1.00)  
    call SetSoundVolume(s,100)  
    call SetSoundDistances(s,6000,6000) 
    call SetSoundDistanceCutoff(s,1000)  
    call AttachSoundToUnit(s,GetTriggerUnit())  
    call StartSound(s)  
    call KillSoundWhenDone(s)  
endfunction

//===========================================================================
function InitTrig_Small_Sound takes nothing returns nothing
    set gg_trg_Small_Sound = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Small_Sound, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_Small_Sound, Condition( function Trig_Small_Sound_Conditions ) )
    call TriggerAddAction( gg_trg_Small_Sound, function Trig_Small_Sound_Actions )
endfunction
 
Level 11
Joined
Feb 18, 2004
Messages
394
I have no idea what your sound problem is, but for gods sakes, post CLEAN script if you're going to post script! Lets clean this up...:

JASS:
function Trig_Small_Sound_Conditions takes nothing returns boolean
    if ( not Trig_Small_Sound_Func002C() ) then
        return false
    endif
    return true
endfunction

No. the return statement takes a value of the type the function returns. and expression is computed down to a value, so this is valid (and better):
JASS:
function Trig_Small_Sound_Conditions takes nothing returns boolean
    return Trig_Small_Sound_Func002C()
endfunction

But thats still wrong, because Trig_Small_Sound_Func002C() contains the following:
JASS:
function Trig_Small_Sound_Func002C takes nothing returns boolean
    if ( not Trig_Small_Sound_Func002Func001C() ) then
        return false
    endif
    return true
endfunction

That function should not even exist. I'm not often accusing people of smoking crack, but i have to make an exception here. Removing it and making the condition func call Trig_Small_Sound_Func002Func001C instead yeilds:
JASS:
function Trig_Small_Sound_Conditions takes nothing returns boolean
    return Trig_Small_Sound_Func001C()
endfunction

Now to clean up Trig_Small_Sound_Func001C():
JASS:
function Trig_Small_Sound_Func002Func001C takes nothing returns boolean
    // you don't need all those brackets...
    if GetUnitTypeId(GetTriggerUnit()) == 'H001' then
        return true
    // elseif acts esentally like nesting another if-then-else in an else statement. if may be followed by any number (including 0) of elseif statements, followed by 1 or 0 else statements
    elseif GetUnitTypeId(GetTriggerUnit()) == 'H000' then
        return true
    elseif GetUnitTypeId(GetTriggerUnit()) == 'H003' then
        return true
    elseif GetUnitTypeId(GetTriggerUnit()) == 'H004' then
        return true
    elseif GetUnitTypeId(GetTriggerUnit()) == 'H002' then
        return true
    elseif GetUnitTypeId(GetTriggerUnit()) == 'H005' then
        return true
    endif
    return false
endfunction

Now, that whole function can be expressed on a single like using the "or" statement. But that would be one long line, so using a single local variable, we could:
JASS:
function Trig_Small_Sound_Func002Func001C takes nothing returns boolean
    local integer i = GetUnitTypeId(GetTriggerUnit())
    return i == 'H000' or i == 'H001' or i == 'H002' or i == 'H003' or i == 'H004' or i == 'H005'
endfunction

But wait, our Trig_Small_Sound_Conditions just returns the value this function returns... so remove this function, and put its contents in Trig_Small_Sound_Conditions, resulting in the totality of:
JASS:
function Trig_Small_Sound_Conditions takes nothing returns boolean
    local integer i = GetUnitTypeId(GetTriggerUnit())
    return i == 'H000' or i == 'H001' or i == 'H002' or i == 'H003' or i == 'H004' or i == 'H005'
endfunction

function Trig_Small_Sound_Actions takes nothing returns nothing
    local sound s=CreateSound("usp_unsil-1.wav",false,false,false,10,10,"")  
    call SetSoundPitch(s,1.00)  
    call SetSoundVolume(s,100)  
    call SetSoundDistances(s,6000,6000) 
    call SetSoundDistanceCutoff(s,1000)  
    call AttachSoundToUnit(s,GetTriggerUnit())  
    call StartSound(s)  
    call KillSoundWhenDone(s)  
endfunction

//===========================================================================
function InitTrig_Small_Sound takes nothing returns nothing
    set gg_trg_Small_Sound = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Small_Sound, EVENT_PLAYER_UNIT_ATTACKED )
    call TriggerAddCondition( gg_trg_Small_Sound, Condition( function Trig_Small_Sound_Conditions ) )
    call TriggerAddAction( gg_trg_Small_Sound, function Trig_Small_Sound_Actions )
endfunction
 
Status
Not open for further replies.
Top