Scripting - for n00bs :o ;)

Talk about making games.

Scripting - for n00bs :o ;)

Postby Thanx » Sat Aug 16, 2008 2:24 pm

Hi again, all! :D
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". :roll: 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.
http://www.youtube.com/watch?v=XyXexDJBv58
http://www.youtube.com/watch?v=Be4__gww1xQ
These are me and playing the piano (second one with a friend/fellow student)
Hope you watch and enjoy!
User avatar
Thanx
 
Posts: 314
Joined: Sat Jan 26, 2008 10:07 pm
Location: Home sweet home! :)
Score: 24 Give a positive score

Re: Scripting - for n00bs :o ;)

Postby Thanx » Sat Aug 16, 2008 2:58 pm

Part 2 of the tut!

So now if you want something in script/code only to happen if some condition is true (or false) then there are 2 ways of doing it. One is the must-know if statement, the second is the case switch. For both you need to use an integer or float. However when using the if statement, there is a function that'll allow you to also use strings... in a way I'll talk about later.

First let's talk about if statements
if(variable ** value)
{
do some action...;
}
** isn't something you can use in code. I used it instead of some signs you can use there, to compare the variable witfh the number/value on the other side of the sign.
These can only be numbers, but now here's the signs you can put in place of **:
!= : The action between { } will be done if variable doesn't equal value.
==: The action between { } will be done if variable equals value.
> : The action between { } will be done if variable is greater than value.
< : The action between { } will be done if value is greater than variable.
>=: The action between { } will be done if variable is greater or equal value.
<=: The action between { } will be done if value is greater or equal value.
The number of actions you do when these are fullfilled isn't set, it can be thousands of lines if must. Also, you can put an if statement, or anything inside it. It's just another part of code that is only done if... But no restrictions.

Now if the variable that needs to be compared is a string, then use this function:
strcmp(variable1, "I'm the string variable1 will be compared with. If we're the same, then strcmp returns 1, else returns 0!");
That says it all.

Now that's only a basic if statement. What if you want several things to be equal for the code to be executed?
Well we know one test is: variable ** value . To add several of these, and if you want them to all return true, for code to be executed, then separate these tests by && . If you need only one of these tests to be true to execute the conditional action, then divide them by ||.

If you say the if something is value, then do this... But if not, then do something else, here's the code:
if(variable1 ** value)
{
actions....
}
else
{
other actions....
}

That's it for if statements...
switches...
A name that may make hair stick up on the back of a n00b. :D You'll learn that they're pretty easy to handle, and that they're quite useful too!
So what if you have 1 variable, and realize that a lot depends on it? Like it has 10 different values it can have, which each meaning a different action(s) to take place... That'd make if statements confusing, intimidating, and unfriendly to the CPU. There is a way! case switches!
switch(variable1)
{
case 0:
actions....;
break;
case 1:
actions....;
break;
}

You start out with switch, with the variable you want to check in brackets. No semicolon (;) at the end!!! And then as if statements, those curly brackets... case 0: means, that IF variable1 is 0, then do these actions that are listed till break;
break; will automatically make the rest of the cases ignored! So by using "case number:" you can list what should happen if a variable has a specific value. This time there's no way of doing this with a string variable. Note, that the number after case cannot be a variable, only a number!
Again, the great thing is that these also, as if statements do NOT have limitations on what you can put in a case! It can be another case switch, or more if statements, or just a few functions.
functions?
That leads to my next post on this topic, following with more about arrays, declaring variables, and functions, and the introduction to loops.
Thanks for the comments! I hope some of ya will be able to make use of these, though I know they're incredibly long.
Last edited by Thanx on Sun Aug 17, 2008 12:00 pm, edited 1 time in total.
http://www.youtube.com/watch?v=XyXexDJBv58
http://www.youtube.com/watch?v=Be4__gww1xQ
These are me and playing the piano (second one with a friend/fellow student)
Hope you watch and enjoy!
User avatar
Thanx
 
Posts: 314
Joined: Sat Jan 26, 2008 10:07 pm
Location: Home sweet home! :)
Score: 24 Give a positive score

Re: Scripting - for n00bs :o ;)

Postby BlarghNRawr » Sat Aug 16, 2008 6:50 pm

wow that was very helpful.. too bad im tired and my brain melted halfway through... :lol: Ill try to read it later when i need it lol :lol:
Download Game Music
current projects:
Bold! ?% Started mechanics
Unnamed puzzle game 60% --i need a name!--
User avatar
BlarghNRawr
 
Posts: 767
Joined: Wed Jun 25, 2008 12:36 am
Location: Not using G-E anymore. Now using Source SDK.
Score: 27 Give a positive score

Re: Scripting - for n00bs :o ;)

Postby Thanx » Sun Aug 17, 2008 2:42 pm

Part 3 functions, more arrays and variables, and the introduction to loops
Note: my second post has been modified, and now also includes case switches explained.

So as I mentioned, functions.
ChangeAnimation
PlaySound and all their brothers and sisters. You may have noticed that they're all marked yellow in the script editor. So what are they really?
Well here's another benefit to makslane! Most of these are NOT a standard part of the language! They are defined by GE in the hidden scripts. They simply execute code that was written where they were defined. In the brackets are the information the code needs to be executed. Let's take for instance PlaySound2. It requires a string giving the filename. the filename is needed so the code knows which file it plays at all, it needs volume, pan, loops number. These are all needed for the code to execute properly. If you don't give one of these so-called parameters, or give them in a wrong type (maybe like giving a string when it needs an integer) then GE will through you with an error.

So I said executes the code where defined? arrays and variables are all defined in code too! GE simply hides it.
You can create your own functions, variables and arrays in code too? Answer: YES! Here is the reason for GLOBAL CODE! That is just for this reason. To create your own definitions. You may want to have a better random function? (done before :) ) Put your code idea into actual code, and try it! :P Well of course it ain't always as easy to do than to say...
Let's start with the easiest definitions. Defining variables - in code.
Note: defining a variable in code doesn't let you set the savegroup. So only define a variable in code if you don't want to use it in a savegroup!

there are 3 types we really use. Integer, Float, and String. Note: you can also define a double, or a real too. But these are quite the same as Float. So I'll only talk about float.

define a variable:
int integervariable;
float floatvariable;
char stringvariable;
So why is it good to do this, when there's GE's way? Not too much use, unless you want to keep all of your variables neatly in a place easy to view, easy to modify.
You may have realized that I didn't talk about making a variable global, or actor's. The truth is, that the Global script will only make a variable global. But you may remember earlier I said you can make actor variables. So the way to do that, is put your definition (int variablename;) into the very beginning of one of the actor's scripts. Note: This is even of a smaller influence than that of the actor variable; this type of a variable can only be used inside the script it was defined. Though you may put that short line in the beginning of all scripts where it is needed. It's simple, it's useful in a way.

Defining functions...
Probably if you're not interested in this, then skip it. As a n00b this'll just add to the headache you might have anyway.
Basically I'll only teach how to make functions without parameters, which means: function1(); will be the way of calling your function.
The funky thing about functions, is that sometimes they also return a value. Example: the rand function. It returns the number which was randomly choosen from 0, to the number given as a parameter.
So here's the simplest function definition:
void funkyfunction()
{
some funky stuff...;
}
always put "void" infront of your function's name if you don't want your function to return any value. You can also put int, float, or char in it's place. The word you put there defines what you want your function to return. "void" tells GE to not have this function return a value. int tells it to return an integer, float to return a float, char to return a string.
The brackets are needed there, even though we don't have any parameters for our functions.
Note: NO SEMICOLON
among the curly brackets... You can yet again put ANYTHING!
IF you do want the function to return a value, then add a line in the apropriate spot with this:
return value;
Yet again value can be anything that is either a constant number or string, or usually that'll be a variable if you use this. return will tell GE, that after it runs the function, to act as though it'd see the returned value in place of the function in code where you used this function.
Earlier I said "apropriate spot", for where return should be placed. Reason: This ain't always gona be the very end of the function. It also might be in the end of an if statement, or case switch. So return can be in your code several times depending on how the function is built up.

Definitions over, except for arrays, which I'll describe later Huh, that I'm sure is tough for n00bs. I don't even expect it to be understood fist hand. read it several times, play around with it, and test different things. The key to knowledge is experience!

Finally more arrays, and introduction to loops...

So in part 1 I talked about arrays. It was a really brief description about them. So how to use an array... I talked about using it for maps. But how can it be applied? This will be more of an example:
I'll assume you have an actor which has animations for different states of that field. In my example, I'll just use this. Let's say we're making a map for some type of strategy/war game. The player is given a map to cross. The speed you can go at depends on the type of fields you go across. I assume we have plains, forests, mountains, and water. Now you have an actor with animations for all 4 of these. Clone the actor, to the size you want, and design a map.
In reality, using the animindex of each field is imposible, because you can't ask the game about the animation of a clone, when we don't know its cloneindex.
Way to solve this problem: ARRAY! Bingo!
How is this possible? You can question about the value of a variable in the array, when we don't know the variable's array index.
So how can we set up this?
Create an array first. You can create it the way you already know; through GE, or you can make a Global code for it too. I'll describe the way to do this in global code. first of all, we want it to be an array of integers. Which means, that this definition starts with "int". Then how do we go on? variablename! But semicolon is not the next thing up.
int array_name[sizeofarray];
Important note: sizeofarray is an exact number, and NEVER a variable.
To show how it works, here are 2 more examples:
float array_of_float[sizeofarray];
char array_of_string[sizeofarray];
Sizeofarray is always an integer number. To make the array of different type, you change the word infront of the array's name. What is the "Size" field in GE goes into [ ]. But there's something GE isn't capable of, that code can do. I'll only mention this, cause rarely is it useful...
int arrayname[2][3];
Note that is an example, the numbers can be any number. This is the example of a "multi-dimensional" array, and it has 2x3 variables in it. Here are the variables in that array I just defined:
arrayname[0][0]
arrayname[0][1]
arrayname[0][2]
arrayname[1][0]
arrayname[1][1]
arrayname[1][2]

You might think, that's what we need for a map! Every first [] shows the ow number, and the other one shows which field inside the row!
True, but this method would be inconvenient. Search the forum for Fuzzy's "Simplicity with arrays" topic. It will describe why, I hope reading what I've written sofar will make his topic understandable.
Back to my example: So just how big should our array be in size? Multiply the row and column numbers, and you'll get the number to put between []. Now that we've got an array, we have to make it contain the values for the map we've set up. We'll have 1 mean the field is a plain, 2 it's a forest, 3 is a mountain, 4 is water. Note: I'll also assume that animindex = 0 for plain animation, 1 for forest, 2 for mountain, 4 for water. Or you an change the numbers for each type of field in the array, to match the order in which you added animations.

Sofar so good. Now is the time for loops! What are loops? (No, not the fruity ones, the code ones!)
They allow you to change lots of data with little code; They allow you to do a series of calculations, that are similar, and do those calculations until you get the result you wanted.
Alltogether there are 3 types of loops in code. Each has its own purpose. Since this post is getting long, go on to the next one to continue!
http://www.youtube.com/watch?v=XyXexDJBv58
http://www.youtube.com/watch?v=Be4__gww1xQ
These are me and playing the piano (second one with a friend/fellow student)
Hope you watch and enjoy!
User avatar
Thanx
 
Posts: 314
Joined: Sat Jan 26, 2008 10:07 pm
Location: Home sweet home! :)
Score: 24 Give a positive score

Re: Scripting - for n00bs :o ;)

Postby Thanx » Sun Aug 17, 2008 3:26 pm

Part 4 comming soon...
http://www.youtube.com/watch?v=XyXexDJBv58
http://www.youtube.com/watch?v=Be4__gww1xQ
These are me and playing the piano (second one with a friend/fellow student)
Hope you watch and enjoy!
User avatar
Thanx
 
Posts: 314
Joined: Sat Jan 26, 2008 10:07 pm
Location: Home sweet home! :)
Score: 24 Give a positive score

Re: Scripting - for n00bs :o ;)

Postby Bee-Ant » Sun Aug 17, 2008 9:18 pm

How many part Thanx??? :lol:
Better you write a book about this :P jk
Oh,i request for :
1. How to make the remaping effect of remapKey wont be removed although we use LoadGame?
2. Store getKeyText to a string variable then save it
3. Restore string variable to int variable then remap it to remapKey?
JUST this,thanks thanx :D
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest