PAWN FOR BEGINNERS SERIES - 2
PLOT
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: http://ift.tt/SwfH23 So let's start shall we ?
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 Code:
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.
So, let's go over some integer variables, shall we ?
pawn Code:
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.
Now let's go over mathematical operations that we can do with our variables:
pawn Code:
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.
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.
This is mathematical operations you can do with your variables. There are also a few more. Let's go over them too, shall we ?
pawn Code:
== // 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.
+= // This adds our variable desired amount.
-= // This decreases our variable desired amount.
*= // This multiplies our variable desired amount.
/= // This divides our variable desired amount.
That wasnt so clear ? So lets go over it now !
pawn Code:
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)
}
{
SendClientMessage(playerid,-1,"You successfully found myNumber !");// Sends message to a specific player with the color of -1(white)
}
pawn Code:
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
myNumber -= 5; // This decreases myNumber variable by 5
myNumber *= 6; // This multiplies myNumber variable by 6
myNumber /= 7 // This divides myNumber variable by 7
Notice that I didn't put new in front of them, because we ALREADY defined our variables and we are just setting their values. Remember what I said in the beginning ? We CAN edit the variables we set in the beginning even in the end of our script ! Are there any other methods for setting integer variables ? Of course there is !
pawn Code:
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.
I think that is all for integer variables. Now let's go for STRING variables !
Remember we set myName variable to Yigit by this ? :
pawn Code:
new myName = "Yigit";
I can set myName variable whenever I want by the same method above;
pawn Code:
myName = "Isn't Yigit";
We are putting " again because this is a string variable NOT a integer variable. Where can we use these string variables ? Such as;
pawn Code:
main()
{
printf("My name is %s"),myName);
}
{
printf("My name is %s"),myName);
}
This will just print "My name is Yigit to console as we set Yigit as our myName variable. By the way, %s means STRING VARIABLE. There are a few more;
Code:
%s - String Variable
%d - Integer Variable
%i - Integer Variable
%f - Floating point
%c ASCII character
%x Hexadecimal number
%b - Binary Number
%% - Literal ( % )
However, we use %s, %d, %i mostly. We use format for using variables effectively such as,
pawn Code:
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
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
This was all I can explain about variables. Now let's go for ZCMD shall we ?
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 Code:
COMMAND:mycommand(playerid,params[])
or
pawn Code:
CMD:mycommand(playerid,params[])
You shouldn't add this to publics, it must be out of publics !
pawn Code:
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.
So, here we go to do the magic ! :
pawn Code:
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 ?");
}
}
{
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 ?");
}
}
or more easily, you can do it like this:
pawn Code:
public OnPlayerCommandPerformed(playerid, cmdtext[], success)
{
if(!success) return SendClientMessage(playerid,-1,"[ERROR]: This command does not exist here, maybe try something else ?);
}
{
if(!success) return SendClientMessage(playerid,-1,"[ERROR]: This command does not exist here, maybe try something else ?);
}
Use it however you like it.
pawn Code:
if(!strlen(params)) // This one means if parameters string is empty. This is used for USAGE: messages.
such as:
[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 ! :D
Aucun commentaire:
Enregistrer un commentaire