0 votes

I'm taking a screenshot of the game, at a certain moment, and i want to allow the player to download this image, is that possible? (in a web game).

I asked this same question on discord and received this link:
thanks to @Juulpower.

but this don't seem to have any answer or anything like that, could i make something like that? I'm limited to not using a separate host of the game itself.

Godot version v3.3.2 stable
in Engine by (44 points)

1 Answer

+1 vote
Best answer

i would try convert the image to a dataurl.


This is basicly a base64 encoded version of the image file and hand it over to javascript
Javascript can then build a link and click it

//javascript function in the website
function download(dataurl, filename) {
  var a = document.createElement("a");
  a.href = dataurl;
  a.setAttribute("download", filename);

download("data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="", "nameOfTheFile.jpg"); //<-- this must be called from godot
by (4,078 points)
selected by

Hey, thank you for your answer, but this godot docs link is unavailable.
Do you know how i could link javascript with godot? For example, i need an especific config? or only a script is enough, i have to create a reference? this kind of thing.

im sorry ... here is the link again:


how i could link javascript with godot?

This is the Javascript tuorial for godot:


you may want to experiment a bit with godot to javascript workings

Hello again, so i tried what you suggested, by reading those links i learned about Javascript.eval(), but i don't get any response of the server using the code you said, if i place an alert of some kind, it works, but only that.

var file = File.new()
file.open("res://screenshot.png", File.READ)
var base_64_data = Marshalls.raw_to_base64(file.get_buffer(file.get_len()))
var url = base_64_data
var comand = """
    var a = document.createElement("a"); 
    a.href = """ + url + """; 
    a.setAttribute("download" , screenshot);

JavaScript.eval(comand, true)

Check the javascript console of your browser ... any error there?
Do some console.log() messages into your javascript to debug the code.
Maybe the the scope is wrong and you cant reach the document object.

... and check your dataturl ... i think ist missing the mimetype and encoding ... look up dataurl in google

I figured out, it was indeed an error that was ocorring, but was how i was trying to pass the base64 from godot to javascript, is was in this line:

a.href = """ + url + """; 

but, now i can't send the data to javascript, i can't find how to do it on internet, do you know how to pass this information?

the encoding trashes your posting

you also can use single quotes in javascript

var url = "data:image/jpg;base64,"+base_64_data #dont forget the mimetype and encoding
var comand = "
    var a = document.createElement('a'); 
    a.href = '" + url + "';  //<---- single qoutes and double quotes
    a.setAttribute( 'download' , 'filename.jpg' );

That worked exactly how i wanted.
Thank you very much! :D

Thank you for your time and patience.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.