menu_bookDocumentation
RPC Messages: Remote Procedure Calls with Broadcast, Owner, and Host Targets
Components can contain RPCs (Remote Procedure Calls). An RPC is a function that when called, is also called remotely on other clients.
Broadcast RPC
[Rpc.Broadcast] makes a function call broadcast to everyone:CSHARP
void OnPressed()
{
PlayOpenEffects();
}
[Rpc.Broadcast]
public void PlayOpenEffects()
{
Sound.Play( "bing", WorldPosition );
}Static RPC
Static methods can be RPCs without needing a Component:
CSHARP
[Rpc.Broadcast]
public static void PlaySoundAllClients( string soundName, Vector3 position )
{
Sound.Play( soundName, position );
}RPC Types
| Attribute | Description |
|---|---|
| [Rpc.Broadcast] | Calls the function for everybody |
| [Rpc.Owner] | Only called for the owner of the networked object (or host if no owner) |
| [Rpc.Host] | Only called on the host |
NetFlags
CSHARP
[Rpc.Broadcast( NetFlags.Unreliable | NetFlags.OwnerOnly )]
public void PlayEffect( string sound, Vector3 pos ) { }| Flag | Description |
|---|---|
| NetFlags.Unreliable | May not arrive or arrive out of order. Fast and cheap. Good for effects. |
| NetFlags.Reliable | Default. Multiple attempts until received. Use for important events. |
| NetFlags.SendImmediate | Not grouped with other messages, sent immediately. Good for streaming. |
| NetFlags.HostOnly | RPC can only be called from the host |
| NetFlags.OwnerOnly | RPC can only be called from the object's owner |
Filtering Recipients
CSHARP
using ( Rpc.FilterExclude( c => c.DisplayName == "Harry" ) )
{
PlayOpenEffects( "bing", WorldPosition );
}
using ( Rpc.FilterInclude( c => c.DisplayName == "Garry" ) )
{
PlayOpenEffects( "bing", WorldPosition );
}Caller Information
Check which connection called the RPC using Rpc.Caller:
CSHARP
[Rpc.Broadcast]
public void PlayOpenEffects( string soundName, Vector3 position )
{
if ( !Rpc.Caller.IsHost ) return;
Log.Info( $"{Rpc.Caller.DisplayName} ({Rpc.Caller.SteamId}) played effects" );
Sound.Play( soundName, position );
}
Was this helpful?