BlueRoseNote/03-UnrealEngine/Gameplay/Online/OnlineSubsystem使用笔记.md
2023-06-29 11:55:02 +08:00

117 lines
5.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: OnlineSubsystem使用笔记
date: 2022-12-09 14:59:15
excerpt:
tags: Online
rating: ⭐
---
## 参考
《Exploring in UE4》Session与Onlinesubsystem[概念理解]https://zhuanlan.zhihu.com/p/34257172
https://dawnarc.com/2019/07/ue4networking-in-baisc-sessions/
## 添加模块
![](https://oscimg.oschina.net/oscnet/6501b00e721d9867e9422c057d79a3f8323.png)
## 添加配置
在DefaultEngine.ini中的 [OnlineSubsytem]标签下将DefaultPlatformService指定为所需服务。测试则使用Steam
在确定平台后UE4就会去加载OnlineSubsystem +Name的模块。
ps1.OnlineSubsystemNull为本地处理模块。
ps2.加载成功后还要继续调用对应平台Module的StartupModule函数。如果是steam还需要到”../Engine/Binaries/ThirdParty/ Steamworks/Steamv132/Win64/”路径下去加载其平台的dll文件(路径可能有些偏差具体看文件steam_api64.dll的位置) 代码如下:
```c++
FString RootSteamPath = GetSteamModulePath();
FPlatformProcess::PushDllDirectory(*RootSteamPath);
SteamDLLHandle = FPlatformProcess::GetDllHandle(*(RootSteamPath + "steam_api64.dll "));
```
### 添加OnlineSubsystemSteam配置
一般默认在非Shipping版本或者配置文件OnlineSubsystemSteam的bEnable为false的情况下在初始化OnlinesubsystemSteam的时候包括其他平台会CreateSubsystem失败然后Destroy该Onlinesubsystem。这样引擎会默认创建OnlinesubsystemNull来替代。所以需要将bEnable设置成true。
![](https://img-blog.csdn.net/20180203094035717?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk5OTk4NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
### 配置Steam SDK过程
UE4使用steam子系统发布steam包https://www.cnblogs.com/VirtualJourneyStudio/archive/2004/01/13/10557044.html
## 文档笔记
https://docs.unrealengine.com/zh-CN/ProgrammingAndScripting/Online/index.html
static IOnlineSubsystem* Get(const FName& SubsystemName = NAME_None)
### 主要接口
- Achievements列出游戏中的所有成就解锁成就并查看自己和其他用户已解锁的成就。
- External UI打开特定硬件平台或在线服务的内置用户接口。在某些情况下仅可通过此接口获取部分核心功能的访问权。
- Friends好友和好友列表的相关内容例如在好友列表中添加用户、阻止和解除阻止用户以及列出最近遇到的在线玩家。
- Leaderboard访问在线排行榜包括登记自己的得分或时间以及在排行榜中查看好友列表或世界其他玩家的得分。
- Online User收集关于用户的元数据。
- Presence设置用户在线状态的显示方式例如"在线"、"离开"、"游戏中"等。
- Purchase进行游戏内购和查看购买历史。
- Session创建、撤销和管理在线游戏会话。还包括搜索会话和配对系统。
- Store检索游戏内购可用的条目和特定价格。
- User Cloud提供每个用户云文件存储的接口。
#### Sessions
- 使用所需设置创建新会话
- 等待玩家申请加入比赛
- 注册想要加入的玩家
- 开始会话
- 玩游戏
- 终止会话
- 取消玩家注册
或者:
- 如果你想要变更比赛类型并返回以等待玩家加入,则更新会话
- 终止会话
#### FOnlineSessionSettings
FOnlineSessionSettings除了这些基础属性外可以增加一些自定义属性具体是往一个FOnlineKeyValuePairs<FName, FOnlineSessionSetting>里添加属性之后客户端在OnFindSessionsComplete()中再通过指定的FName取出。
- bAllowJoinInProgress
- bIsDedicated
- bIsLANMatch
- ShouldAdvertise
- bUsesPresence
- NumPublicConnections
- NumPrivateConnections
```
/** Array of custom session settings */
FSessionSettings Settings;
/** Type defining an array of session settings accessible by key */
typedef FOnlineKeyValuePairs<FName, FOnlineSessionSetting> FSessionSettings;
struct FOnlineSessionSetting
{
public:
/** Settings value */
FVariantData Data;
/** How is this session setting advertised with the backend or searches */
EOnlineDataAdvertisementType::Type AdvertisementType;
/** Optional ID used in some platforms as the index instead of the session name */
int32 ID;
}
```
##### Steam设置
对于Steam需要设置以下一些属性
- ShooterHostSettings->Set(SETTING_MATCHING_HOPPER, FString("TeamDeathmatch"), EOnlineDataAdvertisementType::DontAdvertise);
- ShooterHostSettings->Set(SETTING_MATCHING_TIMEOUT, 120.0f, EOnlineDataAdvertisementType::ViaOnlineService);
- ShooterHostSettings->Set(SETTING_SESSION_TEMPLATE_NAME, FString("GameSession"), EOnlineDataAdvertisementType::DontAdvertise);
- ShooterHostSettings->Set(SETTING_GAMEMODE, FString("TeamDeathmatch"), EOnlineDataAdvertisementType::ViaOnlineService);
- ShooterHostSettings->Set(SETTING_MAPNAME, GetWorld()->GetMapName(), EOnlineDataAdvertisementType::ViaOnlineService);
##### EOnlineDataAdvertisementType
服务器数据广播方式:
```
/** Don't advertise via the online service or QoS data */
DontAdvertise,
/** Advertise via the server ping data only */
ViaPingOnly,
/** Advertise via the online service only */
ViaOnlineService,
/** Advertise via the online service and via the ping data */
ViaOnlineServiceAndPing
```
## 其他代码参考
在Online模块下有个OnlineFramework文件夹。