CRecipientFilter: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(→‎Derived classes: how did I miss this?)
(Reliable vs. Unreliable)
Line 10: Line 10:


  '''CRecipientFilter''' MyFilter;
  '''CRecipientFilter''' MyFilter;
  MyFilter.AddRecipientsByPVS( [[GetAbsOrigin()]] ); ''// Origin of the host entity; clients within its PVS will be added.''
  MyFilter.AddRecipientsByPVS( [[GetAbsOrigin()]] ); ''// OClients within the entity's PVS will be added.''
  MyFilter.MakeReliable(); ''// All this does is make m_bReliable true! Best to use it anyway.''
  MyFilter.MakeReliable();


  '''CRecipientFilter''' MyInverseFilter;
  '''CRecipientFilter''' MyInverseFilter;
Line 17: Line 17:
  MyInverseFilter.RemoveRecipientsByPVS( GetAbsOrigin() ); ''// Leaving only players '''outside''' the PVS''
  MyInverseFilter.RemoveRecipientsByPVS( GetAbsOrigin() ); ''// Leaving only players '''outside''' the PVS''
  MyInverseFilter.MakeReliable();
  MyInverseFilter.MakeReliable();
== Reliable vs. Unreliable ==
<code>[[MakeReliable()]]</code> gives any command using the filter instance a higher network transmission priority. It is placed early on in the next available [[Wikipedia:User Datagram Protocol|UDP]] [[packet]], and if it can't be sent immediately is queued. If a filter remains unreliable it is sent at the end of the packet if there is any room left, and if it isn't transmitted immediately is dropped and forgotten about.
This is useful for prioritising [[:Category:Networking|network data]] at times of heavy traffic. {{todo|Confirm this conjecture.}}


== Derived classes ==
== Derived classes ==

Revision as of 12:24, 16 May 2008

The CRecipientFilter class enables the server to pick and choose which clients it passes an item of data to. Most server-to-client communication supports it.

Alongside adding/removing clients individually or all at once, it can filter by:

Examples

CRecipientFilter MyFilter;
	MyFilter.AddRecipientsByPVS( GetAbsOrigin() );	// OClients within the entity's PVS will be added.
	MyFilter.MakeReliable();
CRecipientFilter MyInverseFilter;
	MyInverseFilter.AddAllPlayers();
	MyInverseFilter.RemoveRecipientsByPVS( GetAbsOrigin() );	// Leaving only players outside the PVS
	MyInverseFilter.MakeReliable();

Reliable vs. Unreliable

MakeReliable() gives any command using the filter instance a higher network transmission priority. It is placed early on in the next available UDP packet, and if it can't be sent immediately is queued. If a filter remains unreliable it is sent at the end of the packet if there is any room left, and if it isn't transmitted immediately is dropped and forgotten about.

This is useful for prioritising network data at times of heavy traffic.

Todo: Confirm this conjecture.

Derived classes

Several derived classes exist to automate the creation of common filters, with constructors set up to directly return an appropriately-configured instance.

CBroadcastRecipientFilter()
All players.
CPASFilter(origin)
All players in the origin's PAS.
CPVSFilter(origin)
All players in the origin's PVS.
CSingleUserRecipientFilter(CBasePlayer)
A specific player.
CTeamRecipientFilter(int team, bool isReliable = false)
A multiplayer team.
CLocalPlayerFilter()
The local player. Used as a dummy filter when calling functions requiring one on the client.

Example

To make an entity produce a sound from another location:

EmitSound( CPASFilter(pPuppet->GetAbsOrigin()),pPuppet->entindex(),"Ventriloquist.Hello" );

See also

Tip.pngTip:You don't need to use the filter entities as a programmer - you already have direct access to the relevant data.