+3 votes

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.

in Engine by (293 points)

1 Answer

0 votes

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.

by (69 points)

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.

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.

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...

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:
http://docs.godotengine.org/en/latest/learning/workflow/export/exporting_for_web.html#calling-javascript-from-script

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?

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

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

Please somebody help me...!!!!

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 webmaster@godotengine.org with your username.