How to let player draw on widget and save image?

Hi all,

I would like to build a system where a player can draw lines on the screen using their mouse, and then save the drawing they have created to be loaded at another time. What would be the best way of achieving this in UE4 using blueprints?

If there is a way to achieve this through C++ then please mention it as well, but i’d ideally like this to be blueprint based as my experience in C++ with UE4 is limited.

Any suggestions?

You want to be using render targets for this. You should watch Epic’s training stream to get a basic idea of how to set up blueprint render targets Blueprint Drawing to Render Targets Overview | Live Training | Unreal Engine - YouTube

As for saving / loading the render targets, that’s a little bit more complicated. There isn’t any out of the box functionality that I’m aware of to do this, so I’ve had to piece together my own solution in C++. The basic idea is that you want to read the pixels from the render target and write them to disk. You can then initialize a new render target with this saved data at a later time.

Skip to the bottom and look at the AHeightMapReader::UpdateBuffer() method in his code. Also take note of the “non blocking update buffer” method used beneath this. Reading pixels from a render target is a fairly expensive operation, and I personally found this solution to be quite necessary!

In my own implementation I am writing the pixels to my saved game object. To load my render target I then grab the pixels in my saved game object and initialize a new Texture Object from these pixels (see FImageUtils::CreateTexture2D()) One thing to be aware of when using this method is that it does contain editor only code, so you’ll probably want to have a look at Rama’s alternative version of this function which you can find here.

Once you have a Texture2D that you’ve created from the saved render target data the rest is easy! You can just draw it to a fresh render target!

something like this:

UKismetRenderingLibrary::BeginDrawCanvasToRenderTarget(this, NewRT, RTCanvas, DrawSize, RTDrawContext);

RTCanvas->K2_DrawTexture(SavedRTTexture, FVector2D::ZeroVector, FVector2D(RTSize, RTSize), FVector2D::ZeroVector, FVector2D::UnitVector, FLinearColor(1.f, 1.f, 1.f, 1.f), EBlendMode::BLEND_Translucent);

UKismetRenderingLibrary::EndDrawCanvasToRenderTarget(this, RTDrawContext);

I hope all of that made some sense. Apologies if not, I’m still figuring it all out myself :slight_smile: It would surely be nice if Epic provided some better tools for handling render targets inside blueprint!

Oh! And I’m not totally sure what your goals are , but if you aren’t using a save game object you can also use the blueprint function “ExportRenderTarget” to just write the render target to disk as an HDR image. This would work fine if you’re cool with having a bunch of loose image files on the disk :slight_smile: