Is it possible to use the javascript singleton to make a HTTP request from a game when exported for the web?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By stubbsy345
:warning: Old Version Published before Godot 3 was released.

Would it be possible to write code in the .eval() function that will send a HTTP request and return the value when using the webassembly export?

You could possibly use XMLHttpRequest to do this? My javascript isn’t great so if someone thinks this may be possible and help would be great.

:bust_in_silhouette: Reply From: dsina

My JavaScript isn’t that great either, but here’s what I did for a simple post command where I send some data and get a response containing the same data:

var dict = {"x":0,"y":0,"z":0,"r":0.0,"g":1.0,"b":1.0}
var dataDict
if JavaScript:
    var code="""
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open( "POST", "http://httpbin.org/post", false );
        xmlHttp.send( '%s' );
        xmlHttp.responseText;""" % (JSON.print(dict))
    var response=JavaScript.eval(code)

    var responseDict=JSON.parse(response).get_result()
    var responseDataStr=responseDict["data"]
    dataDict=JSON.parse(responseDataStr).get_result()

The JavaScript singleton might be in the Globals class depending on which Godot version you use. Also the JSON methods might be in a different location.

Oh amazing. Do you know if this works in the web assembly export in 3.0? I’ll give this a go in my game now.

stubbsy345 | 2017-11-04 14:04

That’s where I used this code. I compiled my godot version a couple of weeks ago from Github so it should work in the new alpha snapshot for example.

dsina | 2017-11-06 14:24

Oh, this could be what I´m looking for but I actually don´t understand well…maybe because English is not my birth language…

Can some one explain to me if this eval() function can be use to execute javascript from Godot? …Or I miss understood everything…

I’m trying to use some js scripts from the webassembled proyects in Godot…

Actually I’m trying to use the Kongregate API. In order to do that I have to use a .js but not sure how to…

ferhand | 2017-11-15 01:23

Yes, with eval you can execute javascript code in your browser (because of this only works with asm.js or webassembly export).
The last line in your javascript string will also produce the return value of the eval() statement.

Here’s the (slightly outdated) doc page describing it:
Exporting for the Web — Godot Engine (latest) documentation in English

dsina | 2017-11-18 19:03

Hi @dsina:

There must be something wrong because I do not understand … I tried to make a simple Godot html5 project with this script:

var JavaScript

func _ready():
	# retrieve the singleton here, will return `null` on platforms other than web
	JavaScript = Globals.get_singleton("kongregate_api")
	my_func()
	pass

func my_func():
	# call JavaScript.eval only if available
	var label = get_node("Label")
	if JavaScript:
		JavaScript.eval("alert('Calling JavaScript per GDScript!');")
		label.set_text("Calling JavaScript per GDScript!")
	else:
		label.set_text("Existe? " + str(JavaScript))

I uploaded this project to Kongregate and Gamejolt. So far, I tested the Kongregate version and printed only:

Existe? [Object:null]

I think Kongregate sends the kongregate_api.js to each game, so it should be available … but I’ve failed … T.T.

Where is JavaSingleton hiding?
What exactly is JavaSingleton?

Can anyone tell me how to simply access a simple .js file within the Godot project?
a functional example. please?

ferhand | 2017-12-14 00:07

Can you open up a new question with this? Also write there which verion of Godot you’re using.

dsina | 2017-12-15 13:48

Hello, dsina
I followed your advice and opened a new question …Here

Please somebody help me…!!!

ferhand | 2017-12-19 01:52