Page 1 of 1

How to understand the use of variables

PostPosted: Tue Dec 16, 2014 1:10 pm
by mog440
Hi,
One of the hardest things I have found (so far) in GE is trying to understand the use of variables.
It is one thing being able to declare a variable, but when and how to use them seems a whole different story.
Just wondering if there is a good tutorial on the use of variables, or if some kind person could make one. Something explaining what to do and the reasons for doing it.
I am just starting to understand variables and it is getting a little complicated, with the if's, else if's, case 0's, breaks, ==1's etc,
There has got to be an easier way to grasp it all than downloading demo's and reverse engineering them to suit?
Thanks, Mog.

Re: How to understand the use of variables

PostPosted: Tue Dec 16, 2014 10:03 pm
by Hares
For me variables are like boxes. You use them to store stuff in.

When you create a variable, it is empty - so it is like an empty box.
Then you can assing a value to it - you fill the box.
Then you can read the value at some other time, or from some other code - you look what is in the box.
Or you can change the value - you take out what is in the box, and put there something else.

In game-editor there are two kinds of variables:
Global:
These variables only exist one time in the game. An example is the score (in a one player game)

Actor:
These variables exist for each actor in the game. An example is the x or y position.

Game editor comes with a number of predefined variables:
http://game-editor.com/Variables

But you can create as many other variables as you like (or need).

How to create them, and what kind of variable can you make?
See this link http://game-editor.com/Creating_variables

So the only variable that doesn't really fit the box description is the array.
An array is more like a closet full of boxes.
So you can have an array with a size of 10, that means you have 10 boxes in the closet.
Then you can fill each box with a value, and do all the stuff you can do with "normal" variables.
Just keep in mind that the first box in the closet is number 0. So the tenth box is number 9.

I hope this makes sense :D

Re: How to understand the use of variables

PostPosted: Wed Dec 17, 2014 1:53 am
by gamemakerdude
Couldn't have said it better myself, but I would like to add, I tend to think of a variable's name as label on the box also, because when I box up a bunch of stuff that I will later have to go thru to find certain things I generally label it so that when I am looking for something later, I know where to look for it. Such as a box named "Bathroom Stuff" or whatever. My particular point being, you want to name variables according to the data they pertain to so that when you want to manipulate it, the names come to mind easily. This may seem like common sense, but you may have many variables with similar names in a large project so it is important to know what data they keep track of.

Re: How to understand the use of variables

PostPosted: Wed Dec 17, 2014 12:35 pm
by mog440
Awesome! Thanks so much for the replies, I am beginning to understand. Might take me a while tho :roll:
A very Merry xmas and new year etc, thanks for your help :)