Pawn Picture Tutorial
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). https://i.imgur.com/JBU83dG.png 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. https://i.imgur.com/bZEsUo1.png An equivalent comment would be: Code:
//Be careful Michael Jackson hangs around here Code:
//Be careful Michael Jackson hangs around here 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 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 Code:
/*This comment type can be one line long*/ 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. https://i.imgur.com/HrLWsx9.png To create a new variable, we use the code: Code:
new variable_name https://i.imgur.com/Ioa5mTU.png 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. https://i.imgur.com/OfyKoR1.png
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. https://i.imgur.com/oLqe7xZ.png We can then change the variable with the following code: Code:
variable_name = 3 https://i.imgur.com/ugP2V9h.png
A variable only pretends to be a number. So if we have: Code:
new variable If we do: Code:
variable = 3 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 Code:
new variable = 3
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. https://i.imgur.com/vIquV0u.png https://i.imgur.com/or8AJVR.png 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 Code:
variable_name = true Code:
variable_name = false
Where would you need a bool variable? A few examples would be:
Continue to the next section of the guide |
Re: Pawn Picture Tutorial
The last type of variable we will be addressing is the Floating Point or Float for short. To create a Float variable we use: Code:
new Float:variable_name Floats can be almost any rational number (oh no another math term!) You can tell it is a Float because the number always has a decimal point. Here are some examples of values you can set:
Okay, so here is a recap of variables:
Arrays and Strings [top] These are simple things to remember with variables. Arrays can be thought of as a row of boxes grouped together. https://i.imgur.com/TNjvDXU.png To create an array, we use similar code to creating a variable: Code:
new array_name[4]
If we want to create an array of 10 boxes, we would do: Code:
new array_name[10] Code:
array_name[box_number]
If we want to initialize an array with values, you do the following: Code:
new array[5] = {
"So what else about arrays?" you might ask. Well we can declare types of arrays just like variables: Code:
new Float:array[3] Well, here is one common example:
Okay... now onto Strings. Strings are basically arrays used to represent words, phrases, and any other text that might be seen. For example, we can create an array to hold a players name. Code:
new Name[32]
So we can do: Code:
new Name[32]
However, we do not want to have to do each letter one by one, that would be rediculous! So instead we can initialize (start) the string with: Code:
new Name[32] = "My Name" That is a quick guide for Arrays and Strings, so here is a recap:
Functions [top] Think of them as these machines that you have in your warehouse. You need to activate them, then they do something. https://i.imgur.com/K2AQZD9.png To activate a function in code: Code:
FunctionName() Some machines return whether or not they worked, while others return other values. In order to save a return value, one can think of putting a box (variable) under the exhaust hole. You can do this by: Code:
variable_name = FunctionName()
But don't stop there! Some functions don't just need to be activated, they need parameteres as well. https://i.imgur.com/ONS4aO9.png Think of putting a number into the hole on top or dumping a variable's number into the hole. Code:
FunctionName( 1 ) Code:
new variable = 1
Some functions need multiple parameters: https://i.imgur.com/4a0MTXi.png Code:
FunctionName( parameter, parameter2, parameter3 )
And some functions do not return anything ( 0 will be returned by default ): https://i.imgur.com/lGu549X.png Hold your horses though, we cannot use a function yet. We still need to create a function. Here is an example function that returns the first parameter plus five. Code:
AddFive( number )
Now that we have that function in our code, somewhere else in our plugin, we can use it: Code:
new variable
Natives [top] These are very similar to functions because that is basically what they are. Natives are machines that are not in your warehouse. You can think of them as shortcuts to another warehouse's machine. They have the same form as a function: Code:
NativeName(parameter, parameter2)
Forwards [top] Now these are going to be one of the key things that make your code work. You can think of them as your ports of the warehouse or communication with the outside world. In order for your code to do something, you need to be told of an event. Some simple events or forwards for games would be when someone spawns or dies. But wait! Your code doesn't let anyone know that it wants to be told about that information! There are two core forwards that you will need to know about:
If we want to know when a player spawns, we would then register the forward during plugin_init then the forward will be called everytime it occurs. There are several different ways to register forwards so I will not go into it here right now. Looking at this thread we see that we can register the forward with this native: Code:
RegisterHam(Ham_Spawn, "player", "fwHamPlayerSpawnPost", 1) A common parameter to register natives is a public function name. Basically it is the location we are sending it to. Looking back at the native Code:
RegisterHam(Ham_Spawn, "player", "fwHamPlayerSpawnPost", 1) So now we have to create that function like so: Code:
public fwHamPlayerSpawnPost(iPlayer)
Another part to most forwards are the returns. Different forwards use different types of returns, but for now we will just talk about:
Returns let the world know what to do once you received the information.
PLUGIN_HANDLED says to stop the information from being sent anywhere else. It will stop it from going to other plugins and stop it from going to the server. PLUGIN_CONTINUE says to do nothing. Everything will continue as normal, other plugins and the server will get the information. PLUGIN_HANDLED_MAIN says to stop the information from being sent to the server. It will go to other plugins, but not the server.
Continue to the next section of the guide |
Re: Pawn Picture Tutorial
Structure [top] All code has a type of structure to follow, you cannot just put random pieces of code in random spots and expect it to work. In a warehouse, you cannot put a door on the roof and expect people to be able to walk in through it. You do not want to put a doorway in the middle of someone's office. You need to place your code in the correct places. Here is the order of items that most plugins are created:
Large Comment [structure] [top] At the begining of a plugin's code there can be a large comment. The comment usually talks about the plugin, what changes have been made, or other things. This is just friendly for people trying to read the code, so they know what the code is going to do. However, most plugins do not contain this, and just jump into the next part. Includes [structure] [top] Before you start any other code, one will want to include some libraries. Wait what? You can think of them as stores. We only want to go to stores that have the things we want. We wouldn't go to Ralphs and BestBuy if we are trying to buy a computer, we would just go to BestBuy. What do the libraries do? They allow you to use natives from other places. Some libraries have their own forwards already too. You can think of it as if the stores are sending you information about the store. Almost every plugin will contain: #include <amxmodx> as it has the basic natives for creating a plugin. The amxmodx include file also contains the forwards plugin_init and plugin_precache. Once you have your includes setup, you can start with some actual code. Defines [structure] [top] These are just helpers for people. They help keep code constant in multiple areas. In the format: #define DEFINE_NAME definition DEFINE_NAME is what will be replaced in the code. definition is what it will be replaced with.
So if we have: #define MAX_PLAYERS 32 Everywhere in the code that has MAX_PLAYERS, it will change to 32. Some plugins don't use any self-made defines and just continue to the next thing. Global Variables [structure] [top] Okay, you should have an understanding of variables and this is a small little addon. If a variable is global it can be accessed everywhere in your warehouse. Usually people place a "g" at the begining of a global variable's name to indicate that it is global. So if we wanted to make a global variable, we would simply do: Code:
new gVariableName Plugin Loading [structure] [top] As talked about before, this is where the forward plugin_init comes into effect. In most cases, plugins have their plugin_init in the form: Code:
public plugin_init() Another core part to some plugins is precaching. This is usually done in the forward plugin_precache. Precaching tells the server that we are going to use this model, sprites, or sound so make sure the players connecting have these files. If the player does not have this file, then they download it. If you use a model, sprite, or sound in your plugin, you should precache it or else when you try to use it, the server will most likely crash. It is usually done in this format: Code:
public plugin_precache
Now that our plugin is loaded, we can actually do some things now. Functions and Forwards [structure] [top] First I should mention that the only thing that serperates Functions and Forwards is that Forwards are declared public and can be accessed by other plugins/modules. Functions are only accessible from the plugin they are created in. Otherwise, they behave exactly the same. I'm going to stop refering to them serparetly, and instead only refer to them as functions. If it has public in front, assume it is a forward. Functions are usually created in the format: Code:
FunctionName(parameters) You cannot create a function within another function. If you create a variable inside a function, it is only accessible in that function. Statements and Loops [structure] [top] There are several types of statements and loops, and here are the more common.
The main parts of the statements and loops are the conditional ( ) and the action { }.
Brackets [structure] [top] Types of Brackets:
In most conditions for square-brackets and parenthesis, you will want to have the closing part on the same line. On the other hand, curly-brackets are mainly used for multiple lines. Curly-brackets can be used to represent a length of code that needs to be executed. By default, the compiler assumes that there will only be a single line of code to execute. However, to execute more than one, you can use Curly-Brackets. Eg: Code:
if( variable ) Code:
if( variable ) White-Space and Indentation [structure] [top] White-Space (spaces, tabs, new lines) do not effect performance, but greatly effect being able to read the code. For indentation, a common rule of thumb is, between every new set of curly-brackets add another tab. You can either use tabs or spaces, but as long as they are the same. Code:
Function() If you try to compile your code and it does not have proper indentation, it will give you a warning for "loose indentation". Tips and Tricks for Getting Started [top] ! (not): The exclamation mark can be substituted with the word not. For example, if you have Code:
if( !is_user_alive(id) ) Additionally, this can be applied to variables. If you have: Code:
if( !variable ) Operator shortcuts: Code:
//If you have to do something like this Code:
//If you find yourself adding or subtracting one, you can use this shortcut Code:
string_name[0] = '^0' Some natives require extra formatting, the most common are:
Using set_task If you need something to happen after an amount of time, use the native set_task. Code:
// The parameters break down as such:
Other Help [top] Introduction to Pawn - an introduction to the Pawn language Introduction to AMXX Scripting - an introduction to creating plugins Pawn Tutorial - an introduction to Pawn for more advanced users Programming for starters - another basic tutorial for learning Pawn Beginner Scripting Tutorial - a small tutorial for creating your first plugin Selective Introduction to AMXX scripting - covers some things for making a plugin Scripting Tutorial - old tutorial with an example .sma Video Guide for Local Compiling - a guide to compiling on your computer Compiling Errors - a list of errors one may encounter while compiling Tutorial for Half-Life Position, Velocity, and Angles List of CS/CZ/DOD Events/Messages - list of forwards for CS/CZ/DOD Good Programming Habits - habits that coders should try to follow Code Styling - more styling that coders should try to follow Entities: what they are and how to use them - guide to entities (objects in the game) Messages (Temp Entities, Game Events, etc.) - introduction to messages (effects) Cvar Information - information about cvars and how to use them in your code Donations [top] |
Re: Pawn Picture Tutorial
Well done, will read it for sure!
|
Re: Pawn Picture Tutorial
Sexy Tuto, Emp`. I'm sure it will help a lot people who want to start to learn.
|
Re: Pawn Picture Tutorial
Nice tut emp :up:
Now if only we could get new scripters to read and experiment with scripting instead of going straight into scripting help to post. |
Re: Pawn Picture Tutorial
this is freaking awesome!
it makes sence! //<-- Unlike that typo! gj |
Re: Pawn Picture Tutorial
Wut the hell? :o That
Nice tutorial for starters. Gj http://tbn2.google.com/images?q=tbn:...heezburger.jpg PS: I must start learning pawn >.< |
Re: Pawn Picture Tutorial
Just read it. It's very good, the pictures is surely a help for understanding that you want.
|
Re: Pawn Picture Tutorial
Great tut, I thought "Structure" was struct from c++ :(
|
All times are GMT -4. The time now is 09:18. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.