10.06.2014, 16:23
(
Последний раз редактировалось Rufio; 12.06.2014 в 16:25.
)
PAWN FOR BEGINNERS SERIES - 2
PLOT
Hello guys, it is me again ! It has been long after my first tutorial for PAWN but here I come again for describing you ZCMD and variables ! This was my first tutorial for anyone interested: https://sampforum.blast.hk/showthread.php?tid=451445 So let's start shall we ?PLOT
1 ) What is a variable ?
Variable is a bit of memory, it is where our PAWN stores the data. The data can be changed/readed and processed if it is needed. We most likely define our variables with "new". Such as:pawn Код:
new myName = "Yigit"; // This defines myName variable, " 's means my variable will be a string. After I do this, I can process myName variable anywhere I want.
pawn Код:
new myNumber = 4; // This defines myNumber variable to be 4, notice that I didn't put " because this is NOT a string variable, this is a INTEGER variable.
pawn Код:
new myNumber = 4; // We already defined this.
new JustAnotherNumber = 2; // This defines JustAnotherNumber variable to be 2.
new Plus = myNumber+JustAnotherNumber; // This defines Plus variable to be an addition of myNumber and JustAnotherNumber which results as 6.
new Minus = myNumber-JustAnotherNumber; // This defines Minus variable to be a subtraction of myNumber and JustAnotherNumber which results as 2.
new Divide = myNumber/JustAnotherNumber; // This defines Divide variable to be a divide of myNumber and JustAnotherNumber which results as 2.
new Multiply = myNumber*JustAnotherNumber; // This defines Multiply variable to be a multiply of myNumber and JustAnotherNumber which results as 8.
pawn Код:
== // This is used in "if/else" statements, such as if(myNumber == 4), this means if myNumber variable is defined as 4.
+= // This adds our variable desired amount.
-= // This decreases our variable desired amount.
*= // This multiplies our variable desired amount.
/= // This divides our variable desired amount.
pawn Код:
if(myNumber == 4) // if myNumber variable was defined as 4
{
SendClientMessage(playerid,-1,"You successfully found myNumber !");// Sends message to a specific player with the color of -1(white)
}
pawn Код:
myNumber += 4; // This increases myNumber variable by 4
myNumber -= 5; // This decreases myNumber variable by 5
myNumber *= 6; // This multiplies myNumber variable by 6
myNumber /= 7 // This divides myNumber variable by 7
pawn Код:
myNumber = myNumber+4; // This means from now on, our myNumber variable will be the addition of 4 and the number we defined on myNumber which means 8. And if we do this once again after setting this it will be addition of 4 and 8 because we are setting myNumber variable to be 8 here. And same method goes for every other mathematical operations.
Remember we set myName variable to Yigit by this ? :
pawn Код:
new myName = "Yigit";
pawn Код:
myName = "Isn't Yigit";
pawn Код:
main()
{
printf("My name is %s"),myName);
}
Код:
%s - String Variable %d - Integer Variable %i - Integer Variable %f - Floating point %c ASCII character %x Hexadecimal number %b - Binary Number %% - Literal ( % )
pawn Код:
new string[128];// This means our string will be max. 128 bits
format(string,sizeof(string),"My name is %s and my number is %i",myName,myNumber); // This formats our text, notice that I put %s for string variable and %i for the integer one !
SendClientMessage(playerid,-1,string); // Sends a client message for specific player
3 ) ZCMD Usage
ZCMD is one of the fastest command processors for SA-MP and it uses SSCANF with it too, and it becomes a great experience for every scripter around SA-MP. It is also way more usefull. You must have zcmd include for SA-MP for using this method. I will give the download link soon.
You can either use
pawn Код:
COMMAND:mycommand(playerid,params[])
pawn Код:
CMD:mycommand(playerid,params[])
pawn Код:
public OnPlayerCommandPerformed(playerid, cmdtext[], success) // This code checks if one of the players performed a command such as /help. We will edit this after this to change SERVER:Unknown command to a cooler one.
pawn Код:
public OnPlayerCommandPerformed(playerid, cmdtext[], success)
{
if(!success) // if that command does NOT exist, notice that '!' symbol means NOT or FALSE in programming language.
{
SendClientMessage(playerid,-1,"[ERROR]: This command does not exist here, maybe try something else ?");
}
}
pawn Код:
public OnPlayerCommandPerformed(playerid, cmdtext[], success)
{
if(!success) return SendClientMessage(playerid,-1,"[ERROR]: This command does not exist here, maybe try something else ?);
}
pawn Код:
if(!strlen(params)) // This one means if parameters string is empty. This is used for USAGE: messages.
pawn Код:
new id; // define an ID variable
if(!sscanf(params,"i",id)) return SendClientMessage(playerid,-1,"USAGE:/thatcommandlel [Player ID]"); // This means if our player does not specify an ID return an usage message. Notice that we used "i" which means an integer variable because player ID's are number, so they are INTEGERS !
Hope this tutorial helped you ! Tell me if I missed something because I am a bit sleepy, I told you guys as much as I could and please do not hesitate telling me my mistakes ! My next tutorial will be about booleans and stuff, have fun in forums guys !

Credits - Zeex for his ZCMD ! https://sampforum.blast.hk/showthread.php?tid=91354 - You can download it here.