UDN
Search public documentation:

ReplicationPatternServerToClientRPCCH
English Translation
日本語訳
한국어

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

虚幻引擎3主页 > 网络&复制 > > 服务器到客户端远程过程调用模式

服务器到客户端远程过程调用模式


概述


当您想让客户端执行一些客户端侧逻辑时使用该服务器到客户端远程过程调用模式。如果您仅想把数据发送给客户端,那么可以使用类似于RepNotify(复制通知)模式的变量复制模式来完成。当您想让客户端使用收到的数据来执行一些逻辑时使用这个模式。其中一个应用示例是服务器广播一条信息给所有连接的客户端。

Player Controllers(玩家控制器)类


玩家控制器是客户端和服务器之间的通信桥梁之一。在这个示例中,服务器可以将ReceiveBroadcast 函数及其参数复制到客户端。

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(游戏信息)类


GameInfo类仅存在于服务器上。在该模式中,通过使用SetTimer函数每秒钟执行一次RandomBroadcast。RandomBroadcast在服务器上迭代所有存储在WorldInfo中的玩家控制器实例,并将ReceiveBroadcast函数及其参数复制给玩家控制器的客户端版本。

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'
}

测试


在客户端上,每秒都会接收到ReceiveBroadcast和一个随机的广播信息。 STCRPC_ClientConsoleWindow.jpg

在服务器上,大约每秒都调用RandomBroadcast,并在连接到服务器上的每个玩家控制器上执行ReceiveBroadcast 。 STCRPC_ServerConsoleWindow.jpg

下载


  • 下载这个模式中使用的源码。(ServerToClientRPCExample.zip)