[Solved] Create Screenshot

Hello all,
is it possible to create a screenshoot and use it as image in the game ?

1 Like

do you mean in during the game? i don’t think it is possible.

yes, during the game

i don’t think there is an event for that.
Maybe you can code something on JavaScript

1 Like

I’m not able to do it, but thanks !!!

You can take a screenshot, and save it as sprite directly ingame !

Open the example Load images from url.

You have the URL of the file, instead an URL we will give a base64 string format.

The base64 string will be your screenshot.
For take a screenshot uses these lines of javascript event :

//For see entire base64 string in developper tool

//Get canvas of game
const game = runtimeScene.getGame();
const variables = runtimeScene.getVariables();
const renderer = runtimeScene.getRenderer();
const canvas = renderer._pixiRenderer.view;
let screenshot = canvas.toDataURL("png");

//Save base 64 screenshot in a global variable called "myScreenshot"

So use this javascript code when you wish take a screenshot, the base 64 string is saved in a global variable called “myScreenshot” in GDevelop.
So if the global variable exist or isn’t empty you can give the value where is setup the URL in the example.

1 Like

I think they meant like adding the image as a pixi Sprite to the renderer. In that case you would need the blob describing the file not a b64 representation of the image. A hacky way would be to do your trick to get the canvas, then get somehow the object data( from the json manager maybe ) then modify one of the animations to make. Now I’m not sure if the resources in object data are directly loaded from a URL in it or from the resources manager, so either add the URL from window.URL.createObjectURL(screenshotImageBlob); to the resources manager and the appropriate name in the animation’s data in the object data or the direct link depending on how it works. Then create a new instance of gdjs.SpriteRuntimeObject and backup existing instances data before erasing them all, replace the constructor and reset the cache for those objects, create new instances and restore their respective data but now they have the screenshot as animation like asked.

I created this (first for me) Javascript :slight_smile:

and the effect is that each atime I press S, then M the screenshoot is displayed with the same size of the original one but with the source position on right and top:

The sprite Image “go” to the right top corner and the text is copied each time.

1 Like

Oof sorry bouh didn’t notice there already was something you made to set an image of a sprite to another. I only said Incorrect thinghs too :thinking:. I am sorry for the inconvenance.

Why have you rewrite my code ?
Your lines 11 and 15 are useless, you can just uncomment line 14 and set “url” instead “myScreenshot”.

This JS event is like an action, you need use once condition with it.

I don’t understand that sentence.
I guess you need take a look on dimensions of the object sprite, and positions.

1 Like

I tried to modify the original one script in this way but it doesn’t work:

In yellow the new code (copied from you example)