GM/PlayerCanHearPlayersVoice

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (To be consistent with the note in the description I marked the current example as inefficient and provided an efficient implementation)
 
Line 5: Line 5:
 
|Realm=Server
 
|Realm=Server
 
|Predicted=No
 
|Predicted=No
|HidePredictionWarning=No
 
|Name=PlayerCanHearPlayersVoice
 
|Parent=GM
 
 
}}
 
}}
 
{{Arg
 
{{Arg
Line 28: Line 25:
 
}}
 
}}
 
{{Example
 
{{Example
|Description=Players can only hear each other if they are within 500 units.
+
|Description='''Inefficient''' implementation where players can only hear each other if they are within 500 units.
 
|Code=hook.Add("PlayerCanHearPlayersVoice", "Maximum Range", function(listener, talker)
 
|Code=hook.Add("PlayerCanHearPlayersVoice", "Maximum Range", function(listener, talker)
 
     if listener:GetPos():Distance(talker:GetPos()) > 500 then return false end
 
     if listener:GetPos():Distance(talker:GetPos()) > 500 then return false end
 +
end)
 +
}}
 +
{{Example
 +
|Description='''Efficient''' implementation where players can only hear each other if they are within 500 units. This is more efficient, because {{ClassFunction|Vector|Distance}} adds an additional square root operation, which isn't necessary and rather expensive.
 +
|Code=hook.Add("PlayerCanHearPlayersVoice", "Maximum Range", function(listener, talker)
 +
    if listener:GetPos():DistToSqr(talker:GetPos()) > 250000 then return false end
 
end)
 
end)
 
}}
 
}}

Latest revision as of 19:59, 22 January 2020

 GM:PlayerCanHearPlayersVoice( )

Contents

Description

Decides whether a player can hear another player using voice chat.

NOTE

This hook is called several times a tick, so ensure your code is efficient.

Arguments

Player listener

The listening player.

Arguments

Player talker

The talking player.

Returns

boolean

Return true if the listener should hear the talker, false if they shouldn't.

Returns

boolean

3D sound. If set to true, will fade out the sound the further away listener is from the talker, the voice will also be in stereo, and not mono.

Examples

Example

Inefficient implementation where players can only hear each other if they are within 500 units.

hook.Add("PlayerCanHearPlayersVoice", "Maximum Range", function(listener, talker)
    if listener:GetPos():Distance(talker:GetPos()) > 500 then return false end
end)


Examples

Example

Efficient implementation where players can only hear each other if they are within 500 units. This is more efficient, because Vector:Distance adds an additional square root operation, which isn't necessary and rather expensive.

hook.Add("PlayerCanHearPlayersVoice", "Maximum Range", function(listener, talker)
    if listener:GetPos():DistToSqr(talker:GetPos()) > 250000 then return false end
end)


Personal tools
Navigation