[[https://docs.unrealengine.com/en-US/InteractiveExperiences/Networking/Server/index.html | 参考]]
UE4的多人在线游戏采用的是client-server模型。意味着有一个单独的server负责游戏状态的裁判,同时所有其他remote终端都维持一份本地相对server的近似。
Server是多人里重要的一部分,它做出所有重要的决定:
* 所有状态控制任务
* 处理remote client连接
* 切换新关卡
* 负责游戏流程:开局、结束等
* etc...
= Starting a Server =
基本命令行(这些命令use the editor,因而不依赖cooked data):
|| Type || Command ||
|| Listen Server || UE4Editor.exe !ProjectName !MapName?Listen -game ||
|| Dedicated Server || UE4Editor.exe !ProjectName !MapName -server -game -log ||
|| Client || UE4Editor.exe !ProjectName ServerIP -game ||
{{attachment:multiplayer.png}}
= Server Gameplay Flow =
The server is in charge of driving the flow of gameplay. It is the server's job to notify clients that it is time to travel to a new map, when gameplay starts and ends, along with actor replication updates, etc.
主要的[[UE4/GameplayFramework | 游戏框架]]超出本文的范畴,这里只是简单提及一些:<
>
Gameplay state and flow are generally driven through the '''!GameMode Actor'''. Only the server contains a valid copy of this Actor (the client doesn't contain a copy at all). To communicate this state to the client, there is a '''!GameState Actor''', that will shadow important state of the !GameMode Actor. This !GameState Actor is marked to be replicated to each client. Clients will contain an approximate copy of this !GameState Actor, and can use this Actor as a reference to know the general state of the game.
上面的话解释一下就是:UE4游戏进展基本上是靠!GameMode Actor来驱动的。并且只有Server上有该Actor,其他client端没有。client端要想获知游戏进展,需要依靠!GameState Actor,在Server上有一个Authoritative !GameState Actor,它屏蔽了!GameMode Actor的一些重要内部细节,并将除此之外的关于game state的信息replicates到remote proxies,client端可根据这些proxies获知游戏状态。
= Connection Process =
终端连接过程。
1. Client发送连接请求
1. 如果Server接受请求:发回当前关卡
1. Server等待Client加载当前关卡
1. Client加载完成后,Server会调用'''AGameModeBase::!PreLogin'''
* 这里!GameMode可以选择拒绝连接
1. 如果!GameMode接受连接,Server会进一步调用'''AGameModeBase::Login'''
* The role of this function is to create a '''!PlayerController''', which will then be replicated to the newly connected client. Once received, this !PlayerController will replace the clients temporary !PlayerController that was used as a placeholder during the connection process.<>[[UE4-Networking-And-Multiplayer/Actor_Replication/Actors_and_their_Owning_Connections#connection_1 |(相关)]]
* Note that '''APlayerController::!BeginPlay''' will be called here. It should be noted that it is '''NOT''' yet safe to call RPC functions on this actor. You should wait until '''AGameModeBase::!PostLogin''' is called.
1. 假如一切顺利,到这里会调用'''AGameModeBase::!PostLogin'''
* At this point, it is safe for the server to start calling RPC functions on this PlayerController.
----
CategoryUnrealEngine