menu_bookDocumentation
Network Events - s&box Networking Documentation
Network Events
Network events allow you to broadcast and listen for events across the network. Unlike RPCs which target specific objects, network events are global to the connection.
Example
Broadcast and listen for custom events:
CSHARP
// Broadcast an event to all clients
Network.Broadcast( "game.start" );
// Broadcast with data
Network.Broadcast( "player.joined", playerName );INetworkListener
Implement INetworkListener to receive connection events:
CSHARP
public class MyGame : Component, INetworkListener
{
public void OnActive( Connection conn )
{
// New connection established
Log.Info( $"Player joined: {conn.DisplayName}" );
}
public void OnDisconnected( Connection conn, Connection.Reason reason )
{
// Connection lost
Log.Info( $"Player left: {conn.DisplayName}" );
}
}INetworkSpawn
Get notified when objects are network spawned:
CSHARP
public class Spawnable : Component, INetworkSpawn
{
public void OnNetworkSpawn( Connection owner )
{
// This object was just spawned over the network
if ( IsProxy )
{
// We're on a client, this just appeared
}
}
}Listening
Subscribe to network events:
CSHARP
Network.Listen( "game.start", () =>
{
StartGame();
} );
Network.Listen( "player.joined", ( string playerName ) =>
{
ShowNotification( $"{playerName} joined!" );
} );Broadcasting
Send events from server to clients:
CSHARP
// From server only
if ( Network.IsHost )
{
Network.Broadcast( "round.end", winnerTeam );
}Network events are useful for game state changes, notifications, and coordination between clients.
Was this helpful?