Check if sound is playing to toggle it?

  • If you're asking a question make sure to set the thread type to be a question!

arthurgps2

L1: Registered
Feb 11, 2018
32
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
 

Da Spud Lord

Occasionally I make maps
aa
Mar 23, 2017
1,339
994
If the sound is looped, just use the StopSound input.
 

arthurgps2

L1: Registered
Feb 11, 2018
32
0
Nope. The alarm needs to be stopped right at the time. To help you, the alarm name is items\cart_warning.wav
 

Da Spud Lord

Occasionally I make maps
aa
Mar 23, 2017
1,339
994
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?
 
Mar 2, 2018
124
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.
 

arthurgps2

L1: Registered
Feb 11, 2018
32
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!
 

ficool2

L4: Comfortable Member
Oct 28, 2017
161
232
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!

logic_brand isn't an entity. Do you mean logic_branch?
 

Da Spud Lord

Occasionally I make maps
aa
Mar 23, 2017
1,339
994
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.
 

arthurgps2

L1: Registered
Feb 11, 2018
32
0
Oh... I think I got it, but how do I test the math_counter value against the logic_branch value?
 

Da Spud Lord

Occasionally I make maps
aa
Mar 23, 2017
1,339
994
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.