Raised This Month: $12 Target: $400
 3% 

Programming for starters


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-28-2009 , 06:30   Programming for starters
Reply With Quote #1

I know that its hard for a starter to begin programming. That lead me to want to make a tutorial that one would read before reading tutorials on how to create his first plugin. An introduction to (pawn) programming before an introduction to amxmodx programming. I tried a less normal approach so I need you to tell me if it is confuse or unhelpful. This is the first part of it.


In programming, everything is represented by numbers. A letter is a number, a word is group of numbers, a color is a group of three numbers (one representing the amount of Red, other the one of Green, other the one of Blue.
To program is to deal with numbers.

Part 1 - Numbers, Arrays of numbers

To have a place where we can store a number (what is refered to as "variable") we do:

PHP Code:
new numberHolder 
new means that we are creating a variable.
numberHolder is its label. It should be named depending on the data that it will hold.

This is called "declaring a variable"

To store a number in our variable we do:

PHP Code:
numberHolder 
Being 5 an example for a number.

To have a place for a group of numbers (what is referred to as an "array") we do:

PHP Code:
new numberGroupHolder[x
Where x is the number of numbers that the variable can hold. (The size of the group)
This group will have x positions. One for each number. To acess each position we do:

PHP Code:
numberGroupHolder[0
Being 0 the first position. The last one is x-1. So, to store a color, we would do
PHP Code:
new color[3]

color[0] = 0
color
[1] = 100
color
[2] = 255 
or
PHP Code:
new color[3]

color = {0,100,255
or even

PHP Code:
new color[3] = {0,100,255
Pawn provide us arithmetic operators so, after creating our color we can make it darker:
PHP Code:
color[0] = color[0] - 10
color
[1] = color[1] - 10
color
[2] = color[2] - 10 
If we want to darken more than one colour we don't need to change each part of the color manually.
We can, instead, store this procedure in a piece of code called "function":
PHP Code:
darkenColor(anyColor[3])
{
    
anyColor[0] = anyColor[0] - 10
    anyColor
[1] = anyColor[1] - 10
    anyColor
[2] = anyColor[2] - 10

Where:

darkenColor is a label for the function that we should name depending on what it does
anyColor is a label for the variable that its given to the function when used inside the function (unrelated to the original label of the variable)

In a function:

Between the parenthesis we have its arguments (the variables that we want it to have so it can do its work). One in this case.
Between the brackets we have its body.

So, to darken some colours we would do:

PHP Code:
new someColor[3] = {100,100,100}
new 
otherColor[3] = {50,100,150}
new 
anotherColor[3] = {255,30,233}

darkenColor(someColor)
darkenColor(otherColor)
darkenColor(anotherColor
This is refered to as "calling the darkenColor function". First we defined it, then we used it.

Then:

someColor will hold the values 90,90,90.
otherColor will hold the values 40,90,140.
anotherColor will hold the values 245,20,223.

But, our function has a problem. If a part of the color is lower than 10, it will become negative. What is wrong since a color part should be between 0 and 255.
To solve this we need to handle a condition. With a "conditional statement":

PHP Code:
darkenColor(anyColor[3])
{
    
anyColor[0] = anyColor[0] - 10
    anyColor
[1] = anyColor[1] - 10
    anyColor
[2] = anyColor[2] - 10
    
    
if(anyColor[0] < 0)
    {
        
anyColor[0] = 0
    
}
    
    if(
anyColor[1] < 0)
    {
        
anyColor[1] = 0
    
}
        
    if(
anyColor[2] < 0)
    {
        
anyColor[2] = 0    
    
}

In an if clause:

Between the parenthesis we have a condition.
Between the brackets we have if's body. What is inside the if body will only happen when the condition holds true.
("anyColor[x] = 0" happens when "anyColor[x] < 0")


Our function is a little impractical. We can't tell him the "luminosity to change" factor. And it is supposed to darken only.
So, lets make it more practical.

PHP Code:
changeColorLuminosity(anyColor[3],factor)
{
    
anyColor[0] = anyColor[0] + factor
    anyColor
[1] = anyColor[1] + factor
    anyColor
[2] = anyColor[2] + factor
    
    
if(anyColor[0] < 0)
    {
        
anyColor[0] = 0
    
}
    
    if(
anyColor[1] < 0)
    {
        
anyColor[1] = 0
    
}
        
    if(
anyColor[2] < 0)
    {
        
anyColor[2] = 0    
    
}

Now we can give it a "luminosity to change" factor and it is now supposed to lighten or darken. (Passing a positive factor to lighten, passing a negative factor to darken).
But, now, we have the problem that it can make a color part bigger than 255. So we have to handle it

PHP Code:
changeColorLuminosity(anyColor[3],factor)
{
    
anyColor[0] = anyColor[0] + factor
    anyColor
[1] = anyColor[1] + factor
    anyColor
[2] = anyColor[2] + factor
    
    
if(anyColor[0] < 0)
    {
        
anyColor[0] = 0
    
}
    
    if(
anyColor[1] < 0)
    {
        
anyColor[1] = 0
    
}
        
    if(
anyColor[2] < 0)
    {
        
anyColor[2] = 0    
    
}
    
    if(
anyColor[0] > 255)
    {
        
anyColor[0] = 255
    
}
    
    if(
anyColor[1] > 255)
    {
        
anyColor[1] = 0
    
}
        
    if(
anyColor[2] > 255)
    {
        
anyColor[2] = 0    
    
}

This function will work. But it can be improved. There is no need to check if a color part has a value bigger than 255 when we know that it is negative.

PHP Code:
changeColorLuminosity(anyColor[3],factor)
{
    
anyColor[0] = anyColor[0] + factor
    anyColor
[1] = anyColor[1] + factor
    anyColor
[2] = anyColor[2] + factor
    
    
if(anyColor[0] < 0)
    {
        
anyColor[0] = 0
    
}
    else if (
anyColor[0] > 255)
    {
        
anyColor[0] = 255
    
}
    
    if(
anyColor[1] < 0)
    {
        
anyColor[1] = 0
    
}
    else if (
anyColor[1] > 255)
    {
        
anyColor[1] = 255
    
}
        
    if(
anyColor[2] < 0)
    {
        
anyColor[2] = 0
    
}
    else if (
anyColor[2] > 255)
    {
        
anyColor[2] = 255
    
}

This:
PHP Code:
    if(anyColor[x] < 0)
    {
        
anyColor[x] = 0
    
}
    else if (
anyColor[x] > 255)
    {
        
anyColor[x] = 255
    

can be read as:

If a colour part is negative make it null. If it isn't negative and it is bigger than 255 make it 255.

Part 2 - Characters, Arrays of characters (strings)

by Bugsy

Strings
A string is defined as a group of 2 or more contiguous characters. Programmatically, a string is just an array of numbers that are
represented as a whole. Each character in the ascii character set is represented by an integer value in the range of 0 to 255. For
the sake of this tutorial we will only consider letters of the alphabet even though the ascii character set has numerous other
characters (including numbers and puncuation characters). A null-terminated string must always end with what is known as a null character
(ascii value 0). When a null character is found within the string it is considered the end of a string. When you are defining an array to
hold a string, you must always add 1 to the size of the string that you wish to hold to allocate space for the null character. Example, If
you desire to hold the word "word" in your string, define the array as new szWord[5]. When you declare an array, each element in the array
has a value of 0 (null) by default so you do not need to manually set the last character as null (or 0). Keep in mind that arrays begin with
an index of 0 so and array defined with a size of 5 will be accessed using 0 - 4. [ 0 , 1 , 2 , 3 , 4 ].

Ascii Character Set
Please note that Uppercase and lowercase characters are considered totally separate characters. Example, 'a' is not equal to 'A'

Ascii character integer values:

A-Z ranges from 65-90. 65 being 'A' and 90 being 'Z'
a-z ranges from 97-122. 97 being 'a' and 122 being 'z'

Defining Strings with Code Example
Suppose we want to define a string and make it hold the word "word" or "WORD"

We can do this in 2 ways:

1. If you want to assign a string value to your variable when defining it you do not need to specify the size.
The compiler will do this automatically, it will also include an extra element for the null character on the end.

PHP Code:
new szWord[] = "word"
new szWord[] = "WORD" 
2. Below we define an array of 5 elements. Notice how we create the array variable sized 1 larger than needed.
Below I set the value of szWord[4] to 0 just for demonstration purposes, though it is not needed since the
default value is 0 when it is defined.
PHP Code:
new szWord[5]
szWord[0] = 119 //w
szWord[1] = 111 //o
szWord[2] = 114 //r
szWord[3] = 100 //d
szWord[4] = 0   //null .. This is not needed since the default value of each element is 0

szWord[0] = 87 //W
szWord[1] = 79 //O
szWord[2] = 82 //R
szWord[3] = 68 //D
szWord[4] = 0  //null .. This is not needed since the default value of each element is 0.

//Note that you do not need to memorize the ascii value for every character. You can assign a character to a variable or array element by enclosing
//it in single quotes. Example:

szWord[0] = 'w'
szWord[1] = 'o'
szWord[2] = 'r'
szWord[3] = 'd'
szWord[4] = '^0' 
Null Terminated Strings
As mentioned above, when a null character (value 0) is found in the array, it is considered the end of the string.

To see it in action, here is a little example:
PHP Code:
new szWord[] = "alphabet"

szWord[5] = 0  //Set where character 'b' is stored to null. (ascii value of 0)

//It will equal "alpha" instead of "alphabet" 
Looping through Characters in a String
The below code will loop through the string 1 character at a time and count the number of spaces and letter
't's that are found. You can do this in two ways either using an if\else or switch statement.

For this demonstration I will be using what is known as a loop. A loop is a mechanism used to repeat the same block of code for a variable
number of times. In this particular situation we know the exact number of iterations (cycles) we want to repeat so we will use what is called
a for-loop. The syntax for this loop is: for ( expression1 ; expression2 ; expression3 )

Expression explanation:
expression1: Evaluated, only once, before entering the loop. This can be used to declare a variable used in the loop, normally an index variable.
expression2: Evaluated before each iteration; loop continues until this returns logically false. Normally used for conditional statement.
expression3: Evaluated after each iteration. Normally used to increment the index variable that was declared in expression1.
Note: You can omit all expressions which will produce an infinite loop: for ( ; ; ) [dont try this until you know what you're doing!]

PHP Code:
//Expression1: Declare i as a variable that will be used to address the array index ( has a value of 0 by default ).
//Expression2: Continue looping until i > 9 ( loop will continue while i <= 9 )
//Expression3: Increment our i variable by one. ( each time the code loops i is increased by 1 )

//With this loop the code will start with i = 0, execute the code in the block and then return to the for-loop line to
//evaluate expression 2. If this expression is true it evaluate expression 3 and procede to execute the block of code again.
//this will continue until expression2 is false.
for ( new <= i++ )
{
        
//code to repeat with each iteration

strlen() is a function that is built into the amx-x core that will return the length of a string (excluding null)

PHP Code:
new szString[] = "this is a test string"
new iLen strlenszString //If you know the length you can manually specify it
new iSpaces;
new 
iLetter_t;

//Start at 0 and loop until the index is 1 less than the length since arrays have a 0-based index [0 to len-1]
for ( new iLen i++ )
{
        if ( 
szString[i] == ' ' )
                
iSpaces++
        else if ( 
szString[i] == 't' )
                
iLetter_t++

//OR
        
switch ( szString[i] )
        {
                case 
' 'iSpaces++
                case 
't'iLetter_t++
        }
}

ResultSpaces Letter 't' 
To create a string that holds the lowercase alphabet:
PHP Code:
new szAlphabet[27//26 letters in the alphabet plus 1 for null terminator

//'a' has a ascii value of 97 so we just add it to our loop index since it is incremented with each pass
for ( new 26 i++ )
        
szAlphabet[i] = 97 //Assign ascii value to array and then increment the ascii value variable

//This is what will occur through the loop
//szAlphabet[0] = 97   //a
//szAlphabet[1] = 98   //b
//szAlphabet[2] = 99   //c
//szAlphabet[3] = 100  //d
//...
//szAlphabet[25] = 122  //z 
Array of Strings
You can create an array of strings. This is also known as a 2-dimension array.
PHP Code:
//To define an array of strings that you will assign a value to when defining:
new szString[][] = { "this" "is" "an" "array" "of" "strings" }
//strings are then accessed via the index of string. example szString[0] = "this", szString[1] = "is"

//If values assigned during run-time (example, for storing player names or steamID's when they connect)
new szString[number of strings][length of longest string
To access a string in an array of strings you use the similar logic as accessing a single character in a normal string. Example, if you wanted to get the word "array" in the above array of strings, you would use szString[3]. To get an individual character you just add access it accordingly. To get the first 'r' in array, szString[3][1].

There are numerous functions built into the AMX-X core that make it easy to manipulate strings. See the function page
on the amxmodx.org homepage.


Part 3 - Conditional Statements

by Exolent[jNr]


The Basics
A conditional statement is a statement to decide whether to continue with the block of code.
Conditional statements can be combined with the "and" and "or" symbols.
&& is the symbol used for "and"
|| is the symbol used for "or"
Example:
Code:
// x is greater than 0 or less than or equal to 255
x > 0 || x <= 255

// x is greater than 15 and y is less than 10
x > 15 && y < 10

// x is less than 15
// or y is less than 0 and y is greater than -10
x < 15 || y < 0 && y > -10

// x is greater than 16
// and y is greater than 10 or y is less than 0
x > 16 && (y > 10 || y < 0)

// a is greater than b and c is less than d and e is less than f
a > b && c < d && e < f
Inequalities have a special case that you can use a different format.
Code:
// x is greater than 0 and less than or equal to 5
x > 0 && x <= 5

// This can be a double-sided inequality (not the term, but easy to understand)
0 < x <= 5
This "double-sided inequality" will only work if it is a specific range of values.
It will not work with something such as:
Code:
// x is greater than 5 or less than 0
x > 5 || x < 0
Conditional statements can also check if something is not true.
! opposite
!= means not equal to
Code:
// x is not less than 0
!(x < 0)

// x is not equal to 0
x != 0
These can become very complicated, and with the "opposite" operator, you can switch the conditional.
Code:
// x is greater than 0 and y is less than 2
x > 0 && y < 2

// the same as above, but with the "opposite" operator
!(x <= 0 || y >= 2)
// this conditional statement reads as this:
// if x <= 0, then it will be false
// if y >= 2, then it will be false
// otherwise, it will be true because x > 0 and y < 2
"If" Statements
An "if" statement is a conditional statement to decide whether one block of code will be used.
It is very simple to use.
The format is:
Code:
if( [conditional] )
{
    // block of code if conditional is true
}
Code:
if( x > 1 )
{
    // this code will be used if x is greater than 1
}
Along with the "if" statement is an "else if" and an "else" statement.
The "else if" statement is used when the first "if" statement is false.
It has the same format, except it has an "else " before it.
Code:
if( [conditional #1] )
{
    // conditional #1 is true
}
else if( [conditional #2] )
{
    // conditional #1 is false
    // and conditional #2 is true
}
The "else" statement is used when all other "if" and "else if" statements are false.
Code:
if( [conditional #1] )
{
    // conditional #1 is true
}
else if( [conditional #2] )
{
    // conditional #1 is false
    // and conditional #2 is true
}
else
{
    // conditional #1 is false
    // and conditional #2 is false
}
Loops
There are 3 different loops that you can do:
  • while
  • do, while
  • for

That list is arranged in terms of complication.

The "while" loop will continue to run as long as the conditional statement given is true.
Format:
Code:
while( [conditional] )
{
    // block of code to be used
}
The way it works:
  1. Check conditional statement
  2. If true, then execute the block of code inside; if false, exit the loop
  3. Go back to #1


The "do, while" loop is almost the same as the "while" loop.
The only difference is the order in which it is used.
Format:
Code:
do
{
    // block of code to be used
}
while( [conditional] ); // semicolon is not needed, except for when semicolons are enforced
The way it works:
  1. Execute the block of code inside
  2. Check conditional statement
  3. If true, then execute the block of code inside; if false, exit the loop
  4. Go back to #2


The "for" loop is more complicated than the others.
They are mainly used to count from one number to the other.
It contains 3 parts: initial, conditional, increment.
Initial - Creating variables to use for the loop.
Conditional - Conditional statement to check if the loop should continue.
Increment - Increases the value of the variable that was created.
Format:
Code:
for( [initial]; [conditional]; [increment] ) // semicolons are a MUST!
{
    // block of code
}
The way it works:
  1. Execute initial
  2. If conditional is true, execute code; if false, exit the loop
  3. Execute the block of code
  4. Execute increment
  5. Go back to #2

Example counting from 1 to 5:
Code:
for( new i = 1; i <= 5; i++ ) // i++ increases i by 1
{
    // block of code
}
Example counting from 10 to 1:
Code:
for( new i = 10; i >= 1; i-- ) // i-- decreases i by 1
{
    // block of code
}
Loops can be forced to skip parts of the code and go back to the conditional again with the "continue" operator.
Example:
Code:
for( new i = 0; i < 4; i++ )
{
    if( i == 2 )
    {
        continue;
    }
    
    // block of code only when i is not 2
}
Loops can also be forced to exit even when the conditional is true.
Example:
Code:
for( new i = 0; i < 4; i++ )
{
    if( i == 2 )
    {
        break;
    }
    
    // this code will only be used for when i is 0 and 1
    // when i reaches 2, the loop will exit
    // therefore, i never reaches 3 like the loop wants to in the conditional
}
"Switch" Statements
Switch statements are used when comparing a single number to 2 or more specific values that result in different actions.
Format:
Code:
switch( variable )
{
    case VALUE_1:
    {
        // variable is equal to VALUE_1
    }
    case VALUE_2:
    {
        // variable is equal to VALUE_2
    }
    case VALUE_3:
    {
        // variable is equal to VALUE_3
    }
}
There is another option to be used if any of the other values are not correct.
Code:
switch( variable )
{
    case VALUE_1:
    {
        // variable is equal to VALUE_1
    }
    case VALUE_2:
    {
        // variable is equal to VALUE_2
    }
    case VALUE_3:
    {
        // variable is equal to VALUE_3
    }
    default:
    {
    // variable is not VALUE_1, VALUE_2, or VALUE_3
    }
}
Part 4 - Mathematical Operators

by Exolent[jNr]


Basic Usage
We all know the standard mathematical operations:
  • Addition
  • Subtraction
  • Multiplication
  • Division
These are easily used in programming.
Code:
new x = 1, y = 4;

// Addition
x = y + 1; // x now equals 5

// Subtraction
y = x - 2; // y now equals 2

// Multiplication
x = y * 2; // x now equals 4

// Division
y = 12 / x; // y now equals 3
In programming, there are more operators than these standard 4.
  • Modulo
  • Bit Shifting
The modulo operator gives the remainder value of when one number is divided by another.
Example:
Code:
25 / 4 = 6 R 1
// That means that 4 goes into 25, 6 times evenly,
// and there is a remainder of 1 left over.

new x = 25 % 4;
// x now equals 1

x = 113 % 100;
// x now equals 13
For information about bit shifting, have a look at this tutorial.

Increments
Variables may need to have values added, subtracted, or any other mathematical operation done to their original value.
This can simply be coded like this:
Code:
new x = 5;

x = x + 1;
// x was incremented by 1, and now equals 6

x = x * 2;
// x was mulitplied by 2, and now equals 12

x = x / 4;
// x was divided by 4, and now equals 3

x = x - 2;
// x was decremented by 2, and now equals 1
There are shorthand operations for code such as this.
Code:
variable (operator)= (value);

// This will perform (operator) to (variable) with (value).
Example:
Code:
new x = 5;

x *= 10;
// x now equals 50

x -= 10;
// x now equals 40

x /= 2;
// x now equals 20

x += 4;
// x now equals 24
There is a special case when using "+= 1" and "-= 1".
Code:
new x = 1;

x += 1;
// x now equals 2

x++;
// x now equals 3
// ++ is the equivalent of += 1
// it can even be placed before the variable
++x;

// There is a difference in ++variable and variable++;
// ++variable increments the variable before it is used,
// and variable++ increments the variable after it is used.

new x = 1;
new y = x++;
// x = 2, y = 1
// x was increased to 2 after it was assigned to y

x = 1;
y = ++x;
// x = 2, y = 2
// x was increased to 2 before it was assigned to y
The same goes for -= 1. It is used with --.
Code:
new x = 1;
new y = --x;
// x = 0, y = 0

x = 10;
y = x--;
// x = 9, y = 10
__________________

Last edited by joaquimandrade; 05-25-2009 at 17:57.
joaquimandrade is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 04-28-2009 , 07:24   Re: Programming for starters
Reply With Quote #2

Sounds well explained. Waiting for others parts. ^^
Arkshine is offline
Dr.G
Senior Member
Join Date: Nov 2008
Old 04-28-2009 , 07:32   Re: Programming for starters
Reply With Quote #3

gj! the text size is very small... or is it just me..
__________________
Dr.G is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-28-2009 , 07:41   Re: Programming for starters
Reply With Quote #4

Quote:
Originally Posted by arkshine View Post
Sounds well explained. Waiting for others parts. ^^
Thanks. I will do it if starters find this useful.


Quote:
Originally Posted by Dr.G View Post
gj! the text size is very small... or is it just me..
I just used a smaller font for the first paragraph. The rest is supposed to be normal. Send me a screenshot.

Edit: i confirmed that the text inside php tags is smaller (In internet explorer). I'm fixing it.
Edit2: fixed
__________________

Last edited by joaquimandrade; 04-28-2009 at 07:47.
joaquimandrade is offline
Shard
Member
Join Date: Mar 2009
Old 04-28-2009 , 09:30   Re: Programming for starters
Reply With Quote #5

Wow.. This is a nice guides for starters like me xD GJ! +K
I hope the other parts come out soon
Shard is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-28-2009 , 09:33   Re: Programming for starters
Reply With Quote #6

Quote:
Originally Posted by Shard View Post
Wow.. This is a nice guides for starters like me xD GJ! +K
I hope the other parts come out soon
Please tell me if you understand everything. If there is any part of it more confusing.
__________________
joaquimandrade is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 04-28-2009 , 09:50   Re: Programming for starters
Reply With Quote #7

Nice. But why in function(you type anyColor) while inside the function you type anycolor?
__________________
hleV is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-28-2009 , 09:51   Re: Programming for starters
Reply With Quote #8

Quote:
Originally Posted by hleV View Post
Nice. But why in function(you type anyColor) while inside the function you type anycolor?
Because its an error.

Edit: fixed, thanks
__________________

Last edited by joaquimandrade; 04-28-2009 at 09:56.
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-28-2009 , 10:13   Re: Programming for starters
Reply With Quote #9

The text at the very top is still tiny.

Nice idea, I think this is definitely needed for people that are interested in beginning scripting.
__________________
Bugsy is offline
minimiller
Veteran Member
Join Date: Aug 2007
Location: United Kingdom
Old 04-28-2009 , 10:20   Re: Programming for starters
Reply With Quote #10

It might be a good idea to mention that almost everything is case sensitive. So the declared variable and the called variable must be exactly the same.

Other than that, nice work
__________________
minimiller is offline
Send a message via MSN to minimiller
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:03.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode