TPS入门DAY02 服务器篇

打印 上一主题 下一主题

主题 1836|帖子 1836|积分 5508

1.创建空白插件



2.导入在线子体系以及在线steam子体系库

`MultiplayerSessions.uplugin`

MultiplayerSessions.Build.cs



3.创建游戏实例以及初始化会话创建流程


创建会话须要的函数,委托,委托绑定的回调,在线子体系接口绑定某一个委托的控制其绑定的生命周期的句柄
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Subsystems/GameInstanceSubsystem.h"
  5. #include "Interfaces/OnlineSessionInterface.h"  // 包含委托定义
  6. #include "MultiplayerSessionsSubsystem.generated.h"
  7. /**
  8. *
  9. */
  10. UCLASS()
  11. class MULTIPLAYERSESSIONS_API UMultiplayerSessionsSubsystem : public UGameInstanceSubsystem
  12. {
  13.         GENERATED_BODY()
  14.        
  15. public:
  16.         UMultiplayerSessionsSubsystem();
  17.         /* 会话有关的函数 */
  18.         void CreateSession(int32 NumPublicConnections, FString MatchType);
  19.         void FindSessions(int32 MaxSearchResults);
  20.         void JoinSession(const FOnlineSessionSearchResult& SearchResult);
  21.         void DestroySession();
  22.         void StartSession();
  23. protected:
  24.         /* 委托的回调函数 */
  25.         void OnCreateSessionComplete(FName SessionName, bool bWasSuccessful);
  26.         void OnFindSessionsComplete(bool bWasSuccessful);
  27.         void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
  28.         void OnDestroySessionComplete(FName SessionName, bool bWasSuccessful);
  29.         void OnStartSessionComplete(FName SessionName, bool bWasSuccessful);
  30. private:
  31.         /* 进入服务器的凭证,通过该凭证来加入同一个服务器 */
  32.         IOnlineSessionPtr OnlineSessionInterface;
  33.         /* 在线子系统的委托 */
  34.         FOnCreateSessionCompleteDelegate OnCreateSessionCompleteDelegate;
  35.         FOnFindSessionsCompleteDelegate OnFindSessionsCompleteDelegate;
  36.         FOnJoinSessionCompleteDelegate OnJoinSessionCompleteDelegate;
  37.         FOnDestroySessionCompleteDelegate OnDestroySessionCompleteDelegate;
  38.         FOnStartSessionCompleteDelegate OnStartSessionCompleteDelegate;
  39.         /* 在线子系统委托对应的句柄 */
  40.         /* 例:当创建会话时,在线子系统会绑定创建完会话的委托,该函数会返回创建会话完成委托的句柄,来管理委托的绑定生命周期*/
  41.         FDelegateHandle OnCreateSessionCompleteDelegateHandle;
  42.         FDelegateHandle OnFindSessionsCompleteDelegateHandle;
  43.         FDelegateHandle OnJoinSessionCompleteDelegateHandle;
  44.         FDelegateHandle OnDestroySessionCompleteDelegateHandle;
  45.         FDelegateHandle OnStartSessionCompleteDelegateHandle;
  46. private:
  47.         /* 绑定委托的回调 */
  48.         void BindCallBack();
  49. };
复制代码
实现代码

4.创建体系菜单


编译报错
  1. 1>[3/4] Link [x64] UnrealEditor-MultiplayerSessions.dll (0:00.78 at +0:14)
  2. 1>  正在创建库 H:\UEProject\5.3\MultiPlayer\MenuSystem\Plugins\MultiplayerSessions\Intermediate\Build\Win64\x64\UnrealEditor\Development\MultiplayerSessions\UnrealEditor-MultiplayerSessions.sup.lib 和对象 H:\UEProject\5.3\MultiPlayer\MenuSystem\Plugins\MultiplayerSessions\Intermediate\Build\Win64\x64\UnrealEditor\Development\MultiplayerSessions\UnrealEditor-MultiplayerSessions.sup.exp
  3. 1>Module.MultiplayerSessions.cpp.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __cdecl UWidget::FFieldNotificationClassDescriptor::FFieldNotificationClassDescriptor(void)" (__imp_??0FFieldNotificationClassDescriptor@UWidget@@QEAA@XZ),函数 "public: virtual struct UE::FieldNotification::IClassDescriptor const & __cdecl UWidget::GetFieldNotificationDescriptor(void)const " (?GetFieldNotificationDescriptor@UWidget@@UEBAAEBUIClassDescriptor@FieldNotification@UE@@XZ) 中引用了该符号
  4. 1>Module.MultiplayerSessions.cpp.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: virtual __cdecl UWidget::FFieldNotificationClassDescriptor::~FFieldNotificationClassDescriptor(void)" (__imp_??1FFieldNotificationClassDescriptor@UWidget@@UEAA@XZ),函数 "void __cdecl `public: virtual struct UE::FieldNotification::IClassDescriptor const & __cdecl UWidget::GetFieldNotificationDescriptor(void)const '::`2'::`dynamic atexit destructor for 'Instance''(void)" (??__FInstance@?1??GetFieldNotificationDescriptor@UWidget@@UEBAAEBUIClassDescriptor@FieldNotification@UE@@XZ@YAXXZ) 中引用了该符号
  5. 1>Module.MultiplayerSessions.cpp.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) private: static class UClass * __cdecl UUserWidget::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UUserWidget@@CAPEAVUClass@@XZ),函数 "public: static class UClass * __cdecl UUserWidget::StaticClass(void)" (?StaticClass@UUserWidget@@SAPEAVUClass@@XZ) 中引用了该符号
  6. 1>  已定义且可能匹配的符号上的提示:
复制代码
无法分析的外部符号,别看报错了这么多,先看他比比的什么
起首UWidget这个不认识,其次UUserWidget这个类也不认识
比比半天,是库缺失了,链接一下就好了,直接搜刮Widget,在办理方案下

没搜到,不要紧,大概这个关键字就不是头文件定名,在搜刮UserWidget


直接将UMG添加到build.cs下即可

实在就是VS下的筛选器的名字

生成成功

设置UI界面配置

  1. #include "CoreMinimal.h"
  2. #include "Blueprint/UserWidget.h"
  3. #include "Menu.generated.h"
  4. /**
  5. *
  6. */
  7. UCLASS()
  8. class MULTIPLAYERSESSIONS_API UMenu : public UUserWidget
  9. {
  10.         GENERATED_BODY()
  11.        
  12. public:
  13.         UFUNCTION(BlueprintCallable)
  14.         void MenuSetup(int32 _NumberOfPublicConnections, FString _TypeOfMatch, FString _LobbyPath);
  15. public:
  16.         /* 最大连接数 */
  17.         UPROPERTY(BlueprintReadWrite)
  18.         int32 NumberOfPublicConnections;
  19.         UPROPERTY(BlueprintReadWrite)
  20.         FString MatchType;
  21.         /* 大厅路径 */
  22.         UPROPERTY(BlueprintReadWrite)
  23.         FString LobbyPath;
  24. };
复制代码
  1. #include "Menu.h"
  2. void UMenu::MenuSetup(int32 _NumberOfPublicConnections, FString _TypeOfMatch, FString _LobbyPath)
  3. {
  4.         LobbyPath = FString::Printf(TEXT("%s?listen"), *_LobbyPath);
  5.         NumberOfPublicConnections = _NumberOfPublicConnections;
  6.         MatchType = _TypeOfMatch;
  7.         AddToViewport();
  8.         SetVisibility(ESlateVisibility::Visible);
  9.         bIsFocusable = true;                                                                                                                                // 允许接收输入事件
  10.         UWorld* World = GetWorld();
  11.         if (World)
  12.         {
  13.                 APlayerController* PlayerController = World->GetFirstPlayerController();
  14.                 if (PlayerController)
  15.                 {
  16.                         FInputModeUIOnly InputMode;
  17.                         InputMode.SetWidgetToFocus(TakeWidget());                                                                        // 聚焦当前控件
  18.                         InputMode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);                // 不限制鼠标
  19.                         PlayerController->SetInputMode(InputMode);                                                                        // 切换为纯UI输入模式
  20.                         PlayerController->bShowMouseCursor = true;                                                                        // 显示鼠标光标
  21.                 }
  22.         }
  23. }
复制代码
创建控件蓝图

以创建的C++Menu类为父类创建




在关卡中显示





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表