赞
踩
目录
The Online Subsystem and its interfaces provide a common way to access the functionality of online services such as Steam, Xbox Live, Facebook, and so on.
在线子系统(OnlineSubsystem)及其接口提供了一种访问在线服务功能的通用方式,有了它,我们就可以访问诸如Steam、Xbox Live、Facebook 等的在线服务,开发人员只需对每个支持的服务进行配置调整即可开发在多个平台上发布,支持多个在线服务的游戏。
在线子系统官方文档:Online Subsystem in Unreal Engine | Unreal Engine 5.0 Documentation
OnlineSubsystem定义并注册特定服务的模块到引擎,定义在Engine.ini配置文件中,初始化时系统将尝试加载配置在其中的在线服务模块。
示例:配置steam的在线子系统
- [/Script/Engine.GameEngine]
- +NetDriverDefinitions=(DefName="GameNetDriver",DriverClassName="OnlineSubsystemSteam.SteamNetDriver",DriverClassNameFallback="OnlineSubsystemUtils.IpNetDriver")
-
- [OnlineSubsystem]
- DefaultPlatformService=Steam
-
- [OnlineSubsystemSteam]
- bEnabled=true
- SteamDevAppId=480
- bInitServerOnClient=true
-
- [/Script/OnlineSubsystemSteam.SteamNetDriver]
- NetConnectionClassName="OnlineSubsystemSteam.SteamNetConnection"
-
- [/Script/OnlineSubsystemUtils.IpNetDriver]
- NetServerMaxTickRate=60
配置在线子系统后,新建C++类,搜索GameInstanceSubsystem并创建
在这里即可进行相关代码编写
在线子系统将委托用于所有操作,用于处理与各种在线服务的异步通信(网络连接速度、服务器延迟和后端服务的运行带来通信延迟)。
官方对委托的描述如下
委托可以以通用、类型安全的方式调用 C++ 对象的成员函数。委托可以动态绑定到任意对象的成员函数,在将来调用对象上的函数,即使调用者不知道对象的类型。代表可以安全地复制。也可以按值传递,但不推荐这样做,因为这个进程会在堆上分配内存;尽可能通过引用传递代表。引擎支持三种类型的委托:
Single
Multicast
- Dynamic (UObject, serializable)
委托通过绑定函数指针,实现一对一(Single),一对多(Multicast),动态(Dynamic)信息传递。
委托使用流程如下
使用steam的相关委托 :
(1)在类中定义委托变量和委托句柄(steam的委托接口)
- FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate;
- FDelegateHandle CreateSessionCompleteDelegateHandle;
-
- FOnFindSessionsCompleteDelegate FindSessionsCompleteDelegate;
- FDelegateHandle FindSessionsCompleteDelegateHandle;
-
- FOnJoinSessionCompleteDelegate JoinSessionCompleteDelegate;
- FDelegateHandle JoinSessionCompleteDelegateHandle;
-
- FOnDestroySessionCompleteDelegate DestroySessionCompleteDelegate;
- FDelegateHandle DestroySessionCompleteDelegateHandle;
-
- FOnStartSessionCompleteDelegate StartSessionCompleteDelegate;
- FDelegateHandle StartSessionCompleteDelegateHandle;
(2)在类中声明委托回调函数,并在cpp中定义:
- void OnCreateSessionComplete(FName SessionName, bool bWasSuccessful);
- void OnFindSessionsComplete(bool bWasSuccessful);
- void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
- void OnDestroySessionComplete(FName SessionName, bool bWasSuccessful);
- void OnStartSessionComplete(FName SessionName, bool bWasSuccessful);
(3)将委托加入委托列表(以steam创建游戏会话为例)
CreateSessionCompleteDelegateHandle = SessionInterface->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate);
(4)在构造函数中绑定委托回调函数
- UMultiplayerSessionsSubsystem::UMultiplayerSessionsSubsystem():
- CreateSessionCompleteDelegate(FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnCreateSessionComplete)),
- FindSessionsCompleteDelegate(FOnFindSessionsCompleteDelegate::CreateUObject(this, &ThisClass::OnFindSessionsComplete)),
- JoinSessionCompleteDelegate(FOnJoinSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnJoinSessionComplete)),
- DestroySessionCompleteDelegate(FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnDestroySessionComplete)),
- StartSessionCompleteDelegate(FOnStartSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnStartSessionComplete))
- {
- IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get();
- if (Subsystem)
- {
- SessionInterface = Subsystem->GetSessionInterface();
- }
- }
(5)回收句柄,删除委托
SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle);
定义自己的委托:
(1)在类中声明委托类型
- DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiplayerOnCreateSessionComplete, bool, bWasSuccessful);
- DECLARE_MULTICAST_DELEGATE_TwoParams(FMultiplayerOnFindSessionsComplete, const TArray<FOnlineSessionSearchResult>& SessionResults, bool bWasSuccessful);
- DECLARE_MULTICAST_DELEGATE_OneParam(FMultiplayerOnJoinSessionComplete, EOnJoinSessionCompleteResult::Type Result);
- DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiplayerOnDestroySessionComplete, bool, bWasSuccessful);
- DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiplayerOnStartSessionComplete, bool, bWasSuccessful);
(2)在类中定义委托变量
- FMultiplayerOnCreateSessionComplete MultiplayerOnCreateSessionComplete;
- FMultiplayerOnFindSessionsComplete MultiplayerOnFindSessionsComplete;
- FMultiplayerOnJoinSessionComplete MultiplayerOnJoinSessionComplete;
- FMultiplayerOnDestroySessionComplete MultiplayerOnDestroySessionComplete;
- FMultiplayerOnStartSessionComplete MultiplayerOnStartSessionComplete;
(3)在被传递的类中声明委托回调函数,并在cpp中定义(参数要和传递的委托类型对应)
- UFUNCTION()
- void OnCreateSession(bool bWasSuccessful);
- void OnFindSessions(const TArray<FOnlineSessionSearchResult>& SessionResults, bool bWasSuccessful);
- void OnJoinSession(EOnJoinSessionCompleteResult::Type Result);
- UFUNCTION()
- void OnDestroySession(bool bWasSuccessful);
- UFUNCTION()
- void OnStartSession(bool bWasSuccessful);
createsession,destorysession,startsession为动态传播,需要用UFUNCTION()宏
(3)绑定委托回调函数
- MultiplayerSessionsSubsystem->MultiplayerOnCreateSessionComplete.AddDynamic(this,&ThisClass::OnCreateSession);
- MultiplayerSessionsSubsystem->MultiplayerOnFindSessionsComplete.AddUObject(this, &ThisClass::OnFindSessions);
- MultiplayerSessionsSubsystem->MultiplayerOnJoinSessionComplete.AddUObject(this, &ThisClass::OnJoinSession);
- MultiplayerSessionsSubsystem->MultiplayerOnDestroySessionComplete.AddDynamic(this, &ThisClass::OnDestroySession);
- MultiplayerSessionsSubsystem->MultiplayerOnStartSessionComplete.AddDynamic(this, &ThisClass::OnStartSession);
运用在线子系统并使用一些委托,便可以使用对应的服务进行联机了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。