GML Manual
GML Manual
Index
[hide]
1General Syntax
o1.1Functions
o1.2Variables
. 1.2.1Types
. 1.2.2Scope
General syntax[edit]
The languageGMLin terms of syntax it is similar toC++, nevertheless, this language consists of
a lot of flexibility due to its various syntax variations (all of which are valid).
For example, when working with operators, it can work like this:
g=g+1;
g+=1;
variable=<value>;
In this case, the variable is already declared and can be used generally, however, GML
it also includes a "var" block mimicking the variable declarations of other languages,
being able to declare a number of variables in the program without the need to assign them a
value (if not, it is automatically0 ). For example:
var xx,yy,ss;
Game Maker also works with constants, which can be assigned from a function.
of the program (plus those that already exist therefore).
Within the GML code, there are two types of working variables, the variables
local variables global. Local variables only work within a
"object", that is, it does not exist outside the "object" or code block(en caso de haber sido
declaradas con "var") que la trabaje (lo cual permite usar el mismo nombre de la variable en
another object). On the other hand, there are global variables, which use the
prefixglobalbefore the variable name, for example:
global.variable=0;
Global variables are used throughout the program, so there can only be one.
global variable with the same name, and its value can be affected from anywhere
program/code parameter.
GML can also handlearraysin a very simple way and very similar to C++.
They must be declared in the following way:
array[index1,index2];
ty_string
Boolean are used to distinguish a character of type true or type false, it does not store any value.
Real Values are signed floating point numbers. That means they can
be positive or negative numbers and may have a decimal part. Since version 6.1,
GM allows the representation of hexadecimal values as reals in the code, if
preceded by the character $. In version 6, the handling of real values had abugwhat
it caused precision errors when working with high real values. The bug also exists.
in GM 7 and 8, but it was mitigated with more precision.
ty_real
Since GML does not have boolean values, statements that require boolean values (such as if)
they will evaluate any real value greater than 0.5 as true and any real value less than or
equal to 0.5 as false. In GML there are also constants for
defecttrueyfalsewhich correspond to 1 and 0 respectively, and can be used to
make the code more readable.
Scope[edit]
In GML, there are two types of local variables: instance-local and script-local.
any other piece of code that has its own container). Be local to an instance
it means that this variable is tied to that particular instance and that to be called from
another instance must use as a prefix the name of the instance that contains the variable. Be
Local to a script means that the variable can only be used in that script and that upon finishing
once this is processed, the variable expires. The term 'local' without further specifications, usually
it refers to a local instance.
By default, a variable is local to an instance but not local to the script in which it is used.
To make a variable accessible to all instances (and scripts), it can be defined
using the global prefixglobal.foo=bar; or declared explicitlyglobalvar
foo,bar;Using the first form, the variable must always be called using the
prefixglobal.the statement withglobalvardoes not require any prefix. To make a
a local variable in a script uses the keywordvar: var foo, bar;.
One can access a variable that is local to one instance from another instance using
how to prefix an instance identifierinstance.variableThere is no way to access
a local variable of a script from another, unless the variable is passed from script to script
as an argument.
An instance can execute code in another instance using the "with" statement. For example,
the following piece of code, placed in a collision event, can be used to destroy
to the instance that collides with the current one. When a collision event is triggered, Game
Maker automáticamente crea la variable "other", la cual se refiere a la instancia envuelta en la
collision.
var foo;
foo="bar";
with (AnotherInstance) {show_message(foo);}
I will teach the programming language with small practical examples that
you can take advantage in any game you make, and this way we see that the code is
usable in many cases.
At the same time, I will also start adding the tutorials. I will begin with the first one.
Tappy Plane game, this way we see in a very simple way how to create a mini
game, which can be perfectly valid for a website or a game of
mobile.
Code Remember that you can also combine your written code with
other actions that we have dragged.
. Code to create the room. We already saw that from therooms editorwe can
put specific code in the room. We can say that it has its own
Conclusion
We have made a small explanation of the importance of programming with code.
With a brief introduction, we have discussed the programming language.
what we can use with Game Maker Studio. In the next post, I will talk about the
variables and how we can use them in our code.
Program.
A program is a set of instructions called statements, which are
interpreted by GameMaker: Studio with the purpose of making something happen inside
from a game. That 'something' can be as simple as adding 2 plus 2 to
to obtain 4, or as complex as making an enemy flee when their health drops from
certain level. The structure of a program varies enormously depending on the
functions it uses, but reducing it to its most basic structure, the representation of
any program would be:
{
<statement>;
<statement>;
...
}
A program must begin with the symbol '{' and end with the symbol '}', and between
these two symbols locate the sentences, each separated by a symbol of
punto y coma ';'. Ahora veamos un típico programa de GML , más precísamente un
program created in theScript EditorGameMaker:Studio
There are various types of sentences, which will be discussed in the following
sections of the manual.
Like any other programming language, GML uses variables as the unit
basic for most programming operations. Variables are used
to store information in memory for later (or immediate) use. They are
assign a name to be able to call them in functions and programs. A variable in
GML can store a real number, (like 100, 2.456575, -56, etc.) or a string.
Hello, world.
localThese variablesthey are declared using the function 'var'. A local variable
it is only valid within the event or script in which it is created. In this
case GameMaker: Studio will create the variable, it will be used for the duration of the
event and then it will "forget" about her, which will cause the error to occur
unknown variable if you try to use it later.
global:A global variablepbelongs to the entire game environment, and not to one
specific instance (even though it is declared within an instance). In a
Initially, it has to be declared as global, but after this, any
instance can read or change its value. The value of the variable will always reflect
the last operation in which he was involved, regardless of what instance it developed
operation.
Internal variables: They are special variables inherent to the objects and
rooms in the environment. They have been there since a room or object is
created. They can have instance or global scope, but not local scope. There is a
a large number of these variables, each with very specific uses, are
describe con detalle a lo largo del manual en las secciones correspondientes.
Instance Variables
The scope of an instance variable is limited to the instance where it is defined.
create.
An instance variable is created within the instance of an object and is considered
unique for that instance: many instances of the same object can have the same
variable, but each one can store a different value, since those variables are
unique to each instance. How do you create an instance variable? You only need to
assign it a value, as shown below:
pociones = 12;
vida = 100;
nombre = "Juan Pérez";
fuerza = 5.5;
armadura = -2;
life -= 5 + armor;
If life 'life' is at 100, its value will change to 97 (100 - (5 + -2) = 97). This is a
simple example, you could replace 'armor' with the value -2, but what happens if
Do you use that value in different places and decide to change it? It would have to go everywhere.
code and change each -2 to the new value, which takes time and can lead
To errors! But when using a variable, all that needs to be done is to reassign it a new
value, which will be used automatically in the code, increasing flexibility and
facilitating error correction. Even if the value were not to change, it is easier
remember what use a variable called 'life' has that only shows a number.
Local variables
A local variable is declared, used, and then discarded.
Local variables are only created for specific events, when that event
I finished, the variable is discarded (The only exception to this is scripts, where a
a variable declared as local remains local to the script, and then it is discarded). Why
What would we need it for? Variables take up space in memory, and it can be
that we will only use them for an operation or function, in that case it is only
necessary to keep them in memory only for the time they are used. This keeps the
clean and orderly base code, while optimizing memory space for
the things that are truly necessary. To declare a local variable we use the
functionvarin this way:
All the variables from the previous example will be 'forgotten' (will disappear from memory)
at the end of the event (or the script) in which they were created. You must be careful and not give
a local variable the name of an instance variable already created within the object
executing the code, and also, not intending to use the value of the variable
local outside the event where it has been declared. These variables are used in a way
frequent, especially in cycles, to count iterations, or when a value is used
recurrently in an operation that will not repeat itself. Here a couple of
examples:
var i = 0;
repeat (10)
{
inventory[i] = 0;
i+=1;
}
The previous code creates a local variable named 'i' and initializes it to zero, and sets it
to 0, everything in the same line. In previous versions of GameMaker it was necessary to declare
first the local variable first and then assign it a value, but in this version
you can declare and assign a value at the same time. In the example, the value of the
variable is used to initialize an array. Since the variable "i" will not be used elsewhere
subsequent operations may have a local scope. Here is another example:
var xx,yy;
xx = x - 32 + irandom(64);
yy = y - 32 + irandom(64);
instance_create(xx, yy, obj_blood);
Here the local variable has been used to store random coordinates used for
create an instance. It can be seen that it is not strictly necessary to use these
variables, but for the sake of clarity and ease of use we do it this way. It is much
more clear and obvious what is being done, than using code like this:
A detail about local variables: Being unique to the event that executes them,
It is also possible to use them in other instances through code! That is, we can
use local variables to adjust and change things in other instances through the
statement "with()" (there is a section about this in section Generalities of
language in the manual). The code would look like this:
This code works because the variable "num" is local to the event (or script) in which
It is contained, NOT IN THE INSTANCE NOR THE GAME ENVIRONMENT, which allows it to be used.
in any function in any object as long as it is in the same block of
code.
Global variables
In simple terms, a global variable is a type of variable that, once declared,
does not belong to any instance, but can be read by all of them. The variables
globals must be declared, just as happens with local variables, but at
the difference between these, a global variable remains in memory until the end of
game. For example, a global variable can be created to keep track of
player's bullet count and simply update this variable in different
game points. A global variable does not belong to a particular instance and
it can be read, changed, and used by all instances at any time,
but any change made to the variable is also 'global', that is, the change
It affects all instances that use this variable. Let's see an example of this:
globalvar food;
food = 5;
we have created a new variable called "food" which has been declared as
"food" is available for any instance, for example, there could be a
object "food" that the player collides with, and in that collision event
we would have
food +=1;
We could have another object that draws the value of "food", this way:
Through global variables we can change values and see those changes reflected in
all instances that reference those variables. In the same way that
with local variables, care must be taken not to name them the same way as
any instance variable, as this would cause problems and it would facilitate the
appearance of errors in the game. As a resource to avoid these inconveniences,
we can call global variables using the reserved word "global" followed by
from a point '.' before the variable. This is illustrated below:
global.food = 5;
Con este método, debemos usar la palabra "global" cada vez que deseemos usar la
variable, for example:
global.food += 1;
draw_text(32, 32, "food = " + string(global.food));
oscore
ohealth
olives
score
Global variable to store the game score.
Syntax:
score;
Return: Real
Description
This variable has a global scope and is used to store a numeric value that
it generally refers to the score of the game, however, it does not necessarily have to
Just because the name is 'score' does not mean it has to be used.
for the marker... it can store the value you want.
Example:
if place_meeting(x, y, obj_Bullet)
{
score += 10;
instance_destroy();
}
This code checks the current instance to determine if there is a collision with any
instance of the object "obj_Bullet", and if there is one, add 10 to the global score and to
continuation is destroyed.
health
Global variable to store health points.
Syntax:
health
Returns: Real.
Descripción
This variable has a global scope and is used to store a numerical value that
it generally represents the player's health, but it should be taken into account
that this does not necessarily have to be this way. Although the name is
health can be used to store any useful numerical value for the game.
Example:
if place_meeting(x, y, obj_Enemy)
{
health -= 10;
if health <= 0 instance_destroy();
}
The previous code checks if there is a collision with the object indexed in the variable.
obj_Enemy, and if there is one, subtracts 10 points from the global health variable and then
determine if the value of that variable is less than or equal to zero. If this is met
condition, the instance that executes the code is destroyed.
Accessing variables from others
instances
Several methods are described to call variables in others.
instances.
Previouslyit was described how to create and use variables within an instance, or of
globally, but what happens when you want to access from an instance a
variable in another different instance? There are many cases where this occurs.
situation, for example, if in a collision event with a bullet one wanted to know
how much damage does the bullet cause, reading one of its variables, or if one wants to stop it
movement of all the bullets in the room; or move the protagonist to a position
determined, to mention a few. Let's see how to achieve it through GML.
Using a point.
This method consists of using the object's name as an identifier, followed by a
dot '.', followed by the name of the variable to be changed or assigned. It is
to say
obj_ball.speed = 0;
What the previous code does is adjust the speed of ALL the instances of
object 'obj_ball', so this method allows changing a variable in all the
instances of some object. And what if you want to change the speed only?
a particular instance, instead of all? In that case, the principle is the same,
except that instead of using the object's name as the identifier for all its
instances, we use the instance identifier to tell it
GameMaker: Studio which we only want to handle a single instance.
(100012).speed = 0;
other collision
var nnn;
nnn = instance_create(100, 100, obj_ball);
nnn.speed = 8;
nnn.direction = random(360);
All of these are valid ways to read and adjust variables in other instances, and
It works because the dot is actually an operator. It takes a value as an operand.
left and a variable (direction) as the right operator, and returns the direction of
that particular variable in the indicated object or instance. All names of
objects and special objects from the previous paragraphs actually represent values,
These values can be treated like any other value. The Names of objects,
special objects and instance identifiers can be used in many
other functions, since GMS treats them as constants. write.
var nnn;
nnn = instance_create(x, y, obj_Bullet);
nnn.damage = 5 + irandom(5);
nnn.speed = 8;
nnn.direction = point_direction(x, y, obj_Player.x, obj_Player.y);
Note that we are adjusting variables using the point method before
explained. Now, what happens with the player object? How to make it know how much
What damage will be received? Using the event of collision (from the player object).
hp -= other.damage;
if hp <= 0 instance_destroy();
the above code will read the value stored in the variable "damage" from
the distance and will subtract it from the player's "hp" variable, then it will check if "hp" is
less than or equal to 0. If it is, it will destroy the instance of the player object. It is important
highlight that this use of others only works in the event of a collision and that it is necessary to
make sure that the variables of the other instance exist, or it will cause
error. However, through other means, values can be assigned to variables and even
crear nuevas , usándola en el evento de colisión así:
other.mana += 10; //adds ten to the variable 'mana' of the other instance
other.hit = true; //set the 'hit' variable of the other instance to true,
creating it if it does not exist
Assignments
An assignment is the method used to store a value in a variable. All
the assignment has the following structure:
<variable> = <expression>;
The simplest expression can be a simple value, but it can also be more.
complex (one or several operations). In addition to assigning values, we can also
add a value to the current value of the variable, using +=, for example:
a += b;
Similarly, you can subtract using -=, multiply using *=, divide
using /=, or using bitwise operators |=, &=, ^=. You can also add or
subtracting from a variable, using ++, --.