UE4 Multiplayer

引擎:

GameMode

Theconcept of a "game" is split into 2 classes. The GameModeis the definition of the game, including things like the game rulesand win conditions. It only exists on the server. It typically shouldnot have much data that changes during play, and it definitely shouldnot have transient data that clients need to know about.

GameMode只存在服务器中,不存在于客户端中。

GameMode可以拥有所有的PlayerController信息。

PlayerController

APlayerControlleris the interface between the Pawn and the human player controllingit. The PlayerController essentially represents the human player'swill.

PlayerController分别在客户端和服务器中存在副本,可以通过RPC(事件里的runon server / run on client)相互调用。


GameInstance

Oneof the new UE4 engine features as of 4.4 is the Game Instance class!

Thisis a globally accessible instanced UObject that can store any datayou want to be carried between levels!

Whereformally you would have had to write out data to a config file or tobinary file, to transfer between levels, now you can use the GameInstance class!


GameInstance独立于Level,可以用于level之间的数据传递。


多人游戏理解:

要实现多人游戏最简单的功能,只需简单的几步:主机CreateSession、其他FindSession

、再JoinSession

以上是最基本的部分,多人游戏涉及到人与人之间的交互。这些功能包括玩家信息自定义、信息的交互(比如聊天,准备开始之类)。这些功能实现的方式都是类似的:客户端-服务器-客户端,就是客户端的改变,将信息传递给服务器,让服务器把这些信息传递给所有(或一些)客户端。(例如基本的聊天就是客户端输入一些文本,传给服务器之后,服务器将这些文本传给所有的客户端,让所有的客户端都显示出来)。


RPC(Run on Server / Run on Cilent / Multicast):

Runon Server : 在客户端中call这个事件,在服务器中运行这个事件。

Runon Cilent : 在服务器中call这个事件,在客户端中运行这个事件。

Multicast: 这服务器中call这个事件,在客户端和服务器中运行这个事件。


关键事件:

EventonPostLogin:

打开游戏时/Open Level /Join Session 时会在GameMode里触发该事件。一 般用于保存PlayeControllers(当然GameState里也有),或者出生Pawn之类的行为 (属于客户端的行为)。

EventonLogout:

退出游戏/Destroy Session时会在GameMode里触发该事件。

EventonSwapPlayerController

条件:TravelGameMode不同、对应的PlayerController不同。


注意事项:

  1. 设置/config/GameEngine.ini,加入网络子系统的信息。

  2. 可以用GameInstance+ SaveGameObject 来保存玩家信息。

  3. 可以使用一个放置MenuLevel,一个放置游戏准备TravelLevel然后是其他游戏的Level

  4. Actor在服务器和客户端里都存在,若事件属于某种行为(跑,跳,生成特效),可以用runon server。当脚本运行到这时,在服务器该Actor会继续运行下去。

  5. 服务器和客户端的变量传递需要用replicated修饰。


出现的问题:

  1. CreateSession 不成功:没有添加OnlineSubSystem

  2. FindSession 不成功:可以FindSession 之后Delay0.2s

  3. 一般服务器响应、客户端不响应的情况就是RPC未调好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值