[Tutorial] PAWN For Beginners 2 - Variables and ZCMD
#1

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 ?


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.
So, let's go over some integer variables, shall we ?

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.
Now let's go over mathematical operations that we can do with our variables:

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.
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 Код:
== // 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.
That wasnt so clear ? So lets go over it now !

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
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 Код:
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 Код:
new myName = "Yigit";
I can set myName variable whenever I want by the same method above;

pawn Код:
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 Код:
main()
{
   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;

Код:
%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 Код:
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
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 Код:
COMMAND:mycommand(playerid,params[])
or

pawn Код:
CMD:mycommand(playerid,params[])
You shouldn't add this to publics, it must be out of publics !

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.
So, here we go to do the magic ! :

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 ?");
  }
}
or more easily, you can do it like this:

pawn Код:
public OnPlayerCommandPerformed(playerid, cmdtext[], success)
{
   if(!success) return SendClientMessage(playerid,-1,"[ERROR]: This command does not exist here, maybe try something else ?);
}
Use it however you like it.

pawn Код:
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 !


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

Yes indeed, fix your indentation or the newbies which read your tutorial will have a big problem in the future.

Edit: What I am trying to say is, they will get used to this indentation and they will have hard time reading code.
Reply
#3

Quote:
Originally Posted by Tamer T
Посмотреть сообщение
Yes indeed, fix your indentation or the newbies which read your tutorial will have a big problem in the future.
Other than the problems that could occur (basically just hard to read code and a warning at compile), you should always indent properly no matter what programming language, or for that matter even markup language should have indentation. It makes it look neater and easier to read as well as easily being able to distinguish between different parts of your code.
Reply
#4

Quote:
Originally Posted by ******
Посмотреть сообщение
One VERY important thing for beginners that you have entirely missed (and indeed got wrong) is indentation - you have none at all.
I fixed the indentation mistakes, thanks for your feedback ^^, I usually do

pawn Код:
#pragma tabsize 0
so I ignore all indentations. That is kind of a mistake but it annoys me :P
Reply
#5

#pragma tabsize 0 and #pragma unused symbol just hides the warnings. They don't solve them.

If you are too lazy to indent properly, do your code and paste it all to http://codegenerators.pl
Then click indent.
Reply
#6

Quote:
Originally Posted by S4t3K
Посмотреть сообщение
#pragma tabsize 0 and #pragma unused symbol just hides the warnings. They don't solve them.

If you are too lazy to indent properly, do your code and paste it all to http://codegenerators.pl
Then click indent.
Oh thanks dude, I will use this from now on ^^

Quote:
Originally Posted by ******
That is not only INCREDIBLY bad practice, but explicitly BANNED by forum rules!
Sorry then :/
Reply
#7

Very nice, very detailed, very good.
Reply
#8

Quote:
Originally Posted by iRaiDeN
Посмотреть сообщение
Very nice, very detailed, very good.
Thanks for your feedback !
Reply
#9

You missed the modulo operator:

pawn Код:
a = 3;
b = 2;
a % b = 1.5
Reply
#10

@sammp : Wrong.

PHP код:

new 3;
new 
2;
new 
0;
new 
Float:1.5
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)