From f051f295069756d548ae006a92a61f9e094fbc3e Mon Sep 17 00:00:00 2001 From: eyakm1 Date: Sat, 20 May 2023 00:44:54 +0300 Subject: [PATCH] Started implementing session matchmaking. Session creation and update implemented. --- Config/DefaultEngine.ini | 5 +- .../MyGameInstanceSubsystem.cpp | 101 ++++++++++++++++++ .../MyGameInstanceSubsystem.h | 44 ++++++++ .../TurnBasedTutorial.Build.cs | 2 +- TurnBased.uproject | 2 +- 5 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 Source/TurnBasedTutorial/MyGameInstanceSubsystem.cpp create mode 100644 Source/TurnBasedTutorial/MyGameInstanceSubsystem.h diff --git a/Config/DefaultEngine.ini b/Config/DefaultEngine.ini index ae6b8ed..9fe9839 100644 --- a/Config/DefaultEngine.ini +++ b/Config/DefaultEngine.ini @@ -30,4 +30,7 @@ AppliedDefaultGraphicsPerformance=Maximum [CoreRedirects] +PropertyRedirects=(OldName="/Script/TurnBasedTutorial.Trooper.OnPlayersSide",NewName="/Script/TurnBasedTutorial.Trooper.bOnPlayersSide") +FunctionRedirects=(OldName="/Script/TurnBasedTutorial.MyPlayerController.MoveHero",NewName="/Script/TurnBasedTutorial.MyPlayerController.MoveTropper") -+FunctionRedirects=(OldName="/Script/TurnBasedTutorial.MyPlayerController.MoveTropper",NewName="/Script/TurnBasedTutorial.MyPlayerController.MoveTrooper") \ No newline at end of file ++FunctionRedirects=(OldName="/Script/TurnBasedTutorial.MyPlayerController.MoveTropper",NewName="/Script/TurnBasedTutorial.MyPlayerController.MoveTrooper") + +[OnlineSubsystem] +DefaultPlatformService=Null diff --git a/Source/TurnBasedTutorial/MyGameInstanceSubsystem.cpp b/Source/TurnBasedTutorial/MyGameInstanceSubsystem.cpp new file mode 100644 index 0000000..a7c3930 --- /dev/null +++ b/Source/TurnBasedTutorial/MyGameInstanceSubsystem.cpp @@ -0,0 +1,101 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#include "MyGameInstanceSubsystem.h" + +#include "OnlineSubsystemUtils.h" + +UMyGameInstanceSubsystem::UMyGameInstanceSubsystem() : CreateSessionCompleteDelegate( + FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnCreateSessionCompleted)) +{ +} + + +void UMyGameInstanceSubsystem::CreateSession(int32 NumPublicConnections, bool bIsLANMatch) +{ + const IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(GetWorld()); + if (!SessionInterface.IsValid()) + { + OnCreateSessionCompleteEvent.Broadcast(false); + return; + } + + LastSessionSettings = MakeShareable(new FOnlineSessionSettings()); + LastSessionSettings->NumPrivateConnections = 0; + LastSessionSettings->NumPublicConnections = NumPublicConnections; + LastSessionSettings->bAllowInvites = true; + LastSessionSettings->bAllowJoinInProgress = true; + LastSessionSettings->bAllowJoinViaPresence = true; + LastSessionSettings->bAllowJoinViaPresenceFriendsOnly = true; + LastSessionSettings->bIsDedicated = false; + LastSessionSettings->bUsesPresence = true; + LastSessionSettings->bIsLANMatch = bIsLANMatch; + LastSessionSettings->bShouldAdvertise = true; + + LastSessionSettings->Set(SETTING_MAPNAME, FString("Your Level Name"), + EOnlineDataAdvertisementType::ViaOnlineService); + + CreateSessionCompleteDelegateHandle = SessionInterface->AddOnCreateSessionCompleteDelegate_Handle( + CreateSessionCompleteDelegate); + + const ULocalPlayer* localPlayer = GetWorld()->GetFirstLocalPlayerFromController(); + if (!SessionInterface->CreateSession(*localPlayer->GetPreferredUniqueNetId(), NAME_GameSession, + *LastSessionSettings)) + { + SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle); + + OnCreateSessionCompleteEvent.Broadcast(false); + } +} + +void UMyGameInstanceSubsystem::OnCreateSessionCompleted(FName SessionName, bool bSuccessful) +{ + const IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(GetWorld()); + if (SessionInterface) + { + SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle); + } + + OnCreateSessionCompleteEvent.Broadcast(bSuccessful); +} + +void UMyGameInstanceSubsystem::UpdateSession() +{ + const IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(GetWorld()); + if (!SessionInterface.IsValid()) + { + OnCreateSessionCompleteEvent.Broadcast(false); + return; + } + + const TSharedPtr UpdatedSessionSettings = MakeShareable( + new FOnlineSessionSettings(*LastSessionSettings)); + + // Here we can insert any changes we want + UpdatedSessionSettings->Set(SETTING_MAPNAME, FString("Updated Level Name"), + EOnlineDataAdvertisementType::ViaOnlineService); + + UpdateSessionCompleteDelegateHandle = + SessionInterface->AddOnUpdateSessionCompleteDelegate_Handle(UpdateSessionCompleteDelegate); + + if (!SessionInterface->UpdateSession(NAME_GameSession, *UpdatedSessionSettings)) + { + SessionInterface->ClearOnUpdateSessionCompleteDelegate_Handle(UpdateSessionCompleteDelegateHandle); + + OnUpdateSessionCompleteEvent.Broadcast(false); + } + else + { + LastSessionSettings = UpdatedSessionSettings; + } +} + +void UMyGameInstanceSubsystem::OnUpdateSessionCompleted(FName SessionName, bool bSuccessful) +{ + const IOnlineSessionPtr sessionInterface = Online::GetSessionInterface(GetWorld()); + if (sessionInterface) + { + sessionInterface->ClearOnUpdateSessionCompleteDelegate_Handle(UpdateSessionCompleteDelegateHandle); + } + + OnUpdateSessionCompleteEvent.Broadcast(bSuccessful); +} diff --git a/Source/TurnBasedTutorial/MyGameInstanceSubsystem.h b/Source/TurnBasedTutorial/MyGameInstanceSubsystem.h new file mode 100644 index 0000000..3fd413d --- /dev/null +++ b/Source/TurnBasedTutorial/MyGameInstanceSubsystem.h @@ -0,0 +1,44 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" + +#include "Interfaces/OnlineSessionInterface.h" +#include "Subsystems/GameInstanceSubsystem.h" +#include "MyGameInstanceSubsystem.generated.h" + +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyOnCreateSessionComplete, bool, Successful); +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyOnUpdateSessionComplete, bool, Successful); + +/** + * + */ +UCLASS() +class TURNBASEDTUTORIAL_API UMyGameInstanceSubsystem : public UGameInstanceSubsystem +{ + GENERATED_BODY() + +public: + UMyGameInstanceSubsystem(); + + void CreateSession(int32 NumPublicConnections, bool bIsLANMatch); + + void UpdateSession(); + + FMyOnCreateSessionComplete OnCreateSessionCompleteEvent; + FMyOnUpdateSessionComplete OnUpdateSessionCompleteEvent; + +protected: + void OnCreateSessionCompleted(FName SessionName, bool bSuccessful); + + void OnUpdateSessionCompleted(FName SessionName, bool bSuccessful); + +private: + FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate; + FDelegateHandle CreateSessionCompleteDelegateHandle; + TSharedPtr LastSessionSettings; + + FOnUpdateSessionCompleteDelegate UpdateSessionCompleteDelegate; + FDelegateHandle UpdateSessionCompleteDelegateHandle; +}; diff --git a/Source/TurnBasedTutorial/TurnBasedTutorial.Build.cs b/Source/TurnBasedTutorial/TurnBasedTutorial.Build.cs index 7f99bb8..de07b92 100644 --- a/Source/TurnBasedTutorial/TurnBasedTutorial.Build.cs +++ b/Source/TurnBasedTutorial/TurnBasedTutorial.Build.cs @@ -16,7 +16,7 @@ public class TurnBasedTutorial : ModuleRules // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" }); // Uncomment if you are using online features - // PrivateDependencyModuleNames.Add("OnlineSubsystem"); + PrivateDependencyModuleNames.AddRange(new string[] {"OnlineSubsystem", "OnlineSubsystemUtils"}); // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true } diff --git a/TurnBased.uproject b/TurnBased.uproject index fd638db..8485d6b 100644 --- a/TurnBased.uproject +++ b/TurnBased.uproject @@ -1,6 +1,6 @@ { "FileVersion": 3, - "EngineAssociation": "{B4FE6467-4579-3D84-B22A-558A3D607596}", + "EngineAssociation": "4.27", "Category": "", "Description": "", "Modules": [