Yes it’s possible but not fun extending GameInstance
in C++ and Blueprint.
Here’s my implementation using GameViewportClient
and does not need any Blueprint nodes (using @AmphDev’s snippet).
First create a new C++ class inheriting GameViewportClient
(do this through the Editor). Let’s call it: GameViewportClientExtended
. Here is the code:
GameViewportClientExtended.h
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameViewportClient.h"
#include "GameViewportClientExtended.generated.h"
/**
* OnScreenshotCaptured delegate added to fix Screenshots not writing to file in Shipping builds.
*/
UCLASS()
class MYGAME_API UGameViewportClientExtended : public UGameViewportClient
{
GENERATED_BODY()
virtual void Init(struct FWorldContext& WorldContext, UGameInstance* OwningGameInstance, bool bCreateNewAudioDevice = true) override;
void OnScreenshotCaptured(int32 InWidth, int32 InHeight, const TArray<FColor>& inColors);
};
GameViewportClientExtended.cpp
#include "GameViewportClientExtended.h"
#include "ImageUtils.h"
void UGameViewportClientExtended::Init(struct FWorldContext& WorldContext, UGameInstance* OwningGameInstance, bool bCreateNewAudioDevice)
{
Super::Init(WorldContext, OwningGameInstance, bCreateNewAudioDevice);
Super::OnScreenshotCaptured().AddUObject(this, &UGameViewportClientExtended::OnScreenshotCaptured);
}
void UGameViewportClientExtended::OnScreenshotCaptured(int32 InWidth, int32 InHeight, const TArray<FColor>& inColors)
{
UE_LOG(LogViewport, Log, TEXT("GameViewportClientExtended::OnScreenshotCaptured"));
FString Path = "";
FScreenshotRequest::CreateViewportScreenShotFilename(Path);
TArray64<uint8> CompressedBitmap;
FImageUtils::PNGCompressImageArray(InWidth, InHeight, TArrayView64<const FColor>(inColors.GetData(), inColors.Num()), CompressedBitmap);
FFileHelper::SaveArrayToFile(CompressedBitmap, *Path);
}
Once the class has been re-compiled: re-open the Editor > Project Settings > update the Game Viewport Client Class to the newly created one:
Any Screenshot related console event should now write files in Shipping builds.