Taking in-game screenshots with UI in Unreal Engine 4
September 8, 2020
Note: Before you get started please note that this method will NOT work in shipping builds as Cheat Manager is stripped from them. For a method which does work in Shipping builds, check out my newer post: Taking In-Game Screenshots With UI in Shipping builds of UE4 Games
Taking screenshots in Unreal is super easy, you just open the console and type "HighResShot". But what if you wanted more?
What if you wanted to use the in-game camera with "ToggleDebugCamera" AND capture any UMG widgets on the UI? Well, we can do that but first, we need to use some C++.
If you don't have a C++ project already, go ahead and make one or convert your current one with File->New C++ Class.
Once that's done, you'll need to create a new Cheat Manager class.
The Cheat Manager class. You'll notice two cheat managers in the screenshot, not to worry, one is mine.
You want to select Cheat Manager and select Create Class, wait for Unreal to compile and then we can get into it. We're using the Cheat Manager as it's designed for this sort of thing.
Open your Cheat Manager header file and below your GENERATED_BODY()
, insert:
cpp
1UFUNCTION(Exec, BlueprintCallable) 2static void TakeScreenShot();
Then in your cpp file:
cpp
1void UMyCheatManager::TakeScreenShot() 2{ 3 FScreenshotRequest * request = new FScreenshotRequest(); 4 request->RequestScreenshot("ScreenShot", true, false); 5 if(request->IsScreenshotRequested()) 6 { 7 FString ResponseMessage = TEXT("Screenshot saved to: " + request->GetFilename()); 8 FWindowsPlatformMisc::MessageBoxExt(EAppMsgType::Ok, *ResponseMessage, TEXT("Screenshot Taken")); 9 } 10}
Now just recompile, play your game, toggle the debug camera and type into the console "TakeScreenShot". Now you have a screenshot with UI!
But what does it all do?
Good question!
cpp
1UFUNCTION(Exec, BlueprintCallable)
Exec = Allows it to be executed from the console.
BlueprintCallable = Allows me to call it from a blueprint node if I wish.
cpp
1FScreenshotRequest * request = new FScreenshotRequest();
As you can probably guess: this creates a new Screenshot Request object. This is how the engine knows when you want a screenshot.
cpp
1request->RequestScreenshot("ScreenShot", true, false);
From that request object, you call RequestScreenshot()
to actually make the request and the engine will handle the rest.
BUT we can see three arguments there, what are they? Easy, but let's break it down:
cpp
1RequestScreenshot( 2 "ScreenShot", //The output filename 3 true, //This one toggles to show the UI, the real star of this post. 4 false //Toggles the filename suffix, more on that below. 5);
So the filename suffix...you thought that was gonna be lower didn't you? I guess I pulled a sneaky on ya.
If the Filename Suffix toggle was set to true your screenshots will be appended with 00000, 00001, 00002, 00003 and so on...
Toggling it to false disables that as I use my own system of appending the current date. On or off? It's up to you. On is the default for the built-in screenshot tools.
That's all you need to get going but you'll notice a little bit of sneaky code in there:
cpp
1if(request->IsScreenshotRequested()) 2 { 3 FString ResponseMessage = TEXT("Screenshot saved to: " + request->GetFilename()); 4 FWindowsPlatformMisc::MessageBoxExt(EAppMsgType::Ok, *ResponseMessage, TEXT("Screenshot Taken")); 5 }
I'll get through this quickly as it's not really necessary but it a bit convinient.
You see; if we take a screenshot, sometimes Unreal is a bit too quick and will include the console in the screenshot too, we don't want that. All this does is:
cpp
1if(request->IsScreenshotRequested()) 2/* Checks if a screenshot has already been requested, 3 not sure how we can get here without it being requested but it's a safety thing more than anything. */ 4 { 5 FString ResponseMessage = TEXT("Screenshot saved to: " + request->GetFilename()); 6//Creates a string for a pop-up message box 7 8 FWindowsPlatformMisc::MessageBoxExt(EAppMsgType::Ok, *ResponseMessage, TEXT("Screenshot Taken")); 9//Displays the message box. 10 }
This, essentially, slows down the taking of the screenshot just enough to make sure the console has been closed. This is more of a bandage over a bigger problem though, so no guarantees.
Now just compile it, open your PlayerController class and find the Cheat Manager entry in its defaults and change the Cheat Class to MyCheatManager. You may need to restart Unreal for it to show up.
And there you have it, just launch your project in PIE, toggle the debug camera and then type into the console TakeScreenShot, and you've got your screenshot with UI.
Screenshot of my game with UI