SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 10/06/2014, 05:23 PM   #1
Rufio
Huge Clucker
 
Join Date: May 2011
Location: Turkey
Posts: 415
Reputation: 40
Default PAWN For Beginners 2 - Variables and ZCMD

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: http://forum.sa-mp.com/showthread.php?t=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 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.

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.

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)
}

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

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);
}
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

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 ?");
  }
}

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 ?);
}

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 Code:
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 ! http://forum.sa-mp.com/showthread.php?t=91354 - You can download it here.

Last edited by Rufio; 12/06/2014 at 05:25 PM.
Rufio is offline   Reply With Quote
Old 12/06/2014, 03:39 PM   #2
Tamer
High-roller
 
Tamer's Avatar
 
Join Date: Nov 2011
Location: Turkey
Posts: 1,101
Reputation: 172
Default Re: PAWN For Beginners 2 - Variables and ZCMD

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.
__________________
The three stages of truth:

- Ridicule.
- Violent opposition.
- Acceptance.


Quote:
The greatest hypnotist on the planet Earth is an oblong box in the corner of the room. It is constantly telling us what to believe is real. If you can persuade people that what they see with their eyes is what there is to see you've got them. you know because they'll laugh in your face of an explanation that portrays the biggest picture of what's happening... and they have.
Tamer is offline   Reply With Quote
Old 12/06/2014, 03:42 PM   #3
NewerthRoleplay
High-roller
 
NewerthRoleplay's Avatar
 
Join Date: Nov 2011
Location: England :: The Danger Zone
Posts: 1,014
Reputation: 133
Default Re: PAWN For Beginners 2 - Variables and ZCMD

Quote:
Originally Posted by Tamer T View Post
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.
__________________
NewerthRoleplay is offline   Reply With Quote
Old 12/06/2014, 05:26 PM   #4
Rufio
Huge Clucker
 
Join Date: May 2011
Location: Turkey
Posts: 415
Reputation: 40
Default Re: PAWN For Beginners 2 - Variables and ZCMD

Quote:
Originally Posted by ****** View Post
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 Code:
#pragma tabsize 0
so I ignore all indentations. That is kind of a mistake but it annoys me :P
Rufio is offline   Reply With Quote
Old 12/06/2014, 06:08 PM   #5
S4t3K
Gangsta
 
S4t3K's Avatar
 
Join Date: Jan 2013
Posts: 864
Reputation: 165
Default Re : PAWN For Beginners 2 - Variables and ZCMD

#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.
__________________
S4t3K is offline   Reply With Quote
Old 12/06/2014, 07:28 PM   #6
Rufio
Huge Clucker
 
Join Date: May 2011
Location: Turkey
Posts: 415
Reputation: 40
Default Re: Re : PAWN For Beginners 2 - Variables and ZCMD

Quote:
Originally Posted by S4t3K View Post
#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 :/
Rufio is offline   Reply With Quote
Old 12/06/2014, 07:36 PM   #7
iRaiDeN
Huge Clucker
 
iRaiDeN's Avatar
 
Join Date: Mar 2014
Location: Israel
Posts: 480
Reputation: 41
Default Re: PAWN For Beginners 2 - Variables and ZCMD

Very nice, very detailed, very good.
iRaiDeN is offline   Reply With Quote
Old 12/06/2014, 07:41 PM   #8
Rufio
Huge Clucker
 
Join Date: May 2011
Location: Turkey
Posts: 415
Reputation: 40
Default Re: PAWN For Beginners 2 - Variables and ZCMD

Quote:
Originally Posted by iRaiDeN View Post
Very nice, very detailed, very good.
Thanks for your feedback !
Rufio is offline   Reply With Quote
Old 02/07/2014, 05:23 PM   #9
sammp
Gangsta
 
sammp's Avatar
 
Join Date: Jan 2014
Posts: 585
Reputation: 49
Default Re: PAWN For Beginners 2 - Variables and ZCMD

You missed the modulo operator:

pawn Code:
a = 3;
b = 2;
a % b = 1.5
__________________
SA-MP must become open-source to maximise longetivity of the project.
sammp is offline   Reply With Quote
Old 02/07/2014, 05:38 PM   #10
S4t3K
Gangsta
 
S4t3K's Avatar
 
Join Date: Jan 2013
Posts: 864
Reputation: 165
Default Re : PAWN For Beginners 2 - Variables and ZCMD

@sammp : Wrong.

PHP Code:

new 3;
new 
2;
new 
0;
new 
Float:1.5
__________________
S4t3K is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] Beginners Guide to PAWN & Welcome/Leave Messages ScriptBling Tutorials 9 21/04/2014 02:09 PM
[Tutorial] Learning PAWN for Beginners AlonzoTorres Tutorials 27 13/04/2014 11:59 PM
[Tutorial] PAWN For Beginners : 1 - Understanding PAWN Rufio Tutorials 11 18/07/2013 07:10 PM
Variables for Beginners [ERP]Snow Help Archive 2 21/08/2009 10:02 AM


All times are GMT. The time now is 09:30 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.