Skip to main content

[TargetRpc]

[TargetRpc] attribute is used when the server calls a function on a specific client. It is employed to deliver changes in the server's state to a specific client only.

Key Features

  • Can only be called from the server to a specific client.
  • Executes only on the specified client.
  • The function name must begin with the Target prefix.
  • The first parameter must be of type NetworkConnection.

Use Examples

Basic Use

public class PlayerNotification : NetworkBehaviour
{
[Server]
private void NotifyPlayer(NetworkConnection target)
{
TargetShowMessage(target, "Welcome to the game!");
}

[TargetRpc]
private void TargetShowMessage(NetworkConnection target, string message)
{
// Logic executed on the specific client
// Display a UI message, play sound, etc.
}
}

Personalized Information Delivery

public class InventorySystem : NetworkBehaviour
{
[Server]
private void SendInventoryUpdate(NetworkConnection player, List<Item> items)
{
TargetUpdateInventory(player, items);
}

[TargetRpc]
private void TargetUpdateInventory(NetworkConnection target, List<Item> items)
{
// Update the inventory UI for the specific player
}
}

Sending Individual Notifications

public class QuestManager : NetworkBehaviour
{
[Server]
private void QuestCompleted(NetworkConnection player, int questId)
{
TargetShowQuestComplete(player, questId);
}

[TargetRpc]
private void TargetShowQuestComplete(NetworkConnection target, int questId)
{
// Show quest completion UI
// Trigger reward animation
// Play sound, etc.
}
}
note
  1. The [TargetRpc] attribute is used when information needs to be delivered to a specific client.
  2. The function name must begin with Target.
  3. The first parameter should be a NetworkConnection object.
  4. It is typically used for personalized messages, updating a player's state. :::