Files
sbox-public/engine/Sandbox.Engine/Systems/Networking/System/NetworkSystem.Send.cs
Tony Ferguson 06f7a33e73 Platform chat system (#4857)
* Added a platform level chatbox that games can use
* Published games do not have this
* Newly published projects have it enabled by default, if the game is networked
* Check out the Platform page in Project Settings to turn it off
* Party chat uses this, you can also switch between party / game chat if eligible using TAB

You can use `Sandbox.Platform.Chat.AddText( ... )` to add system text for notifications or other things. This has blocking behaviour built-in, and you can disable the chat in your settings menu.

You can listen to chat messages using `IChatEvent.OnChatMessage`, so if you want to filter for stuff like team chat, add commands to your game, you can do that. We have potential plans for first-party commands / chat channels in the future.
2026-05-20 10:53:10 +00:00

82 lines
2.7 KiB
C#

namespace Sandbox.Network;
internal partial class NetworkSystem
{
/// <summary>
/// Send a message to all connections. You can optionally pass in a filter to determine who actually receives the message.
/// </summary>
/// <param name="msg">The message to send.</param>
/// <param name="minimumState">The minumum state the connection must be to receive the message.</param>
/// <param name="filter">If specified, the connection must pass this filter to receive the message.</param>
/// <param name="flags">Network message flags that may dictate how the message is sent.</param>
public void Broadcast( ByteStream msg, Connection.ChannelState minimumState = Connection.ChannelState.Snapshot, Connection.Filter? filter = null, NetFlags flags = NetFlags.Reliable )
{
IEnumerable<Connection> availableConnections = Networking.IsHost ? _connections : Connection.All;
if ( Connection is not null )
{
availableConnections = availableConnections
.Append( Connection )
.Distinct();
}
// Encode once so every recipient gets the same wire bytes without re-compressing per connection.
var encoded = Connection.Encode( msg );
foreach ( var c in availableConnections )
{
if ( c == Connection.Local ) continue;
if ( c.State < minimumState ) continue;
if ( filter.HasValue && !filter.Value.IsRecipient( c ) ) continue;
c.Send( encoded, flags );
}
}
/// <summary>
/// Broadcast a packed message to all connections.
/// </summary>
internal void Broadcast<T>( T msg, Connection.ChannelState minimumState = Connection.ChannelState.Snapshot,
Connection.Filter? filter = null, NetFlags flags = NetFlags.Reliable )
{
var bs = ByteStream.Create( 32 );
bs.Write( InternalMessageType.Packed );
TypeLibrary.ToBytes( msg, ref bs );
Broadcast( bs, minimumState, filter, flags );
bs.Dispose();
}
/// <summary>
/// Get a list of connections that meet a specific criteria.
/// </summary>
/// <param name="minimumState">The minumum state the connection must be to receive the message.</param>
/// <param name="filter">If specified, the connection must pass this filter to receive the message.</param>
public IEnumerable<Connection> GetFilteredConnections( Connection.ChannelState minimumState = Connection.ChannelState.Snapshot, Connection.Filter? filter = null )
{
IEnumerable<Connection> availableConnections = Networking.IsHost ? _connections : Connection.All;
if ( Connection is not null )
{
availableConnections = availableConnections
.Append( Connection )
.Distinct();
}
foreach ( var c in availableConnections )
{
if ( c == Connection.Local )
continue;
if ( c.State < minimumState )
continue;
if ( filter.HasValue && !filter.Value.IsRecipient( c ) )
continue;
yield return c;
}
}
}