Scripting Basics NL

From SA-MP

Jump to: navigation, search

Contents

Type variabelen

Variabelen zijn ruimtes om data op te slaan. In PAWN zijn de namen van variabelen hoofdletter gevoelig, dit houd in dat een variabele genaamd "lol" anders is dan "loL" en "LOL", daarnaast moeten variabelen altijd beginnen met een letter.

Integer

Dit is het standaard type variabele, deze word gebruikt om hele cijfers in op te slaan. Dit houd dus in dat cijfers met een punt er tussen hier niet in opgeslagen moeten worden.

Voorbeeld

new integer = 1337; // Dit slaat de waarde 1337 op in de variabele "integer".
new integer; // Deze manier slaat de waarde 0 op in "integer" vanwege dat dit de standaard waarde is voor een integer.


Mogelijke gebruikswijze

if(integer == 1337) { /* Doe iets */ }


Float

Dit type variablen kan getallen opslaan die 1 of meer decimalen heeft, zoals bijvoorbeeld 1.01, hierbij is het niet mogelijk om een getal op te slaan zonder decimaal.

Voorbeeld

new Float:float; // Dit maakt een nieuwe lege variabele aan met de naam "float".
new Float:float = 2.0; // Deze manier maakt de variabele "float" aan en geeft het de waarde 2.0.


Mogelijke gebruikswijze

new Float:float = 2.0;
SetPlayerPos(playerid, float, 2.0, 1400.0);


String

Deze variabele manier slaat één letter of symbool op per cel, de hoeveelheid cellen zal aangegeven moeten worden door middel van een array, hierbij zal 1 extra cell opgetelt moeten worden.

Voorbeelden

new string[7] = "String"; // Aangezien de tekst 6 characters is zullen er 7 cellen moeten zijn vanwege de extra cell.

string[0] = 'S';
string[1] = 't';
string[2] = 'r';
string[3] = 'i';
string[4] = 'n';
string[5] = 'g';

string[6] = 0;


Meest gebruikte mogelijkheid

new string[255];


Mogelijke gebruikswijze

new string[7] = "String";
SendClientMessage(playerid, 0xFFFFFFAA, string);


Boolean

Het variabele type "Boolean" word gebruikt om waar (true) of niet waar (false) in op te slaan, waar staat gelijk aan 1 en niet waar aan 0. Nieuw aangemaakt Booleans zijn automatisch niet waar.

Voorbeelden

new bool:boolname;

Mogelijke gebruikswijze

if(boolname == true) { /* mocht boolname waar zijn, voer dan iets uit */ }


Array

Arrays are large storage places for data to be stored, they are virtual tables. You can have single dimension arrays and multi dimension arrays. You must create a new float or boolean array the same as you would with a non-arrayed variable by using the predefined tag names (bool:, Float:).

Note that in an array you CANNOT use last row or ligne

One-dimensional array

A one-dimensional array is a single row split into the specified amount of cells.

new onedimensionalarray[CELLS];


Example

new onedimensionarray[5] = {4, 6, 7, 9, 2}; // The "5" contained in the [] states how many cells there are for data to be stored in.

Possible Use

new randomnumber = random(sizeof(onedimensionalarray)); // Gets random number for cell.
printf("Random number is: %d", onedimensionalarray[random]); // Uses random number to select a cell.


Two-dimensional array

Two dimensional array allows you to have many collumns and rows.

new twodimensionalarray[ROWS][COLLUMNS];


Example

new Float:twodimensionarray[4][3] = {

{3.0, 4.5, 5.76}, // Row number 0
{10.85, 76.34, 23.54}, // Row number 1
{2.32, 10.23, 80.2}, // Row number 2
{6.6666, 45.3, 32.64} // Row number 3

};

Possible Usage

SetPlayerPos(playerid, twodimensionalarray[2][2], twodimensionalarray[3][1], twodimensionalarray[2][0]);


Tree-dimensional array - Two-dimensional array string

new treedimensionalarray[ROWS][COLLUMNS][Z];


Two-dimensional array string

new twodimensionalarraystring[ROWS][COLLUMNS][255]; //for a 256 bytes long string


Conditional

Conditionals allow you to see if data meets conditions then execute code depending on the result. The most common type of conditional would be the if/else conditional, but their are also many others - case, for loop and while loop.

if/else

The if/else conditionals allow you see if a set of data meets a condition by the use of operators and then if the data does meet the condition - execute the code.

Relational Operators

Relational operators are commonly used in if/else statements; they check the relationship between two sets of data before executing code.

Operator Meaning Usage
== Left is equal to Right if(Left == Right)
!= Left is not equal to Right if(Left != Right)
> Left is greater than Right if(Left > Right)
>= Left is greater than or equal to Right if(Left >= Right)
< Left is less than Right if(Left < Right)
<= Left is less than or equal to Right if(Left <= Right)
Operator Meaning Usage
&& AND if(Left && Right)
|| OR if(Left || Right)
 ! NO if(!Variable)
NOR if(!(Left || Right))
NAND if(!(Left && Right))
XOR if((Left && !Right)||(!Left && Right))
NXOR if(!((Left && !Right)||(!Left && Right)))

Example

if(lvalue > rvalue) { /* Execute code */ }


In this example if the conditions between lvalue and rvalue are true it will execute the code. The conditions are if lvalue is greater than rvalue...

This article is a stub. You can help SA-MP by expanding it.

Personal tools
In other languages