You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.5 KiB
102 lines
3.5 KiB
2 years ago
|
// 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);
|
||
|
}
|