PAWN/Types

Материал из Wikipedia Pi

Variables Variables are simple structures for holding data throughout a period of time in your script.

Types Small has just three data types for declaring variables. The default variable type is a regular whole number, or integer. A variable name, for backwards compatibility, should be 19 characters or less, and MUST start with a letter. It can contain the symbols A-Z, a-z, 0-9, and the underscore ("_"). It is important to note that variable names are case sensitive - "myvar", "MyVaR", and "MYVAR" are three separate symbols.

Integers

The simplest data type in Pawn is an "integer". Integers are whole numbers. To declare a new integer variable, use the "new" operator like so:

new a //Declare empty variable "a" new b=5 //Declare variable "b" and set it to 5. new c=5.0 //This is invalid, technically not a whole number! new d="hello" //"hello" is not a number either, this is invalid.

//You can also declare multiple variables on one line: new e,f,g,h new x=7, y=3 new z = 1_000_000 // Pawn supports numbers like this. So big values are easier to read.

Floats

You can also declare a variable as a "Float", which means it can store numbers with decimal places. These are called "floating point" numbers: new Float:a //Declare empty floating point variable "a" new Float:b=5.3 //This will declare a new variable "b" and assign 5.3 to it. new Float:c=5 //This is valid, but the compiler will give you a warning. new Float:d="hello" //This is invalid, "hello" is not a decimal number.

You can also do the following: //float(n) is a function that takes a number n and makes it a // floating point number. new Float:var = float(5) new Float:var2 = 5.0 new Float:var3 = 1.0*5 new var4 = floatround(5.0) //Note: floatround(n) is a function that takes a number n and rounds it to a whole number. // this makes the assignment to a regular integer variable valid.

Note - Spacing does generally not matter, as long as the compiler can tell symbols apart from each other. If your spacing is REALLY bad, you will get errors or maybe even warnings. For example, "new var = 5" and "new var=5" are the same, but "newvar=5" is totally wrong.

Booleans

The last variable type is "boolean". It is very simple - it is either "true", or "false". Both "true" and "false" are predefined data structures.

new bool:IsItOn //Declares a new variable "IsItOn" which is automatically false new bool:xyz=true //Declares a new variable "xyz" set to true

Strings

You have probably noticed that an important data type is missing - characters (letters and symbols). These are called "strings", and in Pawn, they are technically numbers! A string is an array of numbers that translate to ASCII (character) symbols. For example:

//This will declare a number array "myString" that contains the data "Hello". //It will have 6 slots, because there are 5 characters. //The last slot is reserved for the number 0, which tells the Pawn engine that it is a string. new myString[] = "Hello" // If you're using SourcePawn, do this instead: new String:myString[] = "Hello" Note: anything in between /* and */ is also a comment. You cannot use /* */ inside a /* */. The following set of commands achieves the same purpose, however, it is longer and not recommended. This works because each character of the string "Hello" is stored in a slot in the array.

new myString[6] myString[0] = 'H' myString[1] = 'e' myString[2] = 'l' myString[3] = 'l' myString[4] = 'o' myString[5] = 0 Note: Arrays that are meant to be strings must end in a 0, or the null character. This is so you know where the string ends.

You CANNOT do this! While it may compile, it is highly dangerous as it might cause overflow errors:

new myString[6] myString = "Hello" //INVALID! myString[0] = "Hello" //INVALID! //To add data to a string, you can do this: new goodString[7] copy(goodString, 6, "Hello") Note: In SourcePawn, copy was renamed to strcopy and avoids the issue with copy noted below.

Note that we copied 6 cells of the array into an array that can hold 7. If we were to copy 7 bytes into this array, copy() could potentially copy an extra byte for the Null character, overflowing the array. This is called a buffer overflow and must be carefully avoided.

More examples:

//Copy is a function that takes three parameters: copy(destination[], length, source[]) //It copies the string inside the source array and places // it into the destination array, but only copies up to length characters.

//Lastly, to prove that a string is really an array of numbers, this is completely valid: new weird[6] weird[0] = 68 weird[1] = 65 weird[2] = 73 weird[3] = 86 weird[4] = 68 weird[5] = 0 //This will set the variable "weird" to the string "DAVID".