OnClientWorldSound: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Client event}} This event is triggered whenever a world sound is played. A world sound is a sound effect which has not been caused by playSound or [[playSound3D]...")
 
 
(11 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Client event}}
__NOTOC__
__NOTOC__
{{Client event}}
{{New feature/item|3.0157|1.5.6|14652|This event triggers whenever a GTA sound starts playing.}}
 
{{Note|Use [[setWorldSoundEnabled]] if you want to disable certain sounds conditionless.
This event is triggered whenever a world sound is played. A world sound is a sound effect which has not been caused by [[playSound]] or [[playSound3D]].
For example, you should only cancel player emitted sounds in this event, because when you cancel certain vehicle sounds, the game will try to play the same sound on the next frame.}}


==Parameters==
==Parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
int group, int index
int group, int index, float x, float y, float z
</syntaxhighlight>  
</syntaxhighlight>
 
*'''group:''' An [[int|integer]] representing the [[World sound groups|world sound group]]
*'''group''': An integer representing the world sound group
*'''index:''' An [[int|integer]] representing an individual sound within the group
*'''index''': An integer representing an individual sound within the group
*'''x:''' a [[float]]ing point number representing the X coordinate on the map.
*'''y:''' a [[float]]ing point number representing the Y coordinate on the map.
*'''z:''' a [[float]]ing point number representing the Z coordinate on the map.


==Source==
==Source==
The [[event system#Event source|source]] of this event is the root element
The source of this event is the element, which emitted the sound.


==Cancel effect==
==Cancel effect==
If this event is [[Event system#Canceling|canceled]], then the sound will not be played.
If this event is [[Event system#Canceling|canceled]], the sound won't play at all.


==Example==  
==Example==
This example prints the world sound group and index in the chat box, and stops all world sound effects in group 5
'''Example 1:''' This example will cancel every vehicle sound.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function handleOnClientWorldSound( group, index )
addEventHandler("onClientWorldSound", root, function()
    outputChatBox( "World sound group:" .. group .. ", index:" .. index )
     if getElementType(source) == "vehicle" then
     if group == 5 then
        cancelEvent()
cancelEvent() -- Don't play this
     end
     end
end)
</syntaxhighlight>
'''Example 2:''' This example lets you see how many times each sound that gets played has been played using '/seesoundlist'.
<syntaxhighlight lang="lua">
local sounds = {}
addEventHandler("onClientWorldSound", root, function(group, index)
sounds[group.." | "..index] = (sounds[group.." | "..index] or 0) + 1
end)
function cmdSeeSoundList()
-- Put the non iterated table into an interated table so we can sort them
local tbl = {}
for sound, count in pairs(sounds) do
tbl[#tbl + 1] = {sound, count}
end
table.sort(tbl, function(a, b) return a[2] > b[2] end)
-- Output the table to clipboard
local str = "Group | Index: Times played\n"
for i, dat in ipairs(tbl) do
str = str..dat[1]..": "..dat[2].."\n"
end
setClipboard(str)
outputChatBox("Use CTRL + V in notepad to view the table.")
end
end
addEventHandler ( "onClientWorldSound", getRootElement(), handleOnClientWorldSound)
addCommandHandler("seesoundlist", cmdSeeSoundList)
</syntaxhighlight>
</syntaxhighlight>
==Requirements==
{{Requirements|n/a|1.3.0-9.04082|}}


==See Also==
==See Also==
===Client sound events===
===World sound functions===
{{Sound_events}}
* [[setWorldSoundEnabled]]
* [[isWorldSoundEnabled]]
* [[resetWorldSounds]]
===Client other events===
{{Client_other_events}}
===Client event functions===
{{Client_event_functions}}

Latest revision as of 21:03, 5 April 2020

This event triggers whenever a GTA sound starts playing.

[[{{{image}}}|link=|]] Note: Use setWorldSoundEnabled if you want to disable certain sounds conditionless.

For example, you should only cancel player emitted sounds in this event, because when you cancel certain vehicle sounds, the game will try to play the same sound on the next frame.

Parameters

int group, int index, float x, float y, float z
  • group: An integer representing the world sound group
  • index: An integer representing an individual sound within the group
  • x: a floating point number representing the X coordinate on the map.
  • y: a floating point number representing the Y coordinate on the map.
  • z: a floating point number representing the Z coordinate on the map.

Source

The source of this event is the element, which emitted the sound.

Cancel effect

If this event is canceled, the sound won't play at all.

Example

Example 1: This example will cancel every vehicle sound.

addEventHandler("onClientWorldSound", root, function()
    if getElementType(source) == "vehicle" then
        cancelEvent()
    end
end)

Example 2: This example lets you see how many times each sound that gets played has been played using '/seesoundlist'.

local sounds = {}

addEventHandler("onClientWorldSound", root, function(group, index)
	sounds[group.." | "..index] = (sounds[group.." | "..index] or 0) + 1
end)

function cmdSeeSoundList()
	-- Put the non iterated table into an interated table so we can sort them
	local tbl = {}
	for sound, count in pairs(sounds) do
		tbl[#tbl + 1] = {sound, count}
	end
	table.sort(tbl, function(a, b) return a[2] > b[2] end)
	-- Output the table to clipboard
	local str = "Group | Index: Times played\n"
	for i, dat in ipairs(tbl) do
		str = str..dat[1]..": "..dat[2].."\n"
	end
	setClipboard(str)
	outputChatBox("Use CTRL + V in notepad to view the table.")
end
addCommandHandler("seesoundlist", cmdSeeSoundList)

See Also

World sound functions

Client other events


Client event functions