UDN
Search public documentation:

ReplicationPatternServerToClientRPC
日本語訳
中国翻译
한국어

Interested in the Unreal Engine?
Visit the Unreal Technology site.

Looking for jobs and company info?
Check out the Epic games site.

Questions about support via UDN?
Contact the UDN Staff

UE3 Home > Networking & Replication > Server to client remote procedure call pattern

Server to client remote procedure call pattern


Overview


The server to client remote procedure call pattern is used when you want that client to perform some logic client side. If you wish to only push data to the client, this can be done using variable replication patterns such as the RepNotify pattern. Use this when you want the client to perform some logic using incoming data. An example would be when the server wants to broadcast a message to all of the connected clients.

Player Controller class


Player controllers act as a bridge of communication between the client and the server. In this case, the server is able to replicate the ReceiveBroadcast function and its parameters to the client.

STCRPC_PlayerController.uc
class STCRPC_PlayerController extends PlayerController;

/**
 * Client function which receives a broadcast from the server
 *
 * Network: Client
 */
reliable client function ReceiveBroadcast(String ReceivedText)
{
  `Log(Self$":: Server sent me '"$ReceivedText$"'.");
}

defaultproperties
{
}

Game Info class


The GameInfo class only exists on the server. In this pattern, RandomBroadcast is executed once per second by using the SetTimer function. RandomBroadcast iterates over all of the player controller instances on the server, stored in WorldInfo, and replicates ReceiveBroadcast function and its parameters to the client's version of the player controller.

STCRPC_GameInfo.uc
class STCRPC_GameInfo extends GameInfo;

/**
 * Post Begin Play is executed after the GameInfo is created
 *
 * Network: Dedicated/Listen Server
 */
function PostBeginPlay()
{
  Super.PostBeginPlay();

  // Set the timer which broad casts a random message to all players
  SetTimer(1.f, true, 'RandomBroadcast');
}

/**
 * Broadcast a random message to all players
 *
 * Network: Dedicated/Listen Server
 */
function RandomBroadcast()
{
  local STCRPC_PlayerController PlayerController;
  local int i;
  local string BroadcastMessage;

  if (WorldInfo != None)
  {
    // Constuct a random text message
    for (i = 0; i < 32; ++i)
    {
      BroadcastMessage $= Chr(Rand(255));
    }

    ForEach WorldInfo.AllControllers(class'STCRPC_PlayerController', PlayerController)
    {
      `Log(Self$":: Sending "$PlayerController$" a broadcast message which is '"$BroadcastMessage$"'.");
      PlayerController.ReceiveBroadcast(BroadcastMessage);
    }
  }
}

defaultproperties
{
  PlayerControllerClass=class'STCRPC_PlayerController'
}

Testing


On the client, ReceiveBroadcast is received each second or so along with a very random broadcast message. STCRPC_ClientConsoleWindow.jpg

On the server, RandomBroadcast is called every second or so, and executes ReceiveBroadcast on every player controller that is connected to the server. STCRPC_ServerConsoleWindow.jpg

Downloads


  • Download the source code used in this pattern. (ServerToClientRPCExample.zip)