mirror of
https://github.com/mordentral/AdvancedSessionsPlugin.git
synced 2025-10-23 08:24:18 +00:00
issue-31: Add node to start a game session
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "BlueprintDataDefinitions.h"
|
||||||
|
#include "StartSessionCallbackProxyAdvanced.generated.h"
|
||||||
|
|
||||||
|
UCLASS(MinimalAPI)
|
||||||
|
class UStartSessionCallbackProxyAdvanced : public UOnlineBlueprintCallProxyBase
|
||||||
|
{
|
||||||
|
GENERATED_UCLASS_BODY()
|
||||||
|
// Called when the session starts successfully
|
||||||
|
UPROPERTY(BlueprintAssignable)
|
||||||
|
FEmptyOnlineDelegate OnSuccess;
|
||||||
|
|
||||||
|
// Called when there is an error starting the session
|
||||||
|
UPROPERTY(BlueprintAssignable)
|
||||||
|
FEmptyOnlineDelegate OnFailure;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a session with the default online subsystem. The session needs to be previously created by calling the "CreateAdvancedSession" node.
|
||||||
|
* @param WorldContextObject
|
||||||
|
*/
|
||||||
|
UFUNCTION(
|
||||||
|
BlueprintCallable
|
||||||
|
, meta=(BlueprintInternalUseOnly = "true", WorldContext="WorldContextObject")
|
||||||
|
, Category = "Online|AdvancedSessions"
|
||||||
|
)
|
||||||
|
static UStartSessionCallbackProxyAdvanced* StartAdvancedSession(const UObject* WorldContextObject);
|
||||||
|
|
||||||
|
// UOnlineBlueprintCallProxyBase interface
|
||||||
|
virtual void Activate() override;
|
||||||
|
// End of UOnlineBlueprintCallProxyBase interface
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Internal callback when session start completes
|
||||||
|
void OnStartCompleted(FName SessionName, bool bWasSuccessful);
|
||||||
|
|
||||||
|
// The delegate executed by the online subsystem
|
||||||
|
FOnStartSessionCompleteDelegate StartCompleteDelegate;
|
||||||
|
|
||||||
|
// Handles to the registered delegates above
|
||||||
|
FDelegateHandle StartCompleteDelegateHandle;
|
||||||
|
|
||||||
|
// The world context object in which this call is taking place
|
||||||
|
const UObject* WorldContextObject;
|
||||||
|
};
|
@@ -0,0 +1,62 @@
|
|||||||
|
#include "StartSessionCallbackProxyAdvanced.h"
|
||||||
|
|
||||||
|
UStartSessionCallbackProxyAdvanced::UStartSessionCallbackProxyAdvanced(const FObjectInitializer& ObjectInitializer)
|
||||||
|
: Super(ObjectInitializer)
|
||||||
|
, StartCompleteDelegate(FOnStartSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnStartCompleted))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
UStartSessionCallbackProxyAdvanced* UStartSessionCallbackProxyAdvanced::StartAdvancedSession(
|
||||||
|
const UObject* WorldContextObject)
|
||||||
|
{
|
||||||
|
UStartSessionCallbackProxyAdvanced* Proxy = NewObject<UStartSessionCallbackProxyAdvanced>();
|
||||||
|
Proxy->WorldContextObject = WorldContextObject;
|
||||||
|
return Proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UStartSessionCallbackProxyAdvanced::Activate()
|
||||||
|
{
|
||||||
|
const FOnlineSubsystemBPCallHelperAdvanced Helper(
|
||||||
|
TEXT("StartSession"),
|
||||||
|
GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull));
|
||||||
|
|
||||||
|
if (Helper.OnlineSub != nullptr)
|
||||||
|
{
|
||||||
|
const auto Sessions = Helper.OnlineSub->GetSessionInterface();
|
||||||
|
if (Sessions.IsValid())
|
||||||
|
{
|
||||||
|
StartCompleteDelegateHandle = Sessions->AddOnStartSessionCompleteDelegate_Handle(StartCompleteDelegate);
|
||||||
|
Sessions->StartSession(NAME_GameSession);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FFrame::KismetExecutionMessage(TEXT("Sessions not supported by Online Subsystem"), ELogVerbosity::Warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fail immediately
|
||||||
|
OnFailure.Broadcast();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UStartSessionCallbackProxyAdvanced::OnStartCompleted(FName SessionName, bool bWasSuccessful)
|
||||||
|
{
|
||||||
|
const FOnlineSubsystemBPCallHelperAdvanced Helper(
|
||||||
|
TEXT("StartSessionCallback"),
|
||||||
|
GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull));
|
||||||
|
|
||||||
|
if (Helper.OnlineSub != nullptr)
|
||||||
|
{
|
||||||
|
const auto Sessions = Helper.OnlineSub->GetSessionInterface();
|
||||||
|
if (Sessions.IsValid())
|
||||||
|
{
|
||||||
|
Sessions->ClearOnStartSessionCompleteDelegate_Handle(StartCompleteDelegateHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bWasSuccessful)
|
||||||
|
{
|
||||||
|
OnSuccess.Broadcast();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
OnFailure.Broadcast();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user