Game Maker Developing Android Extension – Part 2

In this part of the tutorial, we will build up Java functions for our extension and effectively use them in our Game Maker project. We will also have some entry level programming in our extension. And the most important part is : Since Game Maker can only pass double and string values, we will convert them into other data types, using some techniques in Java.

It’s important for you to understand that I wrote this part of the tutorial for beginners, and I understand and know that you may say that I’m teaching Java “wrongly”.I don’t. I just do it for GML based users. An intermediate GML user can use Java easily, at least that’s what I believe. Please keep that in mind when reading this article.

A Small Java Lesson

As you’ve learned it in First Part of this tutorial, we will begin with one simple blank extension.

package ${YYAndroidPackageName};

//Game Maker Studio 2 Packages
import ${YYAndroidPackageName}.R;
import com.yoyogames.runner.RunnerJNILib;
import ${YYAndroidPackageName}.RunnerActivity;

public class myClass {

}

Variables a bit different in Java than Game Maker Studio, but also looks and feels the same. In Java, you create variables and functions within the main class (myClass in this example). You can execute these functions from Game Maker, or from your Java extension.

You can find better information about Java on Internet. I’ll just list the basics.

Declaring Variables in Java

In Game Maker, you would normally declare a variable by writing the traditional variable = key statement.

myVariable = "Hello World"
//myVariable's data type is String.

In Java, things are, a bit different than GML. In java, you declare a variable along with its data type. Also, the semicolon (;) is a must! The code below is an example

String myVariable = "Hello World";

myVariable is a STRING and it’s equal to “Hello World”

Creating Global Variables

After your class bracket, outside of any function, you can start defining your global variables. The syntax goes exactly as
public static dataType variableName = Value;

  • public makes your variable accessible from any other place while static defines your variable as “changeable”. This means that you can change your Global Variable in any point of your game. There’s also another one called final, which simply locks down your variable; meaning that it cannot be changed : Much of like #macro in Game Maker.
  • dataType can be a String, an int, boolean. Check here for more information. For using String (with capital letter S), make sure to add import java.lang.String; to your import list. Check the below example.
  • variableName is just the same as Game Maker.
  • Value must be the same as what you set as dataType. If you set it as String, then you must write your Value as “myvalue”.
  • And don’t forget to end it with semicolon (;)

Below I’ll create some Global Variables that I’ll use later on.

package ${YYAndroidPackageName};

//Here we import Packages from other Java files. Obviously we need them.
//Game Maker Studio 2 Packages
import ${YYAndroidPackageName}.R;
import com.yoyogames.runner.RunnerJNILib;
import ${YYAndroidPackageName}.RunnerActivity;
import java.lang.String;

//For printing log messages to the console.
import android.util.Log;

public class myClass {

//Define Global Variables
//See these variables are not inside any function!!
//I can change these anytime I want.
public static String myName = "My name is Marty!";
public static int myAge = 25;

//final variables cannot be changed again!
public final String myFavouriteSongLyric = "Why I gave it up for music and the free electric band";

public void myFunction() {
String myVariable = "Hellooo";
}

//I cannot access to the myVariable variable because it's only available inside of the myFunction Scripot

}

Creating Functions

Now, we’ll add a function. Functions get their, let’s say tags, as well. If you wish to control your function from Game Maker, then you’ll set it as public returnDataType myFunction(dataType argument0, dataType argument1). A function’s;

  • returnDataType : Can be void, String or double. This determines what kind of data type that this function returns. Void returns nothing!
  • Arguments also get data types as well. These must be the same as you set them on Game Maker IDE (Check First Tutorial again). If you close your function with (), then this function gets no arguments public void myFunction()
  • And as always, you must open and close your function with brackets { }

In functions, accessing variables are easy. You just write their names!

We’ll just add some functions to the 22th line of the script above.

public void what_is_my_name() {
Log.i("yoyo", "It seems like your name is :" + myName);
//I can Add one String to Another. Just like in Game Maker.

// "It seems like your name is :"  +  myName
}

public String what_is_my_favourite_lyric() {
//This function will return myFavouriteSongLyric, which is a String value I defined at the beginning.
return myFavouriteSongLyric;
}

public double how_old_will_i_be(double inyears) {
//This function will return a double data type.(REAL)
//I defined myAge as a global variable.
//When I called this function, I called it with an argument : inyears.

//return my age now.
return myAge + inyears;

}

You must also create functions in Game Maker IDE, which can use them. Check first tutorial again.

On the right side.

Once you do these, in Game Maker you can safely use

var mytext = what_is_my_favourite_lyric();
var imold = how_old_will_i_be(5); //Add 5 more years to my age
show_debug_message(mytext);
show_debug_message("I will be "+string(imold)+" years old in 5 years");

In the next part of the tutorial, we will together implement a Third Party Service together.

Share

Marty

Truely speaking, I don't know what am I doing most of the time.

1 Response

  1. http://www.aiobjectives.com/2019/10/31/what-is-simple-linear-model/

    A neural network is a network or circuit of neurons,
    or in a modern sense, an artificial neural networ
    composed of artificial neurons or nodes.
    … The connections of the biological neuron
    are modeled as weights. A positive weight
    reflects an excitatory connection, while
    negative values mean inhibitory connections.

Leave a Reply