Forum
CS2D General lua soundlua sound
8 replies 1
http://amx2d.co.cc/
have a look at utsfx.lua there. it shows how to play sounds with Lua.
COmpaQ has written
why do you need to advertise in every lua thread? one time should be enough. I suggest DC to make every advertising like this spam, if you do not even try to give an answer but just to get people to leegaos board.lee knows all about lua go ask him at the forum he runs
http://amx2d.co.cc/
http://amx2d.co.cc/
@ vrkiller:
1
parse("sv_sound \"Soundfile\"")
but you need to know that sounds used in lua are NOT transfered to the clients! They must be on the map, or everyone must have it!
edited 1×, last 16.04.09 08:23:26 am
TheKilledDeath has written
why do you need to advertise in every lua thread? one time should be enough. I suggest DC to make every advertising like this spam, if you do not even try to give an answer but just to get people to your board
Given that it is not even his board your accusation falls blindly. Have you ever considered the fact that maybe its because of those who have asked my help there, I've done my utmost to help all of them with what I can?
here's the code for an example menu that you are trying to make. Make sure that you will be able to load all of the sounds into your own map using the env_sound entity.
if you want to have it formatted better, go to
http://amx2d.co.cc/viewtopic.php?f=12&t=80
where it's already highlighted.
Note: Copy everything below and either place them inside of CS2D/sys/lua/server.lua or create a new file in CS2D/sys/lua/ called sfx.lua, paste everything in, and go to server.lua and add
1
dofile("sys/lua/sfx.lua")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
--@Usage : !sfx or !sfx Sound Name --Global Namespace-- sfx = {} --Sounds - Taken from sample/sayfunctions.lua sfx.sounds = {"The Way", "let's go", "zombies", "mystery"} --Sound Files - Must match the elements from sfx.sounds in the same order. sfx.files = {"fun/thats_the_way.wav", "hostage/hos2.wav", "player/zm_spray.wav", "env/mystery.wav"} --[[-- @NOTE: There's a more intuitive way of doing this by using just sfx.sounds as a key-value table but it'll require a bit more work. --]]-- function sfx.play(sound) 	--Sound is a number 	--This will produce a string that will look like 	--sv_sound "fun/thats_the_way.wav" 	parse(string.format('sv_sound "%s"', sfx.files[sound])) end addhook("menu", "sfx.menu") function sfx.menu(p, title, sel) 	if title == "Sounds" then 		--If we're dealing with the sound menu 		if sel <= #sfx.files then 			--#sfx.files = the number of elements inside the table sfx.files 			--In this case it is 4 			sfx.play(sel) 		end 	end end addhook("say","sfx.say") function sfx.say(p,txt) 	txt = txt:trim() -- Get rid of leading spaces that can cause parser problems 	--Use the string.split() statement to seperate text by the spaces 	--We're looking for "!sfx Name" or just "!sfx" local cmd = txt:split() 	--cmd = {[1] = "!sfx", [2] = "soundName" or nil} 	if cmd[1] == "!sfx" then 		local sound = txt:sub(#cmd[1]+2) -- "soundName" or nil 		if not cmd[2] then 			-- If no name is given to play, just go ahead and display the menu 			menu(p, "Sounds"..string.join(sfx.sounds)) -- string.join will turn a table into a string with the menu format. 			--[[-- 			print(string.join(sfx.sounds)) 			OutPut: 			The Way,let's go,zombies,mystery 			--]]-- 		else 			sound = sound:trim() -- Trims sound. Note: We must do this here because if sound is nil then it returns will return an error. 			if table.find(sfx.sounds, sound) then 				--table.find will find the first occurence of the sound in sfx.sound table 				--If it's not in the table then it will return a null value - nil 				sfx.play(table.find(sfx.sounds, sound)) 			else 				--table.find returns nil, prints a message to the user. 				msg2(p, "This sound is not installed.") 			end 		end 		return 2 -- Do not display the text. 	end end --initArray-- function initArray(i, x) local array = {} 	 --if (array == nil) then array = {} end if (x == nil) then x = 0 end for m = 1 , i do if (array[m] == nil) then array[m] = x end end return array end if not string.split then 	function string.split(t, d) 		local cmd = {} 		local match = "[^%s]+" 		if b then 			match = "%w+" 		end 		if type(b) == "string" then match = "[^"..b.."]+" end 		if not t then return invalid() end 		for word in string.gmatch(t, match) do 			table.insert(cmd, word) 		end 		return cmd 	end 	function string.trim(t) 		t = string.split(t) 		s = "" 		if not t then return end 		for i, v in ipairs(t) do 			s = s.." "..v 		end 		s = string.sub(s, 2) 		return s 	end 	function string.join(t, n, d) 		local s = "" 		if (n == nil) then n = 1 end 		if not d then d = "," end 		while (n <= #t) do 			s = s .. t[n] .. "".. d 			n = n + 1 		end 		s = string.sub(s, 1, #s-#d) 		return s 	end 	function table.find(t, o) 		for k, v in pairs(t) do 			if v == o then 				return k 			end 		end 	end end
Do not bother with the last half of this file since they're a few utility functions to make this easier. Just look at the first 60 lines.
Note: To add more sounds into the list, just find the line
1
sfx.sounds = {"The Way", "let's go", "zombies", "mystery"}
Then find
1
sfx.files = {"fun/thats_the_way.wav", "hostage/hos2.wav", "player/zm_spray.wav", "env/mystery.wav"}
If you have any further questions, just PM me or go to the site Compaq posted above.
Btw, I'd appreciate it if you'd tell me whether the script works or not, I haven't tested it yet but the logic should work out.
Pls make that dc then you will get a cake!
TheKilledDeath has written
@ vrkiller:
but you need to know that sounds used in lua are NOT transfered to the clients! They must be on the map, or everyone must have it!
COmpaQ has written
why do you need to advertise in every lua thread? one time should be enough. I suggest DC to make every advertising like this spam, if you do not even try to give an answer but just to get people to leegaos board.lee knows all about lua go ask him at the forum he runs
http://amx2d.co.cc/
http://amx2d.co.cc/
@ vrkiller:
1
parse("sv_sound \"Soundfile\"")
but you need to know that sounds used in lua are NOT transfered to the clients! They must be on the map, or everyone must have it!
I don't see anyone asking you to make scripts.
1