GML Blog‎ > ‎Fundamentals‎ > ‎

Your First GML Code Segment

If you haven't started writing code in Game Maker, your at the right place. This article will explain how to code. You will learn what you need to know to get started. This article will not only teach you GML (Game Maker Language) from the ground up, but also give you a very organized and methodical view of how to write code, and write it well. Many tutorials jump around all over the place leaving out these details because they are not "necessary". They may not be necessary to the demo they use to teach you, but if you learn bad form from the beginning, it will stick with you for longer than it needs to. Let's jump right in to an example of some GML!

material = "Glass"

age = 32

Yay! We just made our first variables. There are two variables in the code above. The first is material and the second is age. Variables can hold information. When a variable holds a number we call that variable a real. When a variable holds a word, letter, or sentence, we call it a string. The variable material is a string because it's holding the word "Glass". The variable age is a real because its holding the number 32. Objects in Game Maker have variables before you even declare them. X, Y, speed, gravity, image_single, image_speed, and a whole bunch of other variables are included in every object. Each of those variables feeds what is called a function. We won't get into functions just yet, but they are a very important part of programming.
    All we have done so far in our code is set two variables. Lets set them based on what they are at the current moment. Lets set a rule. If the material is "Glass" then lets make the age 20.

if material = "Glass"

{

    age = 20

}

The code above asks if material equals "Glass" and if it does then it makes age 20. Any set of { and }'s will execute the code written inside of it if the statement above it is true. Since material really is "Glass" (Because we made it "Glass" earlier) the statement is true, and so age will be turned from 32 (Because we made it 32 before) to 20. Notice how I wrote the code above. I could have written it like this:

if material = "Glass"

{age = 20}

For consistency, I highly recommend for you to write code the same way I do in this article (Not including the code in red). I follow a very popular and logical programming format that has helped me keep all of my projects organized, and easy to read. There are other formats out there like:

if material = "Glass"{

age = 20}

Or the dreadful single line which I suggest never to use:

if material = "Glass" { age = 20 }

Okay, we've started off real slow to get the basics down. I'm going to ramp up the speed a little bit. Here are some pieces of code and explanations of what they do. You should be able to deduce what is going on and learn from my examples:


if material = "Metal"

{

    if age > 10

    {

        age = 10

    }

}

The above code checks to see if material is "Metal", then if it is, it checks to see if age is more than 10, if it is, it makes age 10. So basically speaking, if the material is Metal, make sure to limit the age to a maximum of 10. Notice how the if age > 10 block is "nested" inside of the other one. This is called nesting. It is used very frequently in programming in general.  Your now ready to make Your First GM Game

Affiliates