parent
a51acf9d02
commit
f051f29506
@ -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;
|
||||
};
|
Loading…
Reference in new issue