0 votes

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")
    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))

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?
in Engine by (36 points)

de acuerdo, veamos.

1 Answer

+2 votes

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")
    JavaScript.eval("""alert("test")""")

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 https://docs.kongregate.com/v1.0/docs/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='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></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.:

Javascript.eval(kongregateAPI.loadAPI(function(){
  window.kongregate = kongregateAPI.getAPI();
  // You can now access the Kongregate API with:
  // kongregate.services.getUsername(), etc
  // Proceed with loading your game...
}))

You might have to change your html file with every new export though. http://docs.godotengine.org/en/stable/development/compiling/compiling_for_web.html#customizing-the-html-page 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.

by (69 points)

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")
    JavaScript.eval("""alert("test")""")

I exported the small project to HTML5, then uploaded it to Kongregate.com 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)

Update...

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?

My bad. I think it should look like this:

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

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():
        JavaScript.eval("""alert("test")""")

milestone

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

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

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