The Basics of GML 2

Coding Banner

GML, or Game Maker Language, is the programming language used in GameMaker: Studio. All of the games on Coded Games have been made using this language. One of the primary benefits of using GML is that it is extremely easy to learn and very straight forward.

For this tutorial I’m assuming that you already know how to use GameMaker’s drag and drop system, know how events work, and know the basics of using GameMaker. If you are brand new to using GameMaker: Studio you can watch my tutorials here.

Setup Tips

If you only use the code drag and drop action (which is the only one you should use after switching to GML) it is extremely helpful to go into preferences, click the forms tab, and change the default TAB to select to 4. This makes it so that when you open an object the code action will be easily accessible.

Basic Variable Types

Unlike some programming languages, in GML you don’t have to worry about variable types as much. There are only two primary types of variables in GML, strings and real numbers. Strings are more like words and are always in quotations. For example “hello world” and “5.786” are both strings. Real numbers are numbers don’t go in quotations. For example 5.786 and 72 are both numbers. Keep in mind the true and false keywords are also real numbers. Anything less than 1 is considered false and anything equal or greater is considered true.

Creating Variables

Creating a variable in GML is very simple, all you need to do is type a name for it and then set it equal to something. The name can be anything as long as it doesn’t conflict with any of GameMaker’s built in keywords. For example b=5 well make a variable called b equal to the real number 5. Doing b=”5″ would set b equal to the string “5”. Doing this creates the variables and stores them in memory. Keep in mind that you have to create your variables before you actually use them otherwise you will get errors.

Manipulating Variables

You really will only need to change variables that are real numbers. There are ways to manipulate strings but it is much more complicated. If you want to add, subtract, multiply or divide a variable you can simply add the corresponding sign before the equals sign. For example if I wanted to add 5 to my variable b every step I would put b+=5 in the step event. If I wanted to multiply it by 2 I would do b*=2 and so on.

If Statements

When working with Drag and Drop you may have noticed that some of the actions had rounded corners. That meant they were an if statement. If statements are how you do anything in GameMaker. For a very basic example lets say you want a variable to count up to 10, then reset back to 0. You would create the variable in the create event, b=0. Then in the step event add one to it every step by putting b+=1. Now how do we make it reset back to 0 when it hits 10? With if statements! In the step event you can type:

if b=10 {
b=0
}

This will reset b back to 0 after b is 10. If you wanted to do more than one thing with per if state you would just put more functions in between the curly brackets.

Functions

Most of the drag and drop actions are just GML functions. For example the instance create drag and drop action is just the instance_create(x,y,object) GML function. You learn functions by looking them up online or opening the GameMaker manual. Here is a PDF showing all the drag and drop actions GML equivalent. When switching to GML the GameMaker manual will be your best friend. Most functions are variables. For example the distance_to_object(object) is just a variable that returns the distance between the object calling the function and the object defined the in function. This means you can use if statements to test the variable.

if distance_to_object(obj_player)<5 {
obj_player.hp-=5
}

For example if this code was added to obj_enemy’s step event when the player was within 5 pixels of obj_enemy the enemy would lower the player’s hp variable by 5.

These are the bare bones basics of GML. If you have any questions about your code you can post them below in the comments section or head over to the GameMaker Community. GameMaker has a large community full of people willing to answer your questions so feel free to ask!

You Might Also Like:

  • I Interviewed True Valhalla!I Interviewed True Valhalla! GameMaker has changed extensively over the last few years. Some of these changes are good, while some of them are not […]
  • You Can Make PS4 Games!You Can Make PS4 Games! Today at GDC, YoYoGames, the company behind the widely popular game development engine Game Maker, revealed that in […]

2 thoughts on “The Basics of GML

Comments are closed.