Forum

> > CS2D > Scripts > How to check if the server is playing a sound
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to check if the server is playing a sound

30 replies
Page
To the start Previous 1 2 Next To the start

old How to check if the server is playing a sound

Mami Tomoe
User Off Offline

Quote
Hi I want to make a script that plays sounds but I don't want the sounds to play all together I want it to play by order and the sounds will be chosen randomly so I can't use timers plz help thx bye

old Re: How to check if the server is playing a sound

Masea
Super User Off Offline

Quote
You must use time, second/ms100 hook or anything about time. Do a true parameter whenever sound starts. Save its second duration at another parameter. Then do something like when that duration parameter bigger than 0, reduce it by 1 at second hook. When duration parameter reached to 0, then false the first parameter I said.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
par1 = false
par2 = 0

function sound_(path)
	if par1 then return end
	parse("sv_sound ...")
	par1 = true
	par2 = SOUND'S SECOND DURATION
end

addhook("second","sec")
function sec()
	if par2 > 0 then
		par2 = par2 - 1
	else
		par1 = false
	end
end
You can do it even using only duration parameter.

old Re: How to check if the server is playing a sound

Masea
Super User Off Offline

Quote
Use tables then.
1
2
3
4
5
6
7
8
9
10
11
12
13
SOUNDS = {
	[1] = {
		path = "wada.wav",
		duration = 132, --sec
	},
	[2] = {
		path = "asdasdasd.wav",
		duration = 92, --sec
	}
}

local randomsound = math.random(1,#SOUNDS)
parse('sv_sound '..SOUNDS[randomsound].path)
Trust me, this is not hard...

old Re: How to check if the server is playing a sound

Mami Tomoe
User Off Offline

Quote
Yeah but each kill triggers a random sound and has a chance to trigger even more sounds like double kill + godlike and other players might kill as well and this will also show images on screen so I really need to check if it's playing a sound...

old Re: How to check if the server is playing a sound

THEMUD
User Off Offline

Quote
@user Mami Tomoe: Try this code, I'm not sure if this is what you are demanding.
1
2
3
4
5
6
7
8
9
10
11
sounds = {
	[1] = "sfx/NAME1.wav";
	[2] = "sfx/NAME2.wav";
	[3] = "sfx/NAME3.wav";
};

function _kill(id)
	parse("sv_sound "..sounds[math.random(1, #sounds)]);
end

addhook("kill", "_kill")
You can put more sound files in the table by providing its name, extension and path.

old Re: How to check if the server is playing a sound

Bowlinghead
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
--untested
-- edit it.
sounds = {"mysound1", "mysound2"}
times = {3000, 5000} -- first song is 3 secs long, 2nd is 5secs long

isSoundRunning = false

function killSound()
	isSoundRunning = false
end

function playSound(id)
	if (isSoundRunning == false) then
		parse("sound "..sounds[id]) 
		timer(times[id], "killSound")  
		isSoundRunning = true
	end
end
cs2d lua cmd timer

Do you want to hear the ignored sounds after the current sound has ended?
(Example: while "doublekill" is playing, someone did a tripple kill so after "doublekill" has ended, youll hear a "tripplekill")

autor has written
so plz is there a way to just CHECK if a sound is being played?

Yes, if you keep track of the time. You can make a function like getCurrentSound().

old Re: How to check if the server is playing a sound

Zeik
User Off Offline

Quote
I edited @user Bowlinghead:'s code so it uses a queue:

More >

The kill hook is just an example. You call the queueSound function whenever you need to play a sound, and then the ms100 hook will take care of playing the sounds in order.

PS: not tested.
PS 2: Queueing many sounds in short intervals will cause a total mess. So be careful.

old Re: How to check if the server is playing a sound

Mami Tomoe
User Off Offline

Quote
@user Zeik: Thank you! It seems like it might work

Hello!!! < This is an edit!

I added this function because I need it to play sometimes to one player only

But when I use it the game freeze ( stuck in while! )
1
2
3
4
5
6
7
8
9
10
11
12
13
function playSound2(id, sfx)
	local temp=false
	while temp==false do
		if (isSoundRunning == false) then
			if (not List.isEmpty(queue)) then
				isSoundRunning = true
				parse("sv_sound2 "..id.." "..sfx[1])
				timer(sfx[2], "killSound")
				temp=true
			end
		end
	end
end

plz help thx
edited 1×, last 23.01.17 05:14:32 pm

old Re: How to check if the server is playing a sound

Zeik
User Off Offline

Quote
@user Mami Tomoe: I don't know what you're trying to do but it won't work like that because the while loop will only end when isSoundRunning is false (is the only if block that changes temp state). So when it's true it's an infinite loop, thus the game freezes.

old Re: How to check if the server is playing a sound

Zeik
User Off Offline

Quote
I don't know how costly it is to have a private queue for each player but it's what you're needing.

Here's the complete code:
More >


Again, it's not tested.

PS: Basically, 0 is for global sound, and the rest are private.
edited 1×, last 23.01.17 09:31:04 pm

old Re: How to check if the server is playing a sound

Mami Tomoe
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
LUA ERROR: sys/lua/cool stuff/play.lua:27: attempt to perform arithmetic on field 'first' (a nil value)
 -> sys/lua/cool stuff/play.lua:27: in function 'push'
 -> sys/lua/cool stuff/play.lua:61: in function 'queueSound'
 -> sys/lua/cool stuff/main.lua:4: in main chunk
 -> [C]: in function 'dofile'
 -> sys/lua/server.lua:2: in main chunk
LUA ERROR: sys/lua/cool stuff/play.lua:38: attempt to compare two nil values
 -> sys/lua/cool stuff/play.lua:38: in function 'isEmpty'
 -> sys/lua/cool stuff/play.lua:66: in function 'playSound'
 -> sys/lua/cool stuff/play.lua:82: in function <sys/lua/cool stuff/play.lua:81>
LUA ERROR: sys/lua/cool stuff/play.lua:38: attempt to compare two nil values
 -> sys/lua/cool stuff/play.lua:38: in function 'isEmpty'
 -> sys/lua/cool stuff/play.lua:66: in function 'playSound'
 -> sys/lua/cool stuff/play.lua:82: in function <sys/lua/cool stuff/play.lua:81>

main is the main script that makes cool hooks
play is the script you gave me
score is the script that sends sound commands to play

old Re: How to check if the server is playing a sound

DC
Admin Off Offline

Quote
There is no Lua command to check if CS2D is still playing a sound. That's because Lua only runs on the server and the server does not render anything or play any sounds at all if it's a headless dedicated server.

In fact this functionality is completely removed from dedicated servers because it's not needed and it might even lead to issues because servers commonly don't have audio and video drivers at all. Therefore the server does not even know the length of the used audio files.

So the only thing you can do is to - like already suggested - save the duration of all audio samples somehow and check the duration manually.

To solve the issue with the per-player sound you could use cs2d lua cmd timer and just put the player ID in as parameter:
1
timer(soundDuration, "callbackFunction", playerID)
This value can then be used in the function called by the timer to play the next sound for the player with that particular ID.
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview