当前位置:   article > 正文

UE5 C++教程 (二、在线子系统)_c++调用ue5

c++调用ue5

目录

        1.配置在线子系统(OnlineSubsystem)

        2.委托(Delegate)

        3.联机


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

1.配置在线子系统(OnlineSubsystem)

OnlineSubsystem定义并注册特定服务的模块到引擎,定义在Engine.ini配置文件中,初始化时系统将尝试加载配置在其中的在线服务模块。

示例:配置steam的在线子系统

  1. [/Script/Engine.GameEngine]
  2. +NetDriverDefinitions=(DefName="GameNetDriver",DriverClassName="OnlineSubsystemSteam.SteamNetDriver",DriverClassNameFallback="OnlineSubsystemUtils.IpNetDriver")
  3. [OnlineSubsystem]
  4. DefaultPlatformService=Steam
  5. [OnlineSubsystemSteam]
  6. bEnabled=true
  7. SteamDevAppId=480
  8. bInitServerOnClient=true
  9. [/Script/OnlineSubsystemSteam.SteamNetDriver]
  10. NetConnectionClassName="OnlineSubsystemSteam.SteamNetConnection"
  11. [/Script/OnlineSubsystemUtils.IpNetDriver]
  12. NetServerMaxTickRate=60

配置在线子系统后,新建C++类,搜索GameInstanceSubsystem并创建

5e2fb0196c454f1681a26b153a77a5a4.png

在这里即可进行相关代码编写

f3a2cdbcc9314b9f8846944426192d90.png

2.委托(Delegate)

在线子系统将委托用于所有操作,用于处理与各种在线服务的异步通信(网络连接速度、服务器延迟和后端服务的运行带来通信延迟)。

官方对委托的描述如下

委托可以以通用、类型安全的方式调用 C++ 对象的成员函数。委托可以动态绑定到任意对象的成员函数,在将来调用对象上的函数,即使调用者不知道对象的类型。代表可以安全地复制。也可以按值传递,但不推荐这样做,因为这个进程会在堆上分配内存;尽可能通过引用传递代表。引擎支持三种类型的委托:

  • Single

  • Multicast

  • Dynamic (UObject, serializable)

委托通过绑定函数指针,实现一对一(Single),一对多(Multicast),动态(Dynamic)信息传递。

委托使用流程如下

使用steam的相关委托 :

(1)在类中定义委托变量和委托句柄(steam的委托接口)

  1. FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate;
  2. FDelegateHandle CreateSessionCompleteDelegateHandle;
  3. FOnFindSessionsCompleteDelegate FindSessionsCompleteDelegate;
  4. FDelegateHandle FindSessionsCompleteDelegateHandle;
  5. FOnJoinSessionCompleteDelegate JoinSessionCompleteDelegate;
  6. FDelegateHandle JoinSessionCompleteDelegateHandle;
  7. FOnDestroySessionCompleteDelegate DestroySessionCompleteDelegate;
  8. FDelegateHandle DestroySessionCompleteDelegateHandle;
  9. FOnStartSessionCompleteDelegate StartSessionCompleteDelegate;
  10. FDelegateHandle StartSessionCompleteDelegateHandle;

(2)在类中声明委托回调函数,并在cpp中定义:

  1. void OnCreateSessionComplete(FName SessionName, bool bWasSuccessful);
  2. void OnFindSessionsComplete(bool bWasSuccessful);
  3. void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
  4. void OnDestroySessionComplete(FName SessionName, bool bWasSuccessful);
  5. void OnStartSessionComplete(FName SessionName, bool bWasSuccessful);

(3)将委托加入委托列表(以steam创建游戏会话为例)

CreateSessionCompleteDelegateHandle = SessionInterface->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate);

(4)在构造函数中绑定委托回调函数

  1. UMultiplayerSessionsSubsystem::UMultiplayerSessionsSubsystem():
  2. CreateSessionCompleteDelegate(FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnCreateSessionComplete)),
  3. FindSessionsCompleteDelegate(FOnFindSessionsCompleteDelegate::CreateUObject(this, &ThisClass::OnFindSessionsComplete)),
  4. JoinSessionCompleteDelegate(FOnJoinSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnJoinSessionComplete)),
  5. DestroySessionCompleteDelegate(FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnDestroySessionComplete)),
  6. StartSessionCompleteDelegate(FOnStartSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnStartSessionComplete))
  7. {
  8. IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get();
  9. if (Subsystem)
  10. {
  11. SessionInterface = Subsystem->GetSessionInterface();
  12. }
  13. }

(5)回收句柄,删除委托

SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle);

定义自己的委托:

(1)在类中声明委托类型

  1. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiplayerOnCreateSessionComplete, bool, bWasSuccessful);
  2. DECLARE_MULTICAST_DELEGATE_TwoParams(FMultiplayerOnFindSessionsComplete, const TArray<FOnlineSessionSearchResult>& SessionResults, bool bWasSuccessful);
  3. DECLARE_MULTICAST_DELEGATE_OneParam(FMultiplayerOnJoinSessionComplete, EOnJoinSessionCompleteResult::Type Result);
  4. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiplayerOnDestroySessionComplete, bool, bWasSuccessful);
  5. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiplayerOnStartSessionComplete, bool, bWasSuccessful);

(2)在类中定义委托变量

  1. FMultiplayerOnCreateSessionComplete MultiplayerOnCreateSessionComplete;
  2. FMultiplayerOnFindSessionsComplete MultiplayerOnFindSessionsComplete;
  3. FMultiplayerOnJoinSessionComplete MultiplayerOnJoinSessionComplete;
  4. FMultiplayerOnDestroySessionComplete MultiplayerOnDestroySessionComplete;
  5. FMultiplayerOnStartSessionComplete MultiplayerOnStartSessionComplete;

(3)在被传递的类中声明委托回调函数,并在cpp中定义(参数要和传递的委托类型对应)

  1. UFUNCTION()
  2. void OnCreateSession(bool bWasSuccessful);
  3. void OnFindSessions(const TArray<FOnlineSessionSearchResult>& SessionResults, bool bWasSuccessful);
  4. void OnJoinSession(EOnJoinSessionCompleteResult::Type Result);
  5. UFUNCTION()
  6. void OnDestroySession(bool bWasSuccessful);
  7. UFUNCTION()
  8. void OnStartSession(bool bWasSuccessful);

 createsession,destorysession,startsession为动态传播,需要用UFUNCTION()宏

(3)绑定委托回调函数

  1. MultiplayerSessionsSubsystem->MultiplayerOnCreateSessionComplete.AddDynamic(this,&ThisClass::OnCreateSession);
  2. MultiplayerSessionsSubsystem->MultiplayerOnFindSessionsComplete.AddUObject(this, &ThisClass::OnFindSessions);
  3. MultiplayerSessionsSubsystem->MultiplayerOnJoinSessionComplete.AddUObject(this, &ThisClass::OnJoinSession);
  4. MultiplayerSessionsSubsystem->MultiplayerOnDestroySessionComplete.AddDynamic(this, &ThisClass::OnDestroySession);
  5. MultiplayerSessionsSubsystem->MultiplayerOnStartSessionComplete.AddDynamic(this, &ThisClass::OnStartSession);

3.联机

运用在线子系统并使用一些委托,便可以使用对应的服务进行联机了

05b3a7013fda4a519d0cf07e4a049f25.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/107165
推荐阅读
相关标签
  

闽ICP备14008679号