UE5 C++ TPS开发 学习记录(10

2024-03-12 07:04
文章标签 c++ 学习 开发 记录 tps ue5

本文主要是介绍UE5 C++ TPS开发 学习记录(10,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

p22

这节课把创建,查找,加入游戏房间的菜单类,以及插件内的系统类给补完了.说实话这节课有点绕,因为需要一直使用委托进行传值,先由菜单类Menu向系统类Subsystem发送函数传值请求,然后监听Subsystem的委托回调,同时系统类Subsystem向Session的工具发送请求,监听回调,再返回给Menu类进行传值.

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" /** * 这里是我们自定义的委托 */ DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiPlayerOnCreateSessionComplete ,bool, bWasSuccessful); DECLARE_MULTICAST_DELEGATE_TwoParams(FMultiPlayerOnFindSessionComplete, const TArray<FOnlineSessionSearchResult>& SessionSearchResult , 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); 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(); /* * 这是给菜单类做的回调委托 */ FMultiPlayerOnCreateSessionComplete MultiPlayerOnCreateSessionComplete; FMultiPlayerOnFindSessionComplete MultiPlayerOnFindSessionComplete; FMultiPlayerOnJoinSessionComplete MultiPlayerOnJoinSessionComplete; FMultiPlayerOnDestroySessionComplete MultiPlayerOnDestroySessionComplete; FMultiPlayerOnStartSessionComplete MultiPlayerOnStartSessionComplete; 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: //查找会话设置 TSharedPtr<FOnlineSessionSearch> LastSessionSearch; 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" #include "Online/OnlineSessionNames.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); /*在这里广播*/ MultiPlayerOnCreateSessionComplete.Broadcast(false); } } void UMultiPlayerSessionSubsystem::FindSession(int32 MaxSearchResults) { if(!OnlineSessionInterface.IsValid()) { return; } //这里将我们的委托设置成SessionInterface的句柄使用 FindSessionsCompleteDelegate_Handle = OnlineSessionInterface->AddOnFindSessionsCompleteDelegate_Handle(FindSessionsCompleteDelegate); //添加查询委托 OnlineSessionInterface->AddOnFindSessionsCompleteDelegate_Handle(FindSessionsCompleteDelegate); //设置查找 LastSessionSearch = MakeShareable(new FOnlineSessionSearch()); LastSessionSearch->MaxSearchResults = 10000; LastSessionSearch->bIsLanQuery= LastSessionSettings->bIsLANMatch = IOnlineSubsystem::Get()->GetSubsystemName() == "NULL" ? true:false; //设置查询设置 LastSessionSearch->QuerySettings.Set(SEARCH_PRESENCE,true,EOnlineComparisonOp::Equals); //获得本地的第一个玩家 const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController(); //使用本地的第一个玩家的URL和查找设置进行查找,当查找为false或者空的时候删除句柄 if(!OnlineSessionInterface->FindSessions(*LocalPlayer->GetPreferredUniqueNetId(),LastSessionSearch.ToSharedRef())) { //删除掉句柄 OnlineSessionInterface->ClearOnFindSessionsCompleteDelegate_Handle(FindSessionsCompleteDelegate_Handle); MultiPlayerOnFindSessionComplete.Broadcast(TArray<FOnlineSessionSearchResult>(),false); } } void UMultiPlayerSessionSubsystem::JoinSession(const FOnlineSessionSearchResult& SessionResults) { if(!OnlineSessionInterface.IsValid()) { MultiPlayerOnJoinSessionComplete.Broadcast(EOnJoinSessionCompleteResult::UnknownError); return; } JoinSessionCompleteDelegate_Handle = OnlineSessionInterface->AddOnJoinSessionCompleteDelegate_Handle(JoinSessionCompleteDelegate); //获得本地的第一个玩家 const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController(); //调用JoinSession,并且加入的时候调用OnJoinSessionCompleteDelegate这个委托 if(!OnlineSessionInterface->JoinSession(*LocalPlayer->GetPreferredUniqueNetId(),NAME_GameSession,SessionResults)) { MultiPlayerOnJoinSessionComplete.Broadcast(EOnJoinSessionCompleteResult::UnknownError); } } void UMultiPlayerSessionSubsystem::DestroySession() { } void UMultiPlayerSessionSubsystem::StartSession() { } void UMultiPlayerSessionSubsystem::CreateSessionComplete(FName SessionName, bool bWasSuccessful) { /*当成功创建房间的时候,删除创建房间的句柄*/ if(OnlineSessionInterface) { OnlineSessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate_Handle); } MultiPlayerOnCreateSessionComplete.Broadcast(bWasSuccessful); } void UMultiPlayerSessionSubsystem::FindSessionsComplete(bool bWasSuccessful) { if(OnlineSessionInterface) { OnlineSessionInterface->ClearOnFindSessionsCompleteDelegate_Handle(FindSessionsCompleteDelegate_Handle); } if(LastSessionSearch->SearchResults.Num()<=0) { //找到了玩家后删除掉句柄 OnlineSessionInterface->ClearOnFindSessionsCompleteDelegate_Handle(FindSessionsCompleteDelegate_Handle); return; } MultiPlayerOnFindSessionComplete.Broadcast(LastSessionSearch->SearchResults,bWasSuccessful); } void UMultiPlayerSessionSubsystem::JoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result) { if(OnlineSessionInterface) { OnlineSessionInterface->ClearOnJoinSessionCompleteDelegate_Handle(JoinSessionCompleteDelegate_Handle); } MultiPlayerOnJoinSessionComplete.Broadcast(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 "Interfaces/OnlineSessionInterface.h" #include "BaseMenu.generated.h" /** * */ class UButton; UCLASS() class MULTIPLAYERSESSION_API UBaseMenu : public UUserWidget { GENERATED_BODY() protected: virtual bool Initialize() override; virtual void NativeDestruct() override; /*这里创建用来对应MultiPlayerOnCreateSessionComplete的函数*/ UFUNCTION() void OnCreateSession(bool bWasSuccessful); void OnFindSession(const TArray<FOnlineSessionSearchResult>& SessionSearchResult , bool bWasSuccessful); void OnJoinSession(EOnJoinSessionCompleteResult::Type Result); UFUNCTION() void OnDestroySession(bool bWasSuccessful); UFUNCTION() void OnStartSession(bool bWasSuccessful); 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")}; };

.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "BaseMenu.h" #include "OnlineSessionSettings.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::NativeDestruct() { Super::NativeDestruct(); } void UBaseMenu::OnCreateSession(bool bWasSuccessful) { if(bWasSuccessful) { _DebugLog(FColor::Green,TEXT("CreateSession Successful")); } UWorld* World = GetWorld(); if(World) { World->ServerTravel("/Game/ThirdPerson/Maps/Lobby?listen"); } } void UBaseMenu::OnFindSession(const TArray<FOnlineSessionSearchResult>& SessionSearchResult, bool bWasSuccessful) { if(MultiPlayerSessionSubsystem==nullptr) { return; } for(auto Result : SessionSearchResult) { FString SettingValue; Result.Session.SessionSettings.Get(FName("MatchType"),SettingValue); if(SettingValue == MatchType) { MultiPlayerSessionSubsystem->JoinSession(Result); return; } } } void UBaseMenu::OnJoinSession(EOnJoinSessionCompleteResult::Type Result) { //加入游戏 IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get(); if(OnlineSubsystem) { IOnlineSessionPtr OnlineSessionInterface = OnlineSubsystem->GetSessionInterface(); if(OnlineSessionInterface.IsValid()) { FString Address; // if(OnlineSessionInterface->GetResolvedConnectString(NAME_GameSession,Address)) { APlayerController* PlayerController = GetGameInstance()->GetFirstLocalPlayerController(); if(PlayerController) { PlayerController->ClientTravel(Address,ETravelType::TRAVEL_Absolute); } } } } } void UBaseMenu::OnDestroySession(bool bWasSuccessful) { } void UBaseMenu::OnStartSession(bool bWasSuccessful) { } 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>(); } if(MultiPlayerSessionSubsystem) { MultiPlayerSessionSubsystem->MultiPlayerOnCreateSessionComplete.AddDynamic(this,&ThisClass::OnCreateSession); /*当委托是静态的时候需要使用 AddUObject */ MultiPlayerSessionSubsystem->MultiPlayerOnFindSessionComplete.AddUObject(this,&ThisClass::OnFindSession); MultiPlayerSessionSubsystem->MultiPlayerOnJoinSessionComplete.AddUObject(this,&ThisClass::OnJoinSession); MultiPlayerSessionSubsystem->MultiPlayerOnDestroySessionComplete.AddDynamic(this,&ThisClass::OnDestroySession); MultiPlayerSessionSubsystem->MultiPlayerOnStartSessionComplete.AddDynamic(this,&ThisClass::OnStartSession); } } void UBaseMenu::_DebugLog(FColor DisplayColor, const FString& DebugMessage) { if(GEngine) { GEngine->AddOnScreenDebugMessage(-1,15.f,DisplayColor,DebugMessage); } } void UBaseMenu::HostButtonClicked() { if(MultiPlayerSessionSubsystem) { MultiPlayerSessionSubsystem->CreateSession(NumPublicConnections,MatchType); } _DebugLog(FColor::Blue,FString::Printf(TEXT("Create Session Success"))); } void UBaseMenu::JoinButtonClincked() { //点击加入按钮 if(MultiPlayerSessionSubsystem) { MultiPlayerSessionSubsystem->FindSession(10000); } } void UBaseMenu::MenuTearDown() { UWorld* World = GetWorld(); RemoveFromParent(); APlayerController* PlayerController = World->GetFirstPlayerController(); FInputModeGameAndUI InputModeGameAndUI; PlayerController->SetInputMode(InputModeGameAndUI); PlayerController->SetShowMouseCursor(false); }

这篇关于UE5 C++ TPS开发 学习记录(10的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/800461

相关文章

关于C++中的虚拟继承的一些总结(虚拟继承,覆盖,派生,隐藏)

1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下: class A class B1:public virtual A; class B2:pu

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

51单片机学习记录———定时器

文章目录 前言一、定时器介绍二、STC89C52定时器资源三、定时器框图四、定时器模式五、定时器相关寄存器六、定时器练习 前言 一个学习嵌入式的小白~ 有问题评论区或私信指出~ 提示:以下是本篇文章正文内容,下面案例可供参考 一、定时器介绍 定时器介绍:51单片机的定时器属于单片机的内部资源,其电路的连接和运转均在单片机内部完成。 定时器作用: 1.用于计数系统,可

C++的模板(八):子系统

平常所见的大部分模板代码,模板所传的参数类型,到了模板里面,或实例化为对象,或嵌入模板内部结构中,或在模板内又派生了子类。不管怎样,最终他们在模板内,直接或间接,都实例化成对象了。 但这不是唯一的用法。试想一下。如果在模板内限制调用参数类型的构造函数会发生什么?参数类的对象在模板内无法构造。他们只能从模板的成员函数传入。模板不保存这些对象或者只保存他们的指针。因为构造函数被分离,这些指针在模板外

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

[word] word设置上标快捷键 #学习方法#其他#媒体

word设置上标快捷键 办公中,少不了使用word,这个是大家必备的软件,今天给大家分享word设置上标快捷键,希望在办公中能帮到您! 1、添加上标 在录入一些公式,或者是化学产品时,需要添加上标内容,按下快捷键Ctrl+shift++就能将需要的内容设置为上标符号。 word设置上标快捷键的方法就是以上内容了,需要的小伙伴都可以试一试呢!

AssetBundle学习笔记

AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。 目录 1.定义: 2.AssetBundle的生成: 1)设置AssetBundle包的属性——通过编辑器界面 补充:分组策略 2)调用引擎接口API

C++工程编译链接错误汇总VisualStudio

目录 一些小的知识点 make工具 可以使用windows下的事件查看器崩溃的地方 dumpbin工具查看dll是32位还是64位的 _MSC_VER .cc 和.cpp 【VC++目录中的包含目录】 vs 【C/C++常规中的附加包含目录】——头文件所在目录如何怎么添加,添加了以后搜索头文件就会到这些个路径下搜索了 include<> 和 include"" WinMain 和

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

大学湖北中医药大学法医学试题及答案,分享几个实用搜题和学习工具 #微信#学习方法#职场发展

今天分享拥有拍照搜题、文字搜题、语音搜题、多重搜题等搜题模式,可以快速查找问题解析,加深对题目答案的理解。 1.快练题 这是一个网站 找题的网站海量题库,在线搜题,快速刷题~为您提供百万优质题库,直接搜索题库名称,支持多种刷题模式:顺序练习、语音听题、本地搜题、顺序阅读、模拟考试、组卷考试、赶快下载吧! 2.彩虹搜题 这是个老公众号了 支持手写输入,截图搜题,详细步骤,解题必备