GML Programming basics in 20 minutes. (cheat sheet)

face

Member
GM Version: Studio 2.3
Target Platform: Windows
Download: see code below
Links: n/a

Summary:
In this tread I will teach you the basics of programing with GML.

Attention: Useful information will be available here but you will need to use this regularly to get decent at it.
What not to expect after reading:
  • Being able to work for google without a degree.
The above is true with the exception of working as a professional janitor.


In this thread we will see:
  • how code works;
  • if statement;
  • while loop;
  • for loop;
  • functions
  • data types

How code works:

Code works with logic:​
If a then b​
a is true.​
then b is true.​
If I'm in my computer I'm at home​
I'm in my computer.​
Therefore I'm at home.​
Now we implement that in code. The more complex the code is, the more math it uses (that's why highschool math is important).​
Please not that working with code is not that easy. Things have an event order. GMS2 has two things that alter the order of things:​
  • object events (more used in recent and advanced programs);
  • code timeline (specific for every program in existence).
Talking about object events is going out of focus for this tread.​
Somewhat usefull: Imagine that computers speak a language, like english or spanish, but you can't speak that language.​
GML, like many other languages, is used to translate actions with human language to machine language.​
So I will explain the code timeline:
Imagine your code sheet like a paper where you will write your story.​
This is my story:​
A man worked in eletronics with his friends. One day, Jason and two of his friends, constructed an amplifier that made him deaf.
This story is badly written. Who is Jason? The man or a friend? We can assume that the amplifier was too loud and that made him deaf but the computer can't, just like he can't understand who is Jason.​
So let's try again:​
Jason Picno is a man that worked in eletronics with his friends. One day, Jason and two of his friends, constructed an amplifier that was really bad, when he disconnected the sound input, the interference was so bad that it made him deaf.
So much more information. Now we know everything to the story, evey character, what happens, how it happened, etc.​
If I were to translate my first try at that story in code, it would look something like this:​
Code:
var friends  = 2;
var amplifier;

amplifer = jason + friends;

var jason = 1;
Okay, what the hell is that? This code doesn't work. Why? Because of the timeline.​
Computers don't like being suprised, they are not like us, either you tell them everything before or you get a compile error.​
Take a closer look, in that code, at the operation. It should make sense that the amplifier variable equals 3, right? No!!​
You have to tell the program what the variables are before using them.​
Programs read code each line at a time. When it gets to the 5th, it doesn't know how to add the number 2 to a undefined value.​
In other words the computer is trying to do: 2 + inf.​
Now let me transform my second try at that story into code:​
Code:
var jason = 1;
[INDENT]var friends = 2;
var amplifier;
//every variable that i'll use exists
//before i call them

amplifier=jason+friends;

If statement:
When I get home, if there's food in the fridge I'll eat it all, if there isn't any but the restaurant is open I'll order 20$ worth of porkchops, otherwise I'll just watch TV.

Okay what's this, you ask?​
It's the if statement in the english language.​
To put it in code it would look like this:​
Code:
//get home event
var food_fridge;
var restaurant_open;

if(food_fridge) { //here you either can write "food_fridge==true" or like i did
eat(all);
} else if (restaurant_open) { //same thing here
    order(20$,porkchops);
} else {
procastinate(watch_tv)

]
//eat(all) means i'm calling a function called eat
//with the parameter called all

//a function isa piece of code that performs a
//calculation that you can summon few words
// instead of writing every single line you can
//call it with just a few words
Somewhat useful: If you want to calculate 5 to the power of 3​
in c language you need to use a function called pow() and write​
the parameters "pow(5,3)". The pow() function, in c, is:​
Code:
//GML
function pow(base,power)
{
    var i;
    var output;

    for(i=0;i<=power;i++){
        output=base*base;
    }[/INDENT]
    return output;
}
Code:
//C
]float pow(base, power)
{
    int i;[/INDENT]
    float output;

    for(i=0;i<=power;i++){]
        output=base*base;
    }
    return output;
}
Pretty similar languages.​
Do you understand the logic of the if statement? I want to ask you if you understand why languages exist and why there are so many?​
Each language is good in a certain aspect, and people develop more and more each day to satisfy their needs, saving them much time and resources.​
While loop:

A loop is something that happens multiple times. Either forever or until you want it to stop.​
The while loop, although basic, is very usefull to run any kind of program.​
You need a loop to run a game for example.​
I'm gonna go a english to code translation again so you can better understand.​
While I'm in the bathroom, I always lock the door.
In code:​
var a=bathroom;
[var door_lock;

while(a){
door_lock=true;
}

//or, more specific
var game_stop=false;

if(player_pressed_the_esc_key){
game_stop=true;


while (game_stop=false) {
//command here
}

//this could be the start of a game
//games are made of loops
//you're stuck in that loop until you press the quit button
//and that changes something in the code of the game
//in this case it was the game_stop variable
//turning it to true and closing the game

Ok, right now we can conclude that inside the parenthesis go the conditions.
If there's and "=" sign or equivalent, it's a condition.
Inside the curly brackets go commands.

For loop:

While if and while use one condition, the for loop uses three.​
The for loop uses more that one statement.​
I have no gas. I need to find help to survive.
In code, that would be:​
Code:
var gas;
var death=100;

for(gas=0;i<100;i++) {
    find_help();
}
When you're thinking about how to use the for loop you can remember the if statement and while loop.​
The first condition is an if, the second a while and the last is​
something that happens everytime the loop makes a cicle.​
Ine the previous case:​
if (gas==0)​
while (i<100)​
do(i++);​


Functions:

Don't get fooled by it's name. They are not fun. If you find yourself in the need of using functions just pray.​
In GMS2 most objects are rather simple. Most people only use a few lines of code.​
But if you ever need to create an object so complex that you'll lose yourself in it you will need functions to find yourself.​
Usually there's always a function called main where the main thing is going on:​
Code:
function main()

{
    var userinput:
    var seed;
    var output;

    output = calculaion(userinput, seed);

    //in this main function I call anbother function
    //called calculation, i made it and it makes
    //a bunch of nonsence sums and multiplications
    //and the funtion returns the result
}

//the main variables that are transported
//to this function are userinput and seed
//here their names change because i want to
//but i can easily tell which one is which
//because they're used in the same order
//when calling this function in the main
//a = userinput
//b = seed

function calculation(a,b){
    var output;

    //here i equal the formula to a variable to just
    //write return "var name" instade of writing the
    //whole formula
    output = (9877 * (a / 3)) / b + 1;

    return output
}
Attention: Functions can be used in several events of several object but in this case, the code is used in one event of an object. This is meant to be used for one object only.

Global variables

This is the easiest part to learn.​
Global variables are the ones used in the whole game. The ones you need to know their value in more than one object.​
They are interobjectionals (my attempt of saying international if objects were nations).​
they are called like this​
Code:
globalvar a;
globalvar b;

//Declaring the value of this type of variable
//doesn't work if you declare it in the same line
//You must declare it separately like i did with
//the b variable

b=1;

But...,
There are other types of global variables.​
In my functions demonstration, I mentioned that every variable there was exclusively used for​
the function where it was declared and could not be used outside of that function.​
Global variables for interfunctionality (international if functions were nations) are defined outside of functions.​
Here's the last code I showed but with every variable as global​
Code:
    var userinput:
    var seed;
    var output;
    var output2;


function main()

{
     output = calculaion(userinput, seed);

    //in this main function I call anbother function
    //called calculation, i made it and it makes
    //a bunch of nonsence sums and multiplications
    //and the funtion returns the result
}

//the main variables that are transported
//to this function are userinput and seed
//here their names change because i want to
//but i can easily tell which one is which
//because they're used in the same order
//when calling this function in the main
//a = userinput
//b = seed

function calculation(a,b){

    //here i equal the formula to a variable to just
    //write return "var name" instade of writing the
    //whole formula
    output = (9877 * (a / 3)) / b + 1;

    return output2
}
Did you notice what happened to the output variable in the second function? I had to change it, exacly because of the downside of using global variables.
I cannot repeat any of them. So what I can do, in this case, is put the "output" variables local to it's functions and the rest can be global.

Data types:

This part is very easy to understand.​
Computers need to id their data with different types.​
In GMS2 I believe there are four (if there are more, these are the only one's you'll ever use/need):​
  • int (integer numbers: 0, 1, 2,...)
  • float (floating point: 1.56, 6.44,...)
  • string (string of text: "Hello World")
  • boolean (works as a lie detector that you contol. can have two values: true or false)

Although you declare int and float variables with: var variable_name = 5,​
for a string you need to put the text inside quotation marks, either double or single:​
Code:
//no need to specify data type

int a=5;             //wrong

var a=5;            //correct
var b='Hello World'    //correct
var c="Hello World" //correct
Important:
Gamemaker has build in functions that are adjusted to GML and GMS2 functionalities.
You can see all of them
here. You have a searchbar that helps you find what you want
along with a presentation on all of GML documentation.


You have made it to the end!

I appreciate reports of spelling mistakes.

I like to teach these things, but sadly this thread talks about everything
I think a begginer needs to know to get started.
Maybe in the future I'll do more...
I would use this as a cheat sheet for the if, while, etc. statements
until you learn how they work.
The more you use them, the easier they get.
Feel free to ask any querstion.

Depending on feedback I might post more tutorials on GML
Until then, hope this helps! ;)
 
Last edited by a moderator:

FrostyCat

Member
This tutorial is so sloppy and misleading that it makes my blood boil. It has NO BUSINESS being labelled for GMS 2.3, and the author has NO BUSINESS being an instructor. It's clearly written by someone whose understanding of GML has never progressed past GMS 1.2.
  • The code is inconsistently indented, and many semicolons are missing.
  • Exponentiation in GML is already provided by the built-in function power.
  • It's invalid to block the game in a closed while loop and terminate it by checking keyboard input.
  • Your description of for loops is completely wrong, as is the example code.
  • The denigration of functions is completely needless, and there is no coverage of object-level functions as introduced in GMS 2.3.
  • globalvar is deprecated and should no longer be taught. The second example under that section is also wrong (notice the variables declared using var at the top being used inside the functions).
  • The undefined, array and struct types have been completely dismissed.
  • The single-quoted string literal form has NOT been accepted in GML 2 since its start (it is now @'...')
I've had enough dealing with the fallout of HeartBeast's tutorials in GMS 1.4/2.2 and recently FriendlyCosmonaut's tutorials in GMS 2.3. I hope I don't have to deal with any more fallout from this one as well.
 

face

Member
This tutorial is so sloppy and misleading that it makes my blood boil. It has NO BUSINESS being labelled for GMS 2.3, and the author has NO BUSINESS being an instructor. It's clearly written by someone whose understanding of GML has never progressed past GMS 1.2.
  • The code is inconsistently indented, and many semicolons are missing.
  • Exponentiation in GML is already provided by the built-in function power.
  • It's invalid to block the game in a closed while loop and terminate it by checking keyboard input.
  • Your description of for loops is completely wrong, as is the example code.
  • The denigration of functions is completely needless, and there is no coverage of object-level functions as introduced in GMS 2.3.
  • globalvar is deprecated and should no longer be taught. The second example under that section is also wrong (notice the variables declared using var at the top being used inside the functions).
  • The undefined, array and struct types have been completely dismissed.
  • The single-quoted string literal form has NOT been accepted in GML 2 since its start (it is now @'...')
I've had enough dealing with the fallout of HeartBeast's tutorials in GMS 1.4/2.2 and recently FriendlyCosmonaut's tutorials in GMS 2.3. I hope I don't have to deal with any more fallout from this one as well.
And you have no bussiness telling me what to do... Who do you think you are? I can clearly tell that this thread is not for you and I would be in the same way you are if I read something as basic as this.
And I'm sorry but you missed the point of the tutorial. Most of the arguments you make don't even make sense. If you can't read and preffer to complain before understanding that's your problem.
I feel like discussing this further is pointless.
 
Top