Check if sound is playing to toggle it?

Discussion in 'Mapping Questions & Discussion' started by arthurgps2, Feb 28, 2018.

  1. arthurgps2

    arthurgps2 L1: Registered

    Messages:
    32
    Positive Ratings:
    0
    Hello everyone. So I was building my CTF map, which has an ambient_generic entity to play an alarm sound when the intel gets taken (also thanks to Startacker for helping me in the alarm file name issue). It runs perfectally well when you take the intel, the sound starts playing and looping, but it was supposed to stop when the intel returns to the base, which doesn't happen. I think I need to do something to check if the alarm is playing to stop or play it again. In Lua it would look like this:
    Code:
    function intel.OnPickup() --Same as the OnPickup output
        if not sound.Playing then --"Playing" is a variable I created that sets to true when the sound is playing and false when it is not
            sound.ToggleSound()
        end
    end
    
    function intel.OnReturn()
        if sound.Playing then
            sound.ToggleSound() --This is supposed to untoggle the sound. Please correct me if I'm wrong
        end
    end
    Any help would be apreciated
     
  2. Da Spud Lord

    aa Da Spud Lord L0: Crappy Member

    Messages:
    831
    Positive Ratings:
    563
    If the sound is looped, just use the StopSound input.
     
  3. Egan

    aa Egan

    Messages:
    1,318
    Positive Ratings:
    1,512
    an alternative that will force stop the sound is to trigger the 'volume 0' input/parameter. stopsound doesnt seem to always function
     
  4. arthurgps2

    arthurgps2 L1: Registered

    Messages:
    32
    Positive Ratings:
    0
    This sound is an alarm, so it would sound strange using this method
     
  5. Egan

    aa Egan

    Messages:
    1,318
    Positive Ratings:
    1,512
    you can use 'fadeout' which does the same thing but over a period of time
     
  6. arthurgps2

    arthurgps2 L1: Registered

    Messages:
    32
    Positive Ratings:
    0
    Nope. The alarm needs to be stopped right at the time. To help you, the alarm name is items\cart_warning.wav
     
  7. Da Spud Lord

    aa Da Spud Lord L0: Crappy Member

    Messages:
    831
    Positive Ratings:
    563
    What do you mean? You mean that when the sound is supposed to stop, you want it to finish playing and stop rather than stopping partway through?
     
  8. Unregistered HyperCam 2

    Unregistered HyperCam 2 L3: Member

    Messages:
    110
    Positive Ratings:
    4
    The only solution that I have in mind and fits what I think you meant is using a logic_timer so you can wait until the sound stops before stopping it.
     
  9. arthurgps2

    arthurgps2 L1: Registered

    Messages:
    32
    Positive Ratings:
    0
    So I searched for anything that could help me in this and i found these 2 entities: math_counter and logic_brand. As how I got it, math_counter stores a numerical value and logic_brand... I didn't get it, but then, relating the logic_timer Unregistered HyperCam 2 has mentioned before to these 2 things I got a little eureka. All I need to do is, when the intel gets taken, the alarm triggers and the timer is activated, and every time the OnTimer output fires, it checks if the intel is still stolen. If it is, then the sound will play again, If it's not, then the sound will stop playing and the timer is deactivated. Hope you got it...
    C'mon guys, we're almost there! I trust in you!
     
  10. Rare Endangered Spycrab

    Rare Endangered Spycrab L2: Junior Member

    Messages:
    97
    Positive Ratings:
    30
    logic_brand isn't an entity. Do you mean logic_branch?
     
  11. arthurgps2

    arthurgps2 L1: Registered

    Messages:
    32
    Positive Ratings:
    0
    Woops.. yeah that's it
     
  12. arthurgps2

    arthurgps2 L1: Registered

    Messages:
    32
    Positive Ratings:
    0
    Is there a way to check if the sound is playing or anything? That's just what I need
     
  13. Da Spud Lord

    aa Da Spud Lord L0: Crappy Member

    Messages:
    831
    Positive Ratings:
    563
    Not that I know of. One workaround would be to set up an entity whose state is manually changed based on whether the sound is playing or not. I would recommend using a logic_branch, but you could alternatively use a logic_relay or a math_counter. Just fire an input to change the state of the entity every time the sound is played or stopped. For logic_branch or math_counter, you'll have inputs to test what the state of the entity is and fire appropriate outputs. If you use math_counter, you'll need to test the value against a logic_branch or logic_case. For logic_relay, you'll use the enabled/disabled state of the entity, and take advantage of the fact that the relay won't fire OnTrigger outputs while disabled to test if the sound is playing.

    Note about nonlooping sounds: For nonlooping sounds, you'll need to set up a logic_timer to set your entity of choice back to the not playing state when the sound duration is over. The length of the timer should be the length of the sound, and enable the timer when the sound starts playing. If your map has ever stops the sound before the duration is complete, you'll need to disable this timer at the same time as stopping the sound.
     
  14. arthurgps2

    arthurgps2 L1: Registered

    Messages:
    32
    Positive Ratings:
    0
    Oh... I think I got it, but how do I test the math_counter value against the logic_branch value?
     
  15. Da Spud Lord

    aa Da Spud Lord L0: Crappy Member

    Messages:
    831
    Positive Ratings:
    563
    In the math_counter, create a new output "OnGetValue" targeting a logic_branch with the input SetValueTest. Don't change the parameter. To do the test, just give the math_counter the GetValue input, then the logic_branch will fire either the OnTrue or OnFalse output, depending on what the math_counter's value was.