Jump to content


Photo

Tutorial: Calling Java


  • Please log in to reply
9 replies to this topic

#1 Mayhem Games

Mayhem Games

    Proud Kiwi

  • GMC Member
  • 959 posts
  • Version:GM:Studio

Posted 24 September 2011 - 01:14 AM

Because we can't use dll's in Game Maker HTML5 we are going to have to start porting our dll's to Java. So i put together a quick tutorial of how to call Java functions from Game Maker HTML5

Step 1: Your going to need to write your Java applet "dll". I wrote a really simple maths example.

import java.applet.*;

public class MathsTest extends Applet
{
   public void init()
   {
   }
 
   public void stop()
   {
   }

   public double addition(double a, double b)
   {
       double answer = 0;
       answer = a+b;
       return answer;   
   }

   public double subtraction(double a, double b)
   {
       double answer = 0;
       answer = a-b;
       return answer;   
   }

   public double division(double a, double b)
   {
       double answer = 0;
       answer = a/b;
       return answer;  
   }

   public double multiply(double a, double b)
   {
       double answer = 0;
       answer = a*b;
       return answer; 
   }

}
Step 2: Next your going to have to compile your .java file with the JDK (if you don't know how to do this you shouldn't be here :tongue: )
Step 3: We need someway for Game Maker HTML5 to call and retrieve values for the applet. So we are going to need a Javascript bridge.

function MT_addition(a,b)
{
   i = document.MathTest.addition(a,b);
   return i;
}

function MT_subtraction(a,b)
{
   i = document.MathTest.subtraction(a,b);
   return i;
}

function MT_division(a,b)
{
   i = document.MathTest.division(a,b);
   return i;
}

function MT_multiply(a,b)
{
   i = document.MathTest.multiply(a,b);
   return i;
}
Step 4: Create a new extension in your project for the Javascript bridge. If you don't know how to do this follow JacksonYarr's tutorial.
Step 5: Save your game somewhere using the "Create Application" button. Goto the location where you saved it and find the "index.html". We now need to add the applet to webpage using to code below. (remember to change the "code" and "name" tag to match your files) The name tag is used in the Javascript bridge and the code tag is the location of your .class file.

<applet code="MathsTest.class" name="MathTest" width=0 height=0>
</applet>
Step 6: Copy your class file into the same directory as your HTML file if using to code above or where ever your class tag points.

Done! :biggrin: Hopfully this will be useful to atleast one person. As you can tell by the tutorial you can't preview it in Game Maker HTML5 because you have to add the applet tag manully to the index webpage. Yoyo Games should add a feature where you can cutomise the generated webpage to fix this limitation. If you didn't manage to follow here are all the files HERE.

Mayhem Games

Edited by Mayhem Games, 24 September 2011 - 01:16 AM.

  • 1

For a long time it puzzled me how something so expensive, so leading edge, could be so useless, and then it occurred to me that a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a dangerously perfect match. ~ Bill Bryson


#2 BlueMoonProductions

BlueMoonProductions

    BlueMoonProductions

  • GMC Member
  • 241 posts
  • Version:GM:Studio

Posted 24 September 2011 - 01:35 AM

The main problem with DLL's is that they only work on Windows, not that they don't work with HTML5.
  • 0

#3 Mayhem Games

Mayhem Games

    Proud Kiwi

  • GMC Member
  • 959 posts
  • Version:GM:Studio

Posted 24 September 2011 - 01:40 AM

The main problem with DLL's is that they only work on Windows, not that they don't work with HTML5.

Yeah i realise that. I know you can use Game Maker HTML5 for windows development but i was refering to HTML5 Games. :tongue:
  • 0

For a long time it puzzled me how something so expensive, so leading edge, could be so useless, and then it occurred to me that a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a dangerously perfect match. ~ Bill Bryson


#4 BlueMoonProductions

BlueMoonProductions

    BlueMoonProductions

  • GMC Member
  • 241 posts
  • Version:GM:Studio

Posted 24 September 2011 - 02:13 AM

The main problem with DLL's is that they only work on Windows, not that they don't work with HTML5.

Yeah i realise that. I know you can use Game Maker HTML5 for windows development but i was refering to HTML5 Games. :tongue:

Yes, I know, I was referring to HTML5 (games) too. Your solution is nice, but the problem is simply that HTML5 is for webbrowsers, not for Windows. Using a DLL is like requiring a keyboard or using multi-touch (all in HTML5). It just shrinks the number of available target platforms.

Edited by BlueMoonProductions, 24 September 2011 - 02:14 AM.

  • 0

#5 Mayhem Games

Mayhem Games

    Proud Kiwi

  • GMC Member
  • 959 posts
  • Version:GM:Studio

Posted 24 September 2011 - 02:23 AM

The same thing could be said about Game Maker, its for games. Yet dll's like Max WinAPI exist. Not everyone will use this because like you said it limits target platforms. For example i have found the java file manipulation functions work better than the HTML5 ones due to sandbox issues. These functions work on my Nokia n95 which is like 5 years old and doesn't support HTML5 fully. Once again not to everyones taste but its there for people that might use it.

Edited by Mayhem Games, 24 September 2011 - 02:25 AM.

  • 0

For a long time it puzzled me how something so expensive, so leading edge, could be so useless, and then it occurred to me that a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a dangerously perfect match. ~ Bill Bryson


#6 OMGCarlos

OMGCarlos

    GMC Member

  • GMC Member
  • 329 posts
  • Version:GM:HTML5

Posted 24 September 2011 - 04:34 AM

I definitely agree with you on the customization of the generated webpages, even like a basic textbox that allows you to copy/paste code into it - similar to the way web templates work for platforms like Joomla and Drupal. The more I use GMHTML5 the more I realize the ridiculous potential it has, I'm not sleeping tonight
  • 0

#7 paul23

paul23

    GMC Member

  • Global Moderators
  • 4147 posts
  • Version:GM:Studio

Posted 24 September 2011 - 08:31 AM

Java would for example the only way to create fast multiplayer games - as most browsers don't support javascript websockets. (And some said they wouldn't ever support those).
  • 0

#8 giga970

giga970

    GMC Member

  • GMC Member
  • 458 posts
  • Version:GM:Studio

Posted 11 August 2012 - 02:06 AM

This is completely brilliant. I'm surprised this topic isn't getting more traffic. I can use my little bit of Java experience with GM! I'm not exactly sure what I'd do with it, but it's cool to know about this.
  • 0

| Website - http://afrodynamics.net/ | Twitter - @afrodynamics |

| My Ludum Dare 24 Entry | GameMaker Video Tutorials | Aegis (HTML5 game on Kongregate) |

| Kongregate API Extension - Use Kongregate's API in your HTML5 Games |


#9 Debels

Debels

    GMC Member

  • GMC Member
  • 3029 posts
  • Version:GM:Studio

Posted 05 November 2012 - 12:46 AM

Would this work on Android?

Note: I'm going to test this on android and edit this comment with the answer if no one else responded this question :thumbsup:

edit: I couldn't test this, since I need to download the Android SDK, so Ill wait for someone to respond.

Edited by Debels, 05 November 2012 - 01:07 AM.

  • 0

"Give a man a fish, and you feed him for a day; show him how to catch fish, and you feed him for a lifetime."

(Chinese axiom)


#10 Derme

Derme

    Time for a break.

  • GMC Member
  • 489 posts
  • Version:GM:Studio

Posted 22 November 2012 - 11:56 PM

No it won't, as Android still does not support extensions. :/
  • 0
TwitterBanner_zpsf5eaf370.pngSignBanner_zps6e538aa2.pngjust21_promo_graphic_zps785b873f.png