Thursday, January 23, 2020

GameMaker Studio 2 - Variables and Functions

2. VARIABLES AND FUNCTIONS

Lets take a look from GMS2 documentations :

Just like any other programming language, GML uses variables as the basic unit for most programming operations.

A variable is something that we use to store a value for use in one or more operations o functions.

Lets do some math.
1 + 2 = 3.

okay, now lets add
a + b = ..... ?

it is hard to answer.
how about
a = 1
b = 2
a + b = 3

A variable could be anything. lets take another look from the documentations, the example of variables :

potions = 12;
life = 100;
name = "Jock MacSweeney";
strength = 5.5
armour = -2;

Now, In many programming languages you need to "declare" a variable before you can use it. This basically means that you tell the computer the name you wish to use so that it can assign a place in memory to store whatever value you decide to hold in that variable. With GML, that is not always necessary as it depends on the scope of the variable.

Now, what about functions?

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

As in GameMaker Studio 2, GameMaker language gives you many built-in functions like move_towards_point( x, y, sp );, etc. but you can also create your own functions. These functions in GameMaker Studio 2 are called scripts.

Again, lets take a look at the documentations :

A function has an input and an output, and the output is related somehow to the input.
In GameMaker Studio 2 you use functions as part of the code structure to make things happen in your game and there are a great number of GML functions available to you, all of which are explained in the Language Reference section of the manual.

In GML a function has the form of a function name, followed by the input arguments between brackets and separated by commas (if the function has no input arguments then just brackets are used). Here is an outline of the structure of a function:

<function>(<arg0>, <arg1> ,... <arg15>);

As for the example above, the <function> is move_towards_point and the <args> are x, y and sp;



No comments:

Post a Comment