Hi again, all!
I've realized that every time a newbie comes, they don't quite understand coding, even while making a game. When they ask something on the forum (which happens pretty often...) and give them a coding answer, they often don't really know what they were given. So I thought I'd make this topic specially for them, so they can understand the codes we are talking about.
Let's start out with the fundamental basics of all: variables.
1. What's a variable?
Answer: It's a place where you store some information in the computer. This will allow you to make switches, save information in-game, for later use, and it has so many uses, it really depends on what you want to do.
They're the very base brick of programming, since most things are accomplished with them.
Great! But how can I make such a "thing"?
Answer: When you open the script editor, at the bottom, you'll find a "variables" button. Open it, and click "Add". At the top, choose a name for your variable. If you want to change the information this variable contains, you use this name. Make it descriptive, so you don't have to go through head-aches just to find out which variable's value you want to change.
Below you can choose the type of your variable. You can't assign an integer (a whole number, like 1, 34, 1924, -23 etc.) to a string (any kind of text) variable, neither can you assign a float (a number, not whole, 0.123, 0.5, -23.9, etc.) to the others. As a matter of fact, you can only assign text to a variable (of string!) with the strcpy function, talked about later.
The next shiny button is set to "Global" on default, but there is a possability, to make it "Actor variable". you might say as a newbie. Here's what that's for: Each variable has what is called a scope (expert term), which defines where you can use the variable. The best way to describe is by example:
Let's say we create a variable, called tutvariable, and say it's an actor variable. Note, we're creating it in "player" actor. Now if I go and use this in actor called "platform", then I'll be given a nasty error.... Why? I didn't use it in "player"! How can we solve the problem? Create another actor variable with the same name - and type - for "platform". If you would create a Global variable, then under that name ("tutvariable") they'd modify the info of the same variable. Creating several Actor variables of the same name, then the actor only modifies it's OWN variable.
2nd perspective: A Global vaiable can be modified by any actor. The local/actor variable can only be modifiedby the actor it was created for.
Arrays.... O.o! This is tough for a newb! If you only want a simple variable, then keep that button "No" as default. Otherwise, here's what an array is:
An array is basically a group of variables we put together in a stack, cause they have a similar use. This means, that if I make "tutvariable" an array, then it actually means it's several variables. How many variables are in the stack? You define it with the "size" box. This can be any integer/whole number. What are their names? If you made a variable an array, then it should be called like this variablename[5] Note that will work if the size is minimum 6. Because the first variable's name in it is variablename[0] !!! It's like animindex. This is useful ig let's say we have a map. Every place on the map has a height value. let's say the map is 4x4, so the variable size should be 16, so the array index (the number of the variable you call in []) will be from 0-15. That's about it... This isn't always usefull, most of the time, you'll be making a variable, not an array.
Next is the save group. Let's say you want your game progress to be savable. The way to make it savable, is to make all the variables that contain information about the game's progress (points, level number, player's position, lives, etc.) should have the same value for "save group". This way, when you save the variables (Use savevars function, using the name of the group you saved the variables to, and a filename giving the name of the file on disk, where it saves). So when you close the game, then reopen it, then load the game, everything willl be the same as left.
After you've set those to what you want, click "Add", or elegantly press enter, and you've created your variable!
To edit a variable's type, scope, save group, or edit the array's size, or wether it's variable or array, find your variable by clicking the bar next to "Variables:", then when you've selected the variable click "edit". To remove a variable, select the variable as described, then click remove.
Now I have told you all the properties of a variable. But how can you change them in code?
IF it is an integer, or a float, then there's a very easy way to do this:
variable name = number;
That is if you want to set the variable's value to another variable's value, or a number.
If you want to add to the number, I recommend:
variablename += number;
In place of number, again, you can put a number or a variable too.
subtract:
variablename -= number;
same story...
To multiply variable by a number use *= instead of -=, to divide variable by a number use /= . Otherwise, if the new number you want to assign to the variable IS NOT in relation with the variable's current value, that is: variable not = variable plus, minus, multi., divide some number, then here's what to do:
variable = number + number;
variable = number - number;
variable = number * number;
variable = number / number;
And yet again any of those "number"s can also be a variable.
IF you variable ISN'T AN INTEGE/FLOAT, that is, it is a STRING! then:
strcpy(variable1, "I'm the text, that'll be assigned to variable1, and can be a variable myself. I'm not supposed to be longer than 255 characters");
The code says it all...
That's about what you need to know about variables. If you truly understood what I've written, then you should have no problems with them now. Go to post 2 of this topic to learn how to do conditional actions.