Forum

> > CS2D > Scripts > Sv_stopsound
Forums overviewCS2D overview Scripts overviewLog in to reply

English Sv_stopsound

9 replies
To the start Previous 1 Next To the start

old Sv_stopsound

_Vava_
User Off Offline

Quote
Hey Everyone today i have an idea with some Sounds and i want to use sv_stopsound to stop some sounds

1
2
3
4
5
6
7
8
addhook("say","onsay")
function onsay(id,txt)
if txt=="!start" then
parse("sv_sound ZombieCrown/ambience1.wav")
elseif txt=="!stop" then
parse("sv_stopsound ZombieCrown/ambience1.wav 0")
end
end

but it's not working

old Re: Sv_stopsound

Baloon
GAME BANNED Off Offline

Quote
1
2
3
4
5
6
7
8
addhook("say","onsay")
function onsay(id,txt)
if string.sub(txt,1,6)=="!start" then
parse('sv_sound "ZombieCrown/ambience1.wav"')
elseif string.sub(txt,1,5)=="!stop" then
parse('sv_stopsound "ZombieCrown/ambience1.wav" 0')
end
end

old Re: Sv_stopsound

_Vava_
User Off Offline

Quote
Thx @user Baloon:
is there any lua that if start a sound it stop the previous Sound
like that I should not write every time sv_stopsound

old Re: Sv_stopsound

GeoB99
Moderator Off Offline

Quote
No. Once cs2d cmd sv_sound executes its sound file as signed in the parameter you have to wait till the sound stops.

old Re: Sv_stopsound

MikuAuahDark
User Off Offline

Quote
user _Vava_ has written
Thx @user Baloon:
is there any lua that if start a sound it stop the previous Sound
like that I should not write every time sv_stopsound


Do you mean something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
local playing_sound_list = {}

local function attempt_stop_sound(player)
	if playing_sound_list[player] then
		parse(string.format("sv_stopsound %q %d", playing_sound_list, player))
		playing_sound_list[player] = nil
	end
end

function play_sound(path, player)
	if type(player) ~= "number" then
		for i = 1, 32 do
			attempt_stop_sound(i)
			playing_sound_list[i] = path
			parse(string.format("sv_sound %q", path))
		end
	else
		attempt_stop_sound(player)
		playing_sound_list[player] = path
		parse(string.format("sv_sound2 %d %q", player, path))
	end
end

Call it like this
1
2
3
play_sound("sfx/path/to/audio.flac", 1)	-- Play only for ID 1
-- other part in your code --
play_sound("sfx/some_sound.ogg")	-- Play for all players and stop previous playing sound for ID 1

old Re: Sv_stopsound

RIP-HereRestsPlookerbooy
BANNED Off Offline

Quote
@user MikuAuahDark:
Change this

1
2
3
4
5
for i = 1, 32 do
     attempt_stop_sound(i)
     playing_sound_list[i] = path
     parse(string.format("sv_sound %q", path))
end

To this

1
2
3
4
5
for _,i in pairs(player(0, "table")) do
     attempt_stop_sound(i)
     playing_sound_list[i] = path
     parse(string.format("sv_sound %q", path))
end

> Prevents some lag atleast...

old Re: Sv_stopsound

Baloon
GAME BANNED Off Offline

Quote
@user _Yank: you may be wrong. Considering pairs is bit slower than ipairs, this looping method
for i = 1,32 do
and this looping method
for _,i in ipairs(player(0,"table"))
are same.

old Re: Sv_stopsound

_Yank
User Off Offline

Quote
@user Baloon: But it is pairs that @user RIP-HereRestsPlookerbooy used on his post. Even if it wasn't, the ipairs method still slower than the for one because it relies on a function (thus additional delays for it being called and for it to produce the return value may be included). And still, only in LuaJIT that ipairs method is as fast as the for one when dealing with arrays.

Anyways, you shouldn't care too much about this, the difference is very very VERY minimal and should only be noticeable on much bigger and/or complex cases.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview