Started implementing session matchmaking. Session creation and update implemented.

pull/6/head
eyakm1 2 years ago
parent a51acf9d02
commit f051f29506

@ -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")
+FunctionRedirects=(OldName="/Script/TurnBasedTutorial.MyPlayerController.MoveTropper",NewName="/Script/TurnBasedTutorial.MyPlayerController.MoveTrooper")
[OnlineSubsystem]
DefaultPlatformService=Null

@ -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<FOnlineSessionSettings> 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);
}

@ -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<FOnlineSessionSettings> LastSessionSettings;
FOnUpdateSessionCompleteDelegate UpdateSessionCompleteDelegate;
FDelegateHandle UpdateSessionCompleteDelegateHandle;
};

@ -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
}

@ -1,6 +1,6 @@
{
"FileVersion": 3,
"EngineAssociation": "{B4FE6467-4579-3D84-B22A-558A3D607596}",
"EngineAssociation": "4.27",
"Category": "",
"Description": "",
"Modules": [

Loading…
Cancel
Save