This tutorial applies to Game Maker Studio and all platforms. This tutorial covers how to create, name, manage and run scripts.
Difficulty: Low - Basic GML.
Requirements: Game Maker Studio.
Part 1 - Creating Our Resources:
First thing you need to do is create a new project then add an object as well as a script. To create a new object, right click the "objects" resource folder on the left and click "create object". Name the object then close the object editor window. Once you are done with that, create a script by right clicking the "scripts" resource folder and clicking the "create script" option. Name your script "scr_multiply" and leave the script editor open. Our script is going to multiply one number by a given factor. Scripts can also be added to objects very easily, but we will get to that later on in the tutorial.
Part 2 - Comments and Script Defining
The first thing you need to learn about are comments. There are three types of comments: Single Line, Multi Line and Definition Line. Single line comments and multi line comments act the same; they let you add general comments, descriptions, instructions, etc to your code. To add a single line comment, simply use two forward slash marks as follows:
To add multi line comments to your code, use the forward slash asterisk opening symboles then close with an asterisk forward slash closing symbol. This is illustrated here:
Definition lines are used to name scripts for the help system. Unlike naming a script using the name field, this is not required and simply makes coding in the future easier. The help system will pick these up and add your scripts name and details into both the intellisense auto completion window as well as the script argument details that appear on the bottom of the code editor. Definition lines are single line comments with an extra forward slash. Right after these slashes you should put the script name as well as its arguments. This needs to be formatted as if you were calling the script in code (arguments within brackets and separated by commas). Because we need a number to multiply, as well as the factor, our two arguments would be a number and a factor. This is illustrated here:
You have now full named and defined a script, learned to use comments, as well as learned how scripts are called within any GML code you will write in the future.
Part 3 - Arguments And Returns, Our Script Is Done
We now have to complete the script so that the number provided by the code calling the function will be multiplied by the other number provided by the calling code. We get these two numbers by reading the two arguments set by the code that called the function. Arguments are just like any other variable but hold the info passed to a script. There are 16 total arguments available to every created script. You access the data in these arguments by simply writing argument# (# being the argument number) kind of like you would if you had a variable called "gunstyle1" and "gun style2". The only thing you need to keep in mind is that number starts at 0, not 1. So argument0 would technically be the first argument provided; argument15 would be the 16th possible argument. Since we have two arguments, the number we multiple will be argument0 while the multiplication factor will be argument1. Here is an example of accessing an argument:
Now, we could finish the script by writing the following code:
But if you think about it, we don't HAVE to store the argument in another variable like we did above. I typed it out like that to make it easier to understand. What we could have done was just do the multiplication equation itself by simply using the arguments. We can do this by writing the following:
Our script is now done, fully functional and fully optimized (can't make it faster). We can now go to the object we created earlier and have it run the script as well as draw the answer to the screen. But before we do that I would like to tell you what "return" did in the script. Scripts are able to return a value so that when you run the script you can store the result in a variable within other code in your game. Scripts don't have to return something or even contain the "return" command. If you remove that line the script will still work as intended (as far as the math goes) but you wouldn't be able to draw the answer as the answer was never returned anywhere (you could store the answer in a global variable instead of returning it but this way any varible you create can access the answer).
Part 4 - Finishing The Object
Open the editor for the object we created at the beginning of this tutorial. Right click the event area and add a new "draw" event. Within the draw event we need to create a new custom script. You can do this by going under the "control" sub-tab located on the right. Under the "code" drag and drop options, drag the first icon to create a new script that is run in the draw event. Type the three slashes to define this script. Unlike in script resources we do not name the script and also define it differently. The definition is just any word or short sentence that describes what the script does. This description will then be used as the name of the action within the object editor instead of "execute a piece of code" that is usually shown. This allows you to find scripts within objects easier. You could name one "input" and one "movement and path finding" or something like that. It's very useful. Since we are just drawing the answer, our description comment could just be something like this:
Now all we need to do is use the built in "draw_text()" function to draw our answer. But before we type all that code out, try typing the name of your multiplication script within the code. You could type "scr_m" and stop typing. You will notice that the intellisense auto code complete window will come up with our custom script name as well as what arguments it takes. Finish typing the script name by typing out "scr_multiply(" and you will now notice that the bottom of the code window shows what arguments are needed and which argument you are currently entering/editing (it is bolded). Cool right? Not only can you create your own scripts but Game Maker recognizes them and adds its details to the code system for easier coding in the future.
To finish our script we will use the following code. This tutorial was not to teach GML, just how to create a script, so I will not go into details on these functions. But if you copy and paste this GML code into the objects draw script, place the object in a room then run a game test, you will see the answer drawn on the top left of the game.
Hope this tutorial taught you about script management. Scripts are very powerful and not to difficult to learn to write. I suggest looking at other tutorials to start learning the GML language.
This tutorial applies to Game Maker Studio, Android and Google Game Services. With that said, you should be able to easily apply this knowledge when working with iOS as the functions used are the same.
Difficulty: Medium-Low with basic GML knowledge.
Requirements: Game Maker Studio, Android export module (does not work in mobile testing mode), Google Play developer account, Android device to test on.
Part 1 - Setting Up GGS:
First thing you need to do is set up Google Game Services for your game. You can do this by going to your developer console and clicking the gamepad icon on the left menu. Once you have the game services section up, click "Add a new game" and follow the instructions. This part is pretty straight forward so this tutorial will not go into detail about that setup.
Once you have your game set up with Google Game Services you will be given an app ID that will allow you to activate those services within your Game Maker game. This ID is located next to your game name within the Game Services section of the developer console. This is illustrated below:
Take this number and put it in Game Maker Studio. The place to paste this ID number is located under Global Game Settings -> Android (top tab) -> Social (left sub-tab) -> Google Services (section). After you paste the number into this text box, make sure you check off "Enable Google Services". This process is illustrated below.
Part 2 - Setting Up Leaderboards and Achievements:
First, we are going to set up some achievements. To do this, select the "Achievements" section while under Game Services and your game has been selected. This sub-tab will be shown to the left. If you do not see this tab, maximize your browser. This tab will go away if the window is too small.
Once you are in the achievements section, click "Add new achievement" located at the top. This part is straight forward and will not be gone into detail. Simply fill out the form and save the info you entered. Once that achievement has been created it will have a specific ID generated for it. This is illustrated below (illustrated is the leaderboards page but its the same layout).
This ID is what you will use when using the achievement functions within Game Maker Studio. Continue to make the desired amount of achievements you want then continue this tutorial.
Next we are going to make leaderboards. This process is basically the same; very simple and straight forward so we will not go into detail. Click the "Leaderboards" sub-tab to the left; it is right below the "Achievements" area we just dealt with. Click "Add new leaderboard" and fill out the simple form. Once that is completed you will, once again, have a randomly generated ID be given to that leaderboard. As before, this is the ID you will use when working with leaderboard functions within Game Maker Studio.
Part 3 - Understanding the Game Maker Functions:
This is the 2nd last part of this tutorial. I am not going to go into detail on where to use this code. This should be obvious and you have the choice of when to use these functions. In my Android game, a user taps a button on screen and the "Mouse left released" event is used. You could code a button that way or use these functions in some other way. Again, that is up to you. What this part of the tutorial does is show you what functions are used and what exactly they do.
The first functions we need to look at are "achievement_login()", "achievement_login_status()" and "os_is_network_connected()". achievement_login() will open up a Google Plus login screen and allow the user to connect their device to your Google Game Services. This is automated and only needs to be called. It is recommended that you pause the game or do this where the game can run freely and not interfere with gameplay. achievement_login_status() will return true if the user is logged in, false if they are logged out. os_is_network_connected() will return true if the user has internet access, whether by Wifi or cell network, and return false if no internet access is available.
Before we call achievement_login() we should make sure the user has internet access. There was a bug within Game Maker that caused apps to be permanently locked if this function was called with no internet. It basically caused an infinite loop. This bug was fixed but this also points out how important it is to check. So I will give you the code I use when a user clicks the achievements menu button within my game. The code is as follows:
What this code does is check to see if the user is logged in and check to see if there is internet access. If they are not logged in but there is internet access then the user is asked to log in. If they are logged in then the achievement window is shown. This brings us to the "achievement_show_achievements()" function. This function will display a window listing all your created achievements and their details. This window does not need to be coded, it is a window created by the Google Services API. However, you need to keep in mind that your game will run behind it. So if you choose to use this function during gameplay then it is recommended that you call your "game pause" code. To show a list of all your leaderboards instead of achievements, simply call "achievement_show_leaderboards()". It is that simple.
But what about posting scores and marking achievements as completed? Well, remember those ID's we discussed before? Using those, you only need one line of code to post scores and achievements. Yup! It's that simple. The functions you need to call are "achievement_post()" and "achievement_post_score()". Here are some code examples on how to use these:
What this code does is check to see if the user scored above 100 ("ADjfneDFOaj" is a made up ID, use the correct one from your list of leaderboards). If they did, then post their score to the leaderboard. This is one way you can create a minimum score in order to make it on there. Simply remove the if statement checking the score against 100 to post any score, though I would recommend changing it to "> 0" to make sure only scores above 0 are posted.
You can change the same exact code above to post an achievement instead. If you have an achievement that should be marked as completed if the user scores 100 points or more during a game, simply use:
As you can see, the code is basically the same, but instead of passing global.userscore into the function, we pass "100". The reason we use "100" is because "100" means the achievement was 100% completed and Google will mark that achievement as completed for the user.
Part 4 - Things You Need To Consider:
You must log the user in to post achievements and leaderboard scores. These will not que and post when the user is logged in. I recommend having an alarm run and each time the alarm is called, post the users scores and achievements. This way, if you keep track of user progress in code and continuously use these functions, Google Game Services will get these scores and achievement statuses the next time the user is logged in.
These functions will only work when an APK is built. These functions will not work when using the test game functionality of Game Maker Studio. This is because Google Game Services is linked to the actual APK with the right credentials, not just the ID it generates. This also means that you need to make sure not to lose your keystore and keystore info. If it is lost then your Game Services may be lost forever.