UE5 C++ TPS開發 學習記錄(八

這一次到了p19

完善了UI和寫了創建房間

MultiPlayerSessionSubsystem.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Subsystems/GameInstanceSubsystem.h" #include "Interfaces/OnlineSessionInterface.h" #include "MultiPlayerSessionSubsystem.generated.h" /** * */ UCLASS() class UMultiPlayerSessionSubsystem : public UGameInstanceSubsystem { GENERATED_BODY() public: UMultiPlayerSessionSubsystem(); /* * 這里是一個句柄,讓菜單類可以訪問Subsystem */ void CreateSession(int32 NumPublicConnections,FString MatchType); void FindSession(int32 MaxSearchResults); void JoinSession(const FOnlineSessionSearchResult& SessionResults); void DestroySession(); void StartSession(); protected: /* * 這里是委托 */ void CreateSessionComplete(FName SessionName, bool bWasSuccessful); void FindSessionsComplete(bool bWasSuccessful); void JoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result); void DestroySessionComplete(FName SessionName, bool bWasSuccessful); void StartSessionComplete(FName SessionName, bool bWasSuccessful); private: IOnlineSessionPtr OnlineSessionInterface; TSharedPtr<FOnlineSessionSettings> LastSessionSettings; /* * 添加OnlineSession委托到列表內 * 我們的游玩系統內部回調綁定到這里 * */ FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate; FDelegateHandle CreateSessionCompleteDelegate_Handle; FOnFindSessionsCompleteDelegate FindSessionsCompleteDelegate; FDelegateHandle FindSessionsCompleteDelegate_Handle; FOnJoinSessionCompleteDelegate JoinSessionCompleteDelegate; FDelegateHandle JoinSessionCompleteDelegate_Handle; FOnDestroySessionCompleteDelegate DestroySessionCompleteDelegate; FDelegateHandle DestroySessionCompleteDelegate_Handle; FOnStartSessionCompleteDelegate StartSessionCompleteDelegate; FDelegateHandle StartSessionCompleteDelegate_Handle; };

MultiPlayerSessionSubsystem.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "MultiPlayerSessionSubsystem.h" #include "OnlineSessionSettings.h" #include "OnlineSubsystem.h" UMultiPlayerSessionSubsystem::UMultiPlayerSessionSubsystem(): CreateSessionCompleteDelegate(FOnCreateSessionCompleteDelegate::CreateUObject(this,&ThisClass::CreateSessionComplete)), FindSessionsCompleteDelegate(FOnFindSessionsCompleteDelegate::CreateUObject(this,&ThisClass::FindSessionsComplete)), JoinSessionCompleteDelegate(FOnJoinSessionCompleteDelegate::CreateUObject(this,&ThisClass::JoinSessionComplete)), DestroySessionCompleteDelegate(FOnDestroySessionCompleteDelegate::CreateUObject(this,&ThisClass::DestroySessionComplete)), StartSessionCompleteDelegate(FOnStartSessionCompleteDelegate::CreateUObject(this,&ThisClass::StartSessionComplete)) { IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get(); if(OnlineSubsystem) { OnlineSessionInterface = OnlineSubsystem->GetSessionInterface(); } } void UMultiPlayerSessionSubsystem::CreateSession(int32 NumPublicConnections, FString MatchType) { if(!OnlineSessionInterface.IsValid()) { return; } auto ExistingSession = OnlineSessionInterface->GetNamedSession(NAME_GameSession); if(ExistingSession != nullptr) { OnlineSessionInterface->DestroySession(NAME_GameSession); } //存放創建委托 CreateSessionCompleteDelegate_Handle = OnlineSessionInterface->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate); LastSessionSettings = MakeShareable(new FOnlineSessionSettings()); LastSessionSettings->bIsLANMatch = IOnlineSubsystem::Get()->GetSubsystemName() == "NULL" ? true:false; //最多4人 LastSessionSettings->NumPublicConnections =NumPublicConnections; //允許其他玩家加入 LastSessionSettings->bAllowJoinInProgress = true; //允許好友加入 LastSessionSettings->bAllowJoinViaPresence = true; //線上公開 LastSessionSettings->bShouldAdvertise = true; //顯示用戶狀態 LastSessionSettings->bUsesPresence = true; //使用第三方 LastSessionSettings->bUseLobbiesIfAvailable = true; LastSessionSettings->Set(FName("MatchType"),MatchType,EOnlineDataAdvertisementType::ViaOnlineServiceAndPing); const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController(); if(OnlineSessionInterface->CreateSession(*LocalPlayer->GetPreferredUniqueNetId(), NAME_GameSession , *LastSessionSettings)) { OnlineSessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate_Handle); } } void UMultiPlayerSessionSubsystem::FindSession(int32 MaxSearchResults) { } void UMultiPlayerSessionSubsystem::JoinSession(const FOnlineSessionSearchResult& SessionResults) { } void UMultiPlayerSessionSubsystem::DestroySession() { } void UMultiPlayerSessionSubsystem::StartSession() { } void UMultiPlayerSessionSubsystem::CreateSessionComplete(FName SessionName, bool bWasSuccessful) { } void UMultiPlayerSessionSubsystem::FindSessionsComplete(bool bWasSuccessful) { } void UMultiPlayerSessionSubsystem::JoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result) { } void UMultiPlayerSessionSubsystem::DestroySessionComplete(FName SessionName, bool bWasSuccessful) { } void UMultiPlayerSessionSubsystem::StartSessionComplete(FName SessionName, bool bWasSuccessful) { }

BaseMenu.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Blueprint/UserWidget.h" #include "BaseMenu.generated.h" /** * */ class UButton; UCLASS() class MULTIPLAYERSESSION_API UBaseMenu : public UUserWidget { GENERATED_BODY() protected: virtual bool Initialize() override; virtual void NativeDestruct(); public: UFUNCTION(BlueprintCallable) void MenuSetup(int SetupNumPublicConnections = 4,FString SetupMatchType = TEXT("FreeForAll")); void _DebugLog(FColor DisplayColor, const FString& DebugMessage); private: //將指針與按鈕綁定起來,這里的綁定必須要指針變量名稱和UMG的名稱完全一樣 UPROPERTY(meta=(BindWidget)) UButton* HostButton; UPROPERTY(meta=(BindWidget)) UButton* JoinButton; UFUNCTION() void HostButtonClicked(); UFUNCTION() void JoinButtonClincked(); UFUNCTION() void MenuTearDown(); class UMultiPlayerSessionSubsystem* MultiPlayerSessionSubsystem; int32 NumPublicConnections{4}; FString MatchType{TEXT("FreeForAll")}; };

BaseMenu.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "BaseMenu.h" #include "MultiPlayerSession/Public/MultiPlayerSessionSubsystem.h" #include "Components/Button.h" bool UBaseMenu::Initialize() { if(!Super::Initialize()) { return false; } if(HostButton) { HostButton->OnClicked.AddDynamic(this,&UBaseMenu::HostButtonClicked); } if(JoinButton) { JoinButton->OnClicked.AddDynamic(this,&UBaseMenu::JoinButtonClincked); } return true; } void UBaseMenu::MenuSetup(int SetupNumPublicConnections ,FString SetupMatchType) { NumPublicConnections = SetupNumPublicConnections; MatchType = SetupMatchType; AddToViewport(); SetVisibility(ESlateVisibility::Visible); bIsFocusable = true; UWorld* World = GetWorld(); if(World) { APlayerController* PlayerController = World->GetFirstPlayerController(); if(PlayerController) { FInputModeUIOnly InputModeUIOnly; InputModeUIOnly.SetWidgetToFocus(TakeWidget()); InputModeUIOnly.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock); PlayerController->SetInputMode(InputModeUIOnly); PlayerController->SetShowMouseCursor(true); } } UGameInstance* GameInstance = GetGameInstance(); if(GameInstance) { MultiPlayerSessionSubsystem = GameInstance->GetSubsystem<UMultiPlayerSessionSubsystem>(); } } void UBaseMenu::_DebugLog(FColor DisplayColor, const FString& DebugMessage) { if(GEngine) { GEngine->AddOnScreenDebugMessage(-1,15.f,DisplayColor,DebugMessage); } } void UBaseMenu::HostButtonClicked() { _DebugLog(FColor::Blue,FString::Printf(TEXT("Host Button Click"))); if(MultiPlayerSessionSubsystem) { MultiPlayerSessionSubsystem->CreateSession(NumPublicConnections,MatchType); } _DebugLog(FColor::Blue,FString::Printf(TEXT("Create Session Success"))); UWorld* World = GetWorld(); if(World) { World->ServerTravel("/Game/ThirdPerson/Maps/Lobby?listen"); } } void UBaseMenu::JoinButtonClincked() { _DebugLog(FColor::Blue,FString::Printf(TEXT("Join Button Click"))); } void UBaseMenu::MenuTearDown() { UWorld* World = GetWorld(); RemoveFromParent(); APlayerController* PlayerController = World->GetFirstPlayerController(); FInputModeGameAndUI InputModeGameAndUI; PlayerController->SetInputMode(InputModeGameAndUI); PlayerController->SetShowMouseCursor(false); }

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/718481.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/718481.shtml
英文地址,請注明出處:http://en.pswp.cn/news/718481.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python基礎-基本數據類型深入-2.2

目錄 集合 集合的定義 集合操作 集合的內建函數 集合與內置函數 集合練習-1 集合練習-2 集合練習-3 集合 集合的定義 學習關于 Python 集的所有內容&#xff1b;如何創建它們、添加或刪除其中的元素&#xff0c;以及在 Python 中對集合執行的所有操作。 Python 中的集…

掌握Go語言:探索Go語言中的變量,靈活性與可讀性的完美結合(4)

想要編寫簡潔高效的Go語言代碼嗎&#xff1f;掌握變量的使用是關鍵&#xff01;Go語言的變量聲明方式多樣&#xff0c;包括var關鍵字和短變量聲明&#xff0c;同時支持類型推斷&#xff0c;讓代碼更加清晰易讀。 變量聲明方式 在Go語言中&#xff0c;變量的聲明方式有兩種&am…

少兒編程 中國電子學會C++等級考試一級歷年真題答案解析【持續更新 已更新82題】

C 等級考試一級考綱說明 一、能力目標 通過本級考核的學生&#xff0c;能對 C 語言有基本的了解&#xff0c;會使用順序結構、選擇結構、循環結構編寫程序&#xff0c;具體用計算思維的方式解決簡單的問題。 二、考核目標 考核內容是根據軟件開發所需要的技能和知識&#x…

Leetcode 3071. Minimum Operations to Write the Letter Y on a Grid

Leetcode 3071. Minimum Operations to Write the Letter Y on a Grid 1. 解題思路2. 代碼實現 題目鏈接&#xff1a;3071. Minimum Operations to Write the Letter Y on a Grid 1. 解題思路 這一題思路上也是比較直接的&#xff0c;就是首先找到這個Y字符&#xff0c;然后…

單詞規律00

題目鏈接 單詞規律 題目描述 注意點 pattern只包含小寫英文字母s只包含小寫英文字母和 ’ ’s不包含任何前導或尾隨對空格s中每個單詞都被 單個空格 分隔 解答思路 本題與上一次同構字符串類似&#xff0c;思路可以參照同構字符串 代碼 class Solution {public boolean …

工作流/任務卸載相關開源論文分享

decima-sim 概述&#xff1a; 圖神經網絡強化學習處理多工作流 用的spark的仿真環境&#xff0c;mit的論文&#xff0c;價值很高&#xff0c;高被引&#xff1a;663倉庫地址&#xff1a;https://github.com/hongzimao/decima-sim論文&#xff1a;https://web.mit.edu/decima/co…

企業財務規劃的未來:自動化智能化如何推動全面預算管理

隨著自動化和智能化對企業的影響日益明顯&#xff0c;了解和接受那些有可能改變企業財務規劃的技術變得愈發重要。新興技術是推動企業增長和業務生產的中堅力量。作為企業財務專業人員&#xff0c;熟悉技術能夠幫助他們了解企業的未來價值&#xff0c;從而更好的領導團隊。數智…

springboot支持的常用日志框架介紹

日志系統是計算機系統中用于記錄和跟蹤事件、錯誤和信息的軟件組件。在軟件開發和維護過程中&#xff0c;日志系統起著至關重要的作用。它可以幫助開發人員了解軟件的運行情況&#xff0c;快速定位和解決問題。本文將從以下幾個方面介紹日志系統&#xff1a;日志系統概述、Spri…

Mybatis plus拓展功能-枚舉處理器

目錄 1 前言 2 使用方法 2.1 在application.yml中添加配置 2.2 定義枚舉類 2.3 在實體類和賦值時中使用 1 前言 在我們的開發過程中&#xff0c;常常需要用一些數字來表示狀態。比如說&#xff1a;1-正常&#xff0c;0-凍結。然而這樣并不能做到見名知意&#xff0c;特別是…

HTML最強入門學習筆記+GitHub小項目源碼

HTML學習筆記 GitHub項目鏈接: 點我跳轉GitHub 本博客采用markdown編寫&#xff0c;上面這個鏈接跳轉就是采用了html的<a></a>的代碼設計的跳轉提示~ 1.創建文件可以使用 ! 在VSCode中進行快速補全從而生成一整個HTML結構 HTML組成 <!DOCTYPE html><htm…

vscode——遠端配置及一些問題解決

vscode——遠端配置 安裝Remote -SSH插件配置config本地變化一些問題缺失核心關閉vscode自動更新 嘗試寫入管道不存在hostname -I 查出來的ip連不上 我們之前大概了解了vscode的本地設置&#xff0c;我們之前提過&#xff0c;vscode是一款編輯器&#xff0c;在文本編輯方面有著…

Windows安裝Neo4j數據庫教程(3.X版本)

安裝java的jdk&#xff08;jdk1.8僅支持Neo4j 3.X版本&#xff09;去 Index of /doc/neo4j/ 下載目標版本的Windows zip安裝包將安裝包解壓到任意目錄&#xff0c;并記住解壓后帶版本號的文件夾路徑添加系統環境變量&#xff0c;變量名&#xff1a;NEO4J_HOME&#xff0c;變量值…

程序員的金三銀四求職寶典:如何在關鍵時期脫穎而出?

個人主頁&#xff1a;17_Kevin-CSDN博客 隨著春天的腳步漸近&#xff0c;程序員們的求職熱潮也隨之而來。在這個被稱為“金三銀四”的招聘季&#xff0c;如何從眾多求職者中脫穎而出&#xff0c;成為了許多程序員關注的焦點。本文將為你提供一份全面的求職寶典&#xff0c;助你…

前端架構: 腳手架通用框架封裝之CommonJS和ESM混合開發兼容解決(教程五)

CommonJS 和 ESModule 混合開發 接上文&#xff0c;仍舊在 abc-cli 項目中參考&#xff1a;https://blog.csdn.net/Tyro_java/article/details/136433159現在要在腳手架項目中安裝 chalk 依賴&#xff0c;因為在 abc-cli 項目幾乎都是 CommonJS的實現而 chalk 這個依賴源碼是基…

徹底剖析激光-視覺-IMU-GPS融合SLAM算法:理論推導、代碼講解和實戰

自主導航是機器人與自動駕駛的核心功能&#xff0c;而SLAM技術是實現自主導航的前提與關鍵。現有的機器人與自動駕駛車輛往往會安裝激光雷達&#xff0c;相機&#xff0c;IMU&#xff0c;GPS等多種模態的傳感器&#xff0c;而且已有許多優秀的激光SLAM與視覺SLAM算法。但是每種…

獲取wifi內容信息(僅供學習使用,勿作他用)

文章目錄 背景代碼實現槽點槽點 1槽點2總結背景 上一篇文章我們講解學習了密碼字典生成,下面我們來看一下如何獲取wifi信息 代碼實現 下面代碼,附帶注釋,可以很清晰的看明白每一行代碼具體是什么含義,通俗易通的變量定義,已經函數調用。如有任何不明白的地方可以隨時聯…

曲線生成 | 圖解Dubins曲線生成原理(附ROS C++/Python/Matlab仿真)

目錄 0 專欄介紹1 什么是Dubins曲線&#xff1f;2 Dubins曲線原理2.1 坐標變換2.2 單步運動公式2.3 曲線模式 3 Dubins曲線生成算法4 仿真實現4.1 ROS C實現4.2 Python實現4.3 Matlab實現 0 專欄介紹 &#x1f525;附C/Python/Matlab全套代碼&#x1f525;課程設計、畢業設計、…

c語言:轉移表的實現

Hello,寶子們&#xff01;今天我們來模擬實現一下我們生活中的應用最頻繁的工具&#xff1a;計算器&#xff0c;實現計算器有三種方式。 廢話不多說&#xff0c;直接上代碼&#xff0c;計算器的一般實現&#xff1a; #include <stdio.h> int add(int a, int b)//加法函數…

Foxmail快捷鍵設置問題

當快捷鍵設置錯誤時不會生效&#xff0c;原來的快捷鍵仍有效&#xff0c;即使禁用快捷鍵功能&#xff0c;原先快捷鍵仍有效。正確的快捷鍵&#xff1a; 1. 不能是空&#xff08;NULL&#xff09; 2. 應該設置按鍵值只有一個的鍵盤按鈕。

力扣字符串篇

以下解題思路來自代碼隨想錄以及官方題解。 文章目錄 344.反轉字符串541.反轉字符串||151.反轉字符串中的單詞28.找出字符串中第一個匹配項的下標459.重復的字符串 344.反轉字符串 編寫一個函數&#xff0c;其作用是將輸入的字符串反轉過來。輸入字符串以字符數組 s 的形式給…