Only Call ReadPixels inside of GameViewport:: Draw
I had this issue during the Beta, you can only call ReadPixels inside of the Draw function of Viewport class!
So this means you need to use your own custom GameViewport class and override Draw()
For your entertainment is my example of using ReadPixels to take a screenshot!
Anywhere in your code base you can set the bool to true to trigger a screenshot on the next Draw call.
Enjoy!
//Draw
void USolusViewportClient::Draw(FViewport * Viewport, FCanvas * SceneCanvas)
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//this line is reaalllly important
Super::Draw(Viewport, SceneCanvas);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//UE_LOG(Victory,Warning,TEXT("VICTORY GAME VIEWPORT Ticking!"));
//Take Screen shot?
if (VictoryDoScreenShot)
{
VictoryDoScreenShot = false;
VictoryTakeScreenShot();
}
}
So inside of VictoryTakeScreenShot() is where you’d make the call to ReadPixels()
But this setup allows you to trigger a screenshot from anywhere!
.h
's what the .h looks like.
```
#pragma once
#include "SolusViewportClient.generated.h"
UCLASS()
class USolusViewportClient : public UGameViewportClient
{
GENERATED_UCLASS_BODY()
//set this from anywhere to trigger screenshot inside of Draw()
public:
//Triggers ScreenShot
bool VictoryDoScreenShot;
//protected because must only be run from Draw() or causes crash
protected:
void VictoryTakeScreenShot();
virtual void Draw(FViewport* Viewport,FCanvas* SceneCanvas) override;
};
```
DefaultEngine.ini Config
You have to tell UE4 to use your custom viewport class:
[/Script/Engine.Engine]
GameViewportClientClassName=/Script/Solus.SolusViewportClient