How to let player draw on widget and save image?
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.
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 https://www.youtube.com/watch?v=1Z-V1Buk6z8
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. https://answers.unrealengine.com/questions/33571/fimageutilscreatetexture2d-causes-brief-hang-is-th.html
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:
I hope all of that made some sense. Apologies if not, I'm still figuring it all out myself :) It would surely be nice if Epic provided some better tools for handling render targets inside blueprint!
answered Jan 13 '17 at 05:21 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here