Problems calling Java from Javascript

Anything libgdx related goes here!

Problems calling Java from Javascript

Postby MrAgent » Thu Jun 14, 2018 5:14 pm

I'm trying to call a function I've created in HtmlLauncher.java from javascript within the index.html file.

This is how I'm exporting the function in HtmlLauncher.java:

Code: Select all
public void messageApp()
    {
      ...
    }

public static native void addMessageApp() /*-{
        $wnd.messageApp = $entry(this.@mainTest.HtmlLauncher::messageApp());
    }-*/;


And in my index.html I'm trying to call this function like this:
Code: Select all
<script type="text/javascript">   
   messageApp();
</script>


When the messageApp() is called this is the result I get in the developer console output in Firefox:

TypeError: a is undefined

In Chrome I get this:

Uncaught TypeError: Cannot read property 'apply' of undefined
at qYd (html-0.js:3180) function qYd(a,b,c){return a.apply(b,c);var d}
at tYd (html-0.js:5104) function tYd(a,b,c){var d;d=rYd();try{return qYd(a,b,c)}finally{uYd(d)}}
at eval (html-0.js:5363) function sYd(b){pYd();return function(){return tYd(b,this,arguments);var a}}


If I type messageApp(); in both browsers console it does find the function, so it has been created and does exist... I just don't know what this "a" is or why it's undefined.

I have all sorts of different methods to achieve this, static function instead. Using the whole var that=this, etc... They all seem to work up to this point where a is undefined. I'm not savvy with Javascript, so I'm kinda lost in this.

Any ideas?
MrAgent
 
Posts: 4
Joined: Thu Jun 14, 2018 4:37 pm

Re: Problems calling Java from Javascript

Postby MrStahlfelge » Thu Jun 14, 2018 5:52 pm

For calling messageApp() from JSNI it should be

this.@mainTest.HtmlLauncher::messageApp()()

in case your package name really is just mainTest.

Can't say if your construction with $entry does work. Never needed to call GWT methods from outside JavaScript and can't imagine any use case, however, when I need to define a callback for JS libs I typically use the following syntax to pass the callback function:

var that = this;
function(){ that.@mainTest.HtmlLauncher::messageApp()();}
MrStahlfelge
 
Posts: 97
Joined: Thu Jun 15, 2017 6:40 am

Re: Problems calling Java from Javascript

Postby MrAgent » Thu Jun 14, 2018 6:50 pm

MrStahlfelge wrote:Never needed to call GWT methods from outside JavaScript and can't imagine any use case,


I would guess I'm going about this wrong as this is my first foray into libgdx/java/javascript and I'm most likely using the wrong wording. Perhaps it would be better to explain what I'm trying to do.

I'm fooling around with Facebook instant games. In my game I have a high score button the user can press. When they press the button I call the Facbook SDK function which gets the high score listing. This is an asynchronous call so the data that I get back cannot be passed back to my game right away upon returning. Once that data is available I want to call some function in my game and pass that info to it so I can display the score listing in game.
MrAgent
 
Posts: 4
Joined: Thu Jun 14, 2018 4:37 pm

Re: Problems calling Java from Javascript

Postby MrAgent » Thu Jun 14, 2018 8:46 pm

Hmmm, strange. I got it to work by changing the messageApp function to static and removing the "this." in the addMessatgeApp function.

This is what works for what I wanted below:
Code: Select all
public static void messageApp()
    {
      ...
    }

public static native void addMessageApp() /*-{
        $wnd.messageApp = $entry(@mainTest.HtmlLauncher::messageApp());
    }-*/;
MrAgent
 
Posts: 4
Joined: Thu Jun 14, 2018 4:37 pm


Return to Libgdx

Who is online

Users browsing this forum: evilentity, Google [Bot], Majestic-12 [Bot] and 1 guest