How 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 ferhand
:warning: Old Version Published before Godot 3 was released.

Godot version: 2.1.3 and 2.1.4
OS: Windows 8.1 Enterprise

CPU: Core i5 vPro
RAM: 8 Gb

As I said in this LINK post (19036) :

I am trying to execute javascrip code from my Godot project exported to html5, in order to make HTTP requests and obtain information through the publishers API. In this case, I am trying to achieve this with the Kongregate API.

This LINK part of the documentacion explain how to do some of that so I tried to make a simple Godot html5 project with only this script:

var JavaScript

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

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!")
        label.set_text("Existe? " + str(JavaScript))

The project only have a Node2D as a parent and a Label as child.

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 to find it… 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?

de acuerdo, veamos.

SingingApple | 2018-06-10 05:49

:bust_in_silhouette: Reply From: dsina

See if you can get a simple javascript alert working. For your godot version I think it has to be:

func _ready():
    JavaScript = Globals.get_singleton("JavaScript")

This JavaScript object is just your connection to the Javascript runtime. You can’t directly load javascript files using the get_singleton() method. What you can do is execute any Javascript command with eval() once the javascript singleton is loaded.

To load the kongregate api I suggest you do it like they discribed in Javascript API. Everytime you do a webexport godot generates a html file which you can modify (in Windowns 10 it’s under c:\Users\USERNAME\AppData\Roaming\Godot\tmp\tmp_export.html).

In this html file you need to import the kongregate api, preferably before your game starts. It’s just one line to add. Your html file should then contain something like this:

<script src=''></script>
<script type="text/javascript" src="tmp_export.js"></script>
<script type="text/javascript"> .....

Now you can execute commands there in the javascript part of the html file or in Godot if you’ve loaded the javascript singleton as shown before, e.g.:

  window.kongregate = kongregateAPI.getAPI();
  // You can now access the Kongregate API with:
  //, etc
  // Proceed with loading your game...

You might have to change your html file with every new export though. Compiling for the Web — Godot Engine (stable) documentation in English shows how to customize it properly but I don’t quite understand it tbh. I didn’t test any of this so let me know if it works.

Thank you very much, dsina:

You do not know the problems that I have in my head for not being able to solve this dilemma of the javascript singleton.

Actually I created a Node2D and placed the following script:

func _ready():
    JavaScript = Globals.get_singleton("JavaScript")

I exported the small project to HTML5, then uploaded it to and ran it …

… but nothing happened.

I unlocked the pop up in the webbroser thinking what that would be, but nothing either …

…I do not know what else to do…

Please help…!!! (T.T)

ferhand | 2017-12-20 02:06


Actually in the console the project writes this…

**SCRIPT ERROR**: At: res://.gdc:11:_ready() - Attempt to call function 'eval' in base 'null instance' on a null instance.

What this means?

ferhand | 2017-12-21 00:07

My bad. I think it should look like this:

func _ready():
    var JavaScript = Globals.get_singleton("JavaScript")

Try to get just this running locally and without kongregate first to make sure the singleton works as expected.

If this doesn’t work try upgrading to the new beta1 version. This Singleton might only be available in newer versions. If you’ve done that, the following is what works for me. No need to load the singleton through the Global class, contrary to what the docs say.

func _ready():

dsina | 2017-12-21 17:09


Wow, thank you very much for answering again, dsina:

I have some problems to use version 3 of Godot since my laptop does not have OpenGL 3. I will try some solution for my laptop to work with OpenGL 3 otherwise I will be stuck in Godot 2 until the version of Godot 3 comes out that use OpenGL2. I read that in this link

I would swear that I have seen in some old forum the possibility of using the JavaScript singleton from Godot 2, but I can not find the link to show it to you. Maybe it was also my imagination and the desire to solve this problem.

for the moment I thank you again and I will wait to be able to use OpenGL 3 on my laptop. I have heard that if I use Ubuntu (Linux) it is possible that there is an update for my microprocessor. I’m not sure…

Very grateful for your help, dsina …

ferhand | 2017-12-22 22:04

I just read that you have a question about this on another thread. Where are you stuck currently?

dsina | 2018-06-27 15:23