Search code examples
c++unreal-engine4

UE4: output game frames to file


This applies to Unreal Engine 4.9

Each frame, I'd like to save it to file (overwriting the existing one).

For a simple D3D11 demo that I have made, I've been able to do this with:

void Engine::CaptureFrame(D3DX11_IMAGE_FILE_FORMAT format, const LPCTSTR fileName){

    backbuffer->GetResource(&resource);
    ID3D11Texture2D* texture;
    HRESULT hResult = resource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID*>(&texture));
    D3DX11SaveTextureToFile(deviceContext, texture, format, fileName);

    //Clean up
    texture->Release();
    resource->Release();
}

I have done this with OpenGL, too, using a library called "FreeImage".

I don't want to modify UE4 source and am looking for a way to "hook" into UE4 and "capture" the frame.

I've managed to do this with Open Broadcasting Software, but I'd like to do this natively, within UE4, either via c++ or blueprints.

I am also aware that I can do this from the editor, but that is not what I want to do. I want to do this at run-time, automatically.

How can I go about this? Is it even possible?


Solution

  • For anyone else who wants to do this, you have a couple of options.

    Option A

    If you don't care how many screenshot files are generated and don't care what they are called, then you can use a blueprint:

    enter image description here

    The images will be output to \Unreal Projects\ProjectName\Saved\Screenshots\Windows where ProjectName is the name of your project.

    Option B

    If you don't want multiple screenshot files and need control over where they go, then you can do this via code:

    //Called every frame
    void AMyDempActor::Tick(float DeltaTime)
    {
        Super::Tick(DeltaTime);
    
        FString filename = "test.png";
        FScreenshotRequest::RequestScreenshot(filename, false, false);
    
    }
    

    Here is the documentation on FScreenshotRequest