Login | Register

Variables constants and computing

August 24, 2006, 16:40 by Calle
[loading]
-->
If you have ever programmed before you have probably encountered variables. These are a word or any set of characters that are assigned a value which it will refer to. You may call this word anytime to read the value and use in different computing operations. There are two types of variables global and local and they are assigned like this:

global.variable = 0;
variable = 0;

You may call global variables from whatever object or instance that you please, but must be more careful with local variables which may only be called from the instance which they were assigned in. Except for this difference they are the same and they can contain two different kinds of values. Either do they contain numbers, then they are called "real values", or they contain a string which may be any set of characters but also numbers, or the combination. This example shows the difference:

global.variable = 1337; //A real value
global.variable = "1337"; //A string value
global.variable = "this is a string"; //A string value


As you can see a string is always encapsulated with " ". There are two functions to convert string values into real values and the reverse; string(argument) and real(argument) where the argument is the variable to be converted. Note that the variable that should be converted into a real value may not contain any characters, as this will cause errors.

The advantage with real values in difference to strings is that they may be used in computing operations. GML has a lot of functions that deals with math, which is often necessary in a lot of different situations (e.g. calculating speed or score):

sin(x), cos(x), tan(x), sqr(x), sqrt(x) etc. One very useful functions is the random(x) function which will return a value lower than x, usually rounded by one of the functions floor(x), ceil(x), round(x).

To return to variables: the advantage of local variables is that with them you may treat each instance among several instances of the same object as individuals. You may for an example execute the code nr = floor(random(6))+1; in the create event of an object that should depict a dice. If you set their sub image to nr (image_single = nr) they will get different sub images, all of them. If you assign a global variable the value and choose depending on it, all of the dices in the room would get the same number (as each time you assign a variable it overwrites its previous value and a global value is set from all instances over and over again).

You can address local variables too, if you want to read them from another object you just type object_name.variable_name , in other words, you replace global-dot with the name of the object and then dot. I assume you can imagine yourself global as an object and that each time you use global.variable you actually address the value to the invisible object global.

There are variables though that you don't need to initialize. This is because Game Maker does that automatically, example of these are id, image_single, speed and direction. The first one, id, is also called a constant as its value is constant and may never be changed. A instance is equal to its id, thus this is another way to call (set or read) a local variable in another instance:

other.sprite_index = sprite5;
all.speed = 0;
global.message = 'A good result';
global.x = ball.x;
(100032).speed = 0;


x and y are other examples of values that are automatically assigned to each instance. These might be the most important as well, as they decide the position of the object. I have kind of made the assumption that you who have bought this book is quite familiar with the interface and I think you already know how x and y affects the position, but to those who doesn't, let me explain. Position (0,0) is at the top left corner. The first value, value x, is the horizontal axis, y is the vertical. In each step each object, each instance, is set to (x,y).

Now let us do some calculations (I use custom variable names, remember I told you variables could have any name?):

mynumber = mynumber+5;
gravity = gravity-0.3;
score = score*score;
hello = hello/2;


How this piece of code works is quite obvious, which is why I started with it; each line of the ones below does exactly the same thing as the corresponding line of code in the piece of code above:

mynumber += 5;
gravity -= 0.3;
score *= score;


This is, for those who has earlier not seen it, confusing, but in fact, it's very easy. You simply put the arithmetic character (*, /, + and -) before the =, thus make the calculation "relative". Let me repeat, they do exact the same thing as the earlier piece of code where we wrote = variable+5 rather than +=5. Its only advantage is that it is shorter to write, and it looks more professional as well.

One-dimensional and two-dimensional Arrays
Sometimes when you want to save a large amount of data you will find it inconvenient to use different variables for every piece of information. This is especially true when dealing with an <i>unknown</i> number of data. This can best be explained by an example.

Say for instance that in you game the player can save comments on what he does, so he types something into a box and then press save and then you want to save it in the memory such that it can easily be accesible from the game, but you have no idea how many comments your player will save. You could have a variable comment1, a variable comment2 etc. and prepare for a twenty different comments with a twenty different variables, but this I what I mean with inconvenient. What if you could save 32 000 different values within the same variable? That's exactly what you can do with an array.

A variable is defined as an array by adding an index to it, which looks like this: "variable[i]" as opposed to just "variable". In fact, when you assign something to variable, like variable = 0; that would be the same as variable[0] = 0, but then since any variable can also be an array you can go ahead and also assign variable[1] = 5; variable[10] = 7; ... and so on up to a 32 000 different indexes.

for (i=0; i+=1; i<10) {
myvar[i] = i*2;
}


The above piece of code will assign the following values for the variable myvar in the given index. [0]=> 0, [1] => 2, [2] => 4... can you figure out the rest?

Two dimensional arrays work the same way but can take two variables. Any variable assigned two values is also called a "vector". It looks like this variable[i1,i2]; and it works just as an array. The indexes must be between zero and 32 000. Within a two-dimensional array you can save 1024000000 different values...

If you want to know more about saving data during gameplay you should look at data structures, which will provide a good complement to variables.

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games