View Single Post
Author Message
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 06-10-2009 , 15:06   Pawn Picture Tutorial
Reply With Quote #1


Welcome to the Pawn Picture Tutorial. This will give you a better understanding of coding by relating it to some real world examples.

Note you need to read this entire tutorial in order to understand how to do things.
*Please note that these are examples of code and nothing will appear in game*

*This is intended for very basic purposes and there may be some things not completely correct.

First of all, we will be associating a plugin to a warehouse. The warehouse has boxes (variables), machines (functions), loading and shipping ports (forwards and returns), and doors to other warehouses (natives).



Comments [top]

The very first thing you will need to know about coding is comments. Comments are not read by the computer, and serve no purpose to the actual code.
You can think of it as a sign. It doesn't actually do anything, but it can save you from making a mistake.

An equivalent comment would be:
Code:
//Be careful Michael Jackson hangs around here
However, there might be some changes and you can add some information to the comment:
Code:
//Be careful Michael Jackson hangs around here

//As of June 25, 2009 we do not need to worry about Michael Jackson
Comments are placed in code so it is easier for someone to know or remember what something does.
Putting in comments can help you remember why you did something or help someone else understand your code.

Different programming languages have different types of comments, in Pawn there are two styles.
  • Single Line
  • Multi Line Comment

Single Line Comments start with //. Everything after // is ignored. It can be at the begining of a line, or in the middle of it.
Code:
//This is a good comment

             //This is also a good comment
Multi Line Comments start with /* and end with */. Everything between the two is ignored.
Code:
/*This comment type can be one line long*/

/*Or it
Can be
Spaced out*/

Variables [top]

Anyway, now some actual code: a variable. Oh no a variable! Yes in math we learned about the variables N and X and how they represent any number.

Now in code, a variable represents any number you assign to it. Basically think of it like a box.


To create a new variable, we use the code:
Code:
new variable_name
Where variable_name is... you guessed it, the variable's name. We try to give a variable a name similar to what it will represent, so if we are going to have it represent how many Apples there are:

There are certain characters you cannot use in names, so as a rule of thumb, only use letters, numbers, and _ (underscores)
Note that we can name it whatever we want (almost), as long as we use that name later on.
  • Note that we would have to use _ instead of a space
  • Names cannot begin with a number

Variables do not have to be named anything similar to what they represent, but code is easier to understand if they are named correctly.

Now we have these boxes, but what are they good for?

Well, we can put numbers into the box, and thus the variable has a number assigned to it (whatever is in the box). This may seem confusing at first, but try to read along.

When we first create a variable, it is zeroed. That means that it is assigned the number 0.


We can then change the variable with the following code:
Code:
variable_name = 3
And it results in changing the number from 0 to 3.
  • Note that = is used for assigning (changing) values.
  • And that == is used for comparing (checking) values.

A variable only pretends to be a number. So if we have:
Code:
new variable
Then we do variable + 5 we would get 0 + 5 == 5
If we do:
Code:
variable = 3
And then repeat variable + 5 we would then get 3 + 5 == 8
They can only represent one number at a time.

You should note that you do not need to do this monotonous task every time:
Code:
new variable
variable = 3
You can intialize a variable to a value:
Code:
new variable = 3
Before going on, we need to know the different types of variables. So far you have just learned about Integer variables. Yes another math term, what it means is that they have to be whole numbers.
  • Ie: ..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...

Another variable type is called a Boolean or Bool for short.
To create a bool variable we use:
Code:
new bool:variable_name


Bool variables can only be two values, true or false. Nothing else.


You cannot assign an integer to a boolean, think of it as if it won't fit in the box.

Integer variables are assigned by doing:
Code:
variable_name = 3
But booleans are either True or False, we can't assign 3 to it. So we have to do:
Code:
variable_name = true
or
Code:
variable_name = false
  • Note that Bools start with the False value instead of 0

Where would you need a bool variable? A few examples would be:
  • Are the lights on
  • Is it night time
  • Is the player standing
  • Is the player holding a knife

Continue to the next section of the guide

Last edited by Emp`; 05-17-2020 at 20:26. Reason: fixed images, thanks addons_zz
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`