Skip to content

Instantly share code, notes, and snippets.

@gamerxl
Last active January 17, 2024 10:32
Show Gist options
  • Save gamerxl/1bd4db4f342b0eccbe908b2430bdc77b to your computer and use it in GitHub Desktop.
Save gamerxl/1bd4db4f342b0eccbe908b2430bdc77b to your computer and use it in GitHub Desktop.
A blueprint library for unreal engine with functions available in blueprints to start a customized play session in the editor. It can be for instance used in editor widget utilities for quick actions... like starting a standalone session (with client and server) with a different map used for the client but the current map opened in the editor le…
#include "PlayFromBlueprintLibrary.h"
#if WITH_EDITOR
#include "LevelEditor.h"
void UPlayFromBlueprintLibrary::PlayFrom(
FString LevelName,
const EPlayNetMode PlayNetMode,
const int32 NumberOfClients,
const bool bLaunchServer)
{
ULevelEditorPlaySettings* LevelEditorPlaySettings = GetMutableDefault<ULevelEditorPlaySettings>();
LevelEditorPlaySettings->bLaunchSeparateServer = bLaunchServer;
LevelEditorPlaySettings->SetPlayNetMode(PlayNetMode);
LevelEditorPlaySettings->SetPlayNumberOfClients(NumberOfClients);
LevelEditorPlaySettings->SetRunUnderOneProcess(true);
FLevelEditorModule& LevelEditorModule = FModuleManager::Get().GetModuleChecked<FLevelEditorModule>(TEXT("LevelEditor"));
FRequestPlaySessionParams RequestPlaySessionParams;
RequestPlaySessionParams.EditorPlaySettings = LevelEditorPlaySettings;
RequestPlaySessionParams.GlobalMapOverride = LevelName;
// If only one client is expected then use the editor level viewport as game viewport.
if (NumberOfClients == 1)
{
RequestPlaySessionParams.DestinationSlateViewport = LevelEditorModule.GetFirstActiveViewport();
}
GEditor->RequestPlaySession(RequestPlaySessionParams);
}
#endif
#pragma once
#if WITH_EDITOR
#include "CoreMinimal.h"
#include "Settings/LevelEditorPlaySettings.h"
#include "PlayFromBlueprintLibrary.generated.h"
UCLASS()
class UPlayFromBlueprintLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
// Starts a play session with a specified level name.
// Optionally the PlayNetMode and the number of client windows can be specified.
// If the number of clients is just one then the editor level viewport is the play window for the first client.
// If a server is required it can be optionally specified, then a separate server will be started.
UFUNCTION(BlueprintCallable)
static void PlayFrom(
FString LevelName,
const EPlayNetMode PlayNetMode = PIE_Client,
const int32 NumberOfClients = 1,
const bool bLaunchServer = true);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment