[TUT] Yet Another Introduction to Pawn
Quote:
Introduction to Pawn A non-AMXx tutorial to beginners Table of Contents: 0. Introduction 1. Hello World 2. Variables 3. Control Structures 4. Loop Structures 5. Tags 6. Enums 7. Functions 8. Conclusion 9. Useful Links Introduction [top] Quote:
Pawn should be very easy to learn by intermediate programmers or even beginners, since the language was designed to be friendly and detect the most common beginner mistakes. One of the most notable features of Pawn nowadays is its incredible speed. Lewis Van Winkle did a series of comparisons between famous game scripting languages on his blog, Code Plea, and you can easily see Pawn's speed is notable. Along this tutorial I'll be using the IDE included in the package, a modified version of Quincy IDE. If you prefer to use your own IDE, C++ syntax highlighting fits quite well with Pawn code. Before starting, be sure to copy the binaries from the bin folder to the Pawn installation root, so it's easier to run the example files. Note: some anti-virus software are known to detect malicious code in some of the Pawn binaries. This is a false positive, there's no malware in the Pawn toolkit. Getting started with the language syntax. Note: Pawn's syntax is somehow flexible. You don't need to use semicolons to end statements nor parenthesis to enclose function parameters, but I highly recommend you to use them, as the code is clearer and more C-like. 1. Hello World!of course [top] Code:
Yeah, simple as that. The main function, just like in C, is the first function that gets called when the script is executed by this host, pawnrun. The printf native is included from the console.inc file, which is automatically included by pawnrun. Wait a second, what's this native thing you speak of? In Pawn, natives are the functions exposed by the host. Their signatures are written in the include .inc files so the compiler can know the native call structure. Alternate forms of the Hello World script would be Code:
and Code:
A little detail: The first function isn't necessarily main, this is particular to pawnrun. 2. Variables [top] Code:
The new keyword is used to declare a new variable. You may initialize it to a value on declaration. Code:
Okay, so we have some new things now:
In this code, we are creating a new variable named myString, which is an array, and initializing it to some value. The myString array will have exactly 17 cells, the size of the string it's initialized to plus the 0 terminator, like C. Just like expected, the %s on the string is replaced with the second parameter to printf, "coder". 3. Control Structures [top] Code:
Whoa, complex stuff! Actually it isn't complicated, let's break it down to small pieces: Code:
Here we declared an array to store user input, used printf to ask the user to input his age, and used the getstring native from console.inc to read a line from the standard input. Code:
Here's our control structures! Pawn has C-like if-else structure support. Now that we know what all that code is doing, just one thing is left behind: what's string_to_int? Since pawnrun doesn't provide a way to convert a string to an integer, I used this little function to accomplish it: Code:
Try not to stuck trying to understand this code, we'll get to loop structures and stocks in a bit. 4. Loop Structures [top] Code:
Here we are using the for structure to loop through the array cells. In Pawn the for works just like in C++: for (declaration; condition; increment) { /* body */ } declaration gets called before the start of the loop, then condition is checked. If it's false, the for loop ended, otherwise: Execute the body, execute increment, and check condition again. If it's false, the for ended, otherwise, repeat this line. Another loop structure is while. This is an equivalent to the above code using while instead of for: Code:
5. Tags [top] On the very beginning of this tutorial, the Pawn description said "Pawn is a simple, typeless...". What's this lack of type mentioned? In Pawn, all variables are cells, which are 32 bits integers. An array is just a bunch of cells indexed under the same name. "But what about those strings we've used?!" Strings are just syntax sugar, in fact, when you do new array[] = "Dream.In.Code", it's exactly the same of doing Code:
The above code could also be done like this: Code:
But Pawn gives us a way to define meaning rather than type, tags: Code:
We include float.inc to have the Float tag and floating point functions like strfloat and floatpower. The operators for Float math are also defined in there. You'll notice that if you remove the Float tag from one of the float variables, the compiler will emit a warning, but the program will still compile and run, although not having the expected result. This is because Floats are a different type of tags: strong tags. A Float-tagged variable changes its internal storage layout, and that's why removing the tag from that example gives unexpected results. Weak tags are just a way to hint the meaning of a variable, not its type. 6. Enums [top] Code:
Here we are enumerating constants and defining a new tag correspondent to our enum. This is useful to avoid hard coding constants into the code, and variables that have a determined set of values. Pawn features a nasty syntax candy: you can use enums to index arrays just like C structures. See: Code:
6. Functions [top] In Pawn, like in almost every language, we can define functions to divide our code into small, concise pieces: Code:
Here we isolated the printing code into a smaller function, PrintInfo. The stock keyword is useful for code snippets, because the compiler will simply ignore them if not used in the code. That way you can build your own include files with useful functions like PrintInfo and string_to_int. Conclusion [top] This tutorial should give you a good introduction to the tiny but useful Pawn language. I'll see if I throw up a Pawn embedding tutorial later, so you guys can script your own software with Pawn. Useful Links [top] Pawn Language Guide (PDF File) - The complete documentation for the Pawn language. (Comes in the Pawn toolkit, see your doc folder. AMX Mod X - Having the largest Pawn scripting community out there, AMX Mod X is a server-side modding tool for multiplayer GoldSrc games, like Counter-Strike and Team Fortress Classic. SourceMod - Server-side modding tool for Source games, like Counter-Strike: Source and Team Fortress 2. Uses SourcePawn, an improved version of Pawn. SA-MP San Andreas Multiplayer mod for Grand Theft Auto - The second largest Pawn scripting community. Note: This is a conversion of my tutorial posted on Dream.In.Code: Introduction To Pawn, with a few fixed things. |
Re: [TUT] Yet Another Introduction to Pawn
This is very basic and is missing a lot of info.
|
Re: [TUT] Yet Another Introduction to Pawn
I was quite confused at first. Though you were writing in C (refering to main() and printf()) at first. I have a feeling this may confuse those just starting out programming plugins. That's my opinion.
|
Re: [TUT] Yet Another Introduction to Pawn
No offense but:
AlliedModders Forum Index > AMX Mod X > Scripting > Code Snippets/Tutorials |
Re: [TUT] Yet Another Introduction to Pawn
Quote:
This is actually verry useless to this community since I don't think anyone would come here to script in pawn and not use it for amxx (I dunno what sourcemod uses :}) |
Re: [TUT] Yet Another Introduction to Pawn
Quote:
Quote:
|
Re: [TUT] Yet Another Introduction to Pawn
Quote:
Quote:
Actually, it should've been: Quote:
|
Re: [TUT] Yet Another Introduction to Pawn
Even if it's generic Pawn, you still forgot a lot of info.
|
Re: [TUT] Yet Another Introduction to Pawn
What is the point to make a non-amxx tutorial here, more with the real Pawn ? I mean, how it can be useful for "beginner" here ? Beginner will be confused since they would care about making only AMXX plugins. Weird tutorial.
|
Re: [TUT] Yet Another Introduction to Pawn
How use one var multiple times in IF statement when SWITCH is not possible?
For example: if(sumvar == 666 || sumvar == 69) or if(sumvar == 666 && sumvar == 69) How to shorten this ? |
All times are GMT -4. The time now is 19:37. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.