[Tutorial] Basic Scripting Series: [1]Starting with a GameMode and making a command
#1

INTRO
Hello all, I have put together this simple tutorial on "Basic Scripting" for PAWN.
NOTE: PAWN codes are explained at the top of the code, not at the bottom! Don't get it confused!

First off, we'll start with the requirements:
1. SA:MP Server Package
2. PAWN, but use the PAWN program you receive when you download your SA:MP server package.
3. ZCMD, a simple and fast command processor
4. Very basic knowledge of scripting, e.g. What a comment is

This is one of the tutorials in my series for "Basic Scripting"
These are the tutorials:
[1] Starting with a new GameMode, what do I do?
[2] Implementing a filterscript
[3] Making connect/disconnect messages
[4] Putting your own things on the map

Not all of them are finished, as I have just started and it will take a while to explain all of them, thats why I broke them down into 4 tutorials and will make them a series.

My screen name is nitrochegs and I, myself, am new at scripting but know the basics and have enough knowledge to teach it. I would like to help people learn PAWN and its language (PAWNO) and I would like to finally see an organized tutorial on scripting that is simple, but yet easy to read.

Alright so lets get started, shall we?

[1] Starting with a new GameMode, what do I do?
This chapter explains basic things you will use in your script.

So, you have opened your PAWN.exe file and you have a blank screen, so you click "new" and it generated a blank script.

- Getting started
Assuming you already know the very basic things of pawn, like what a comment is, then we'll get started. If you do not, go elsewhere, this tutorial is for people who want to know how to get started, not what green text means.

- Includes
Includes are basic things that you will use in your script. Right now you should have this in the top portion of your script:
pawn Код:
#include <a_samp>
Inside this include, there are basic things that your server will use, ex: SendClientMessage to send players messages. Without it, you'll be raided by error messages when you compile your script. That's of course, if you use functions like "SendClientMessage"

- Defines
Now you are probably looking for Defines inside your script because you see that this part is about Defines, well you shouldn't have any yet (assuming that you are starting from scratch). So let me explain to you what a define is.

A define is basically what its name is, it defines something. So if you put a color like "red" , for example, in SendClientMessage, and you have red defined at the top, then the text you send to the player will be red.

So, how do we make a "Define"? Well that's really simple, for this tutorial, I will be showing you how to make a color define. PAWN uses hexadecimal colors for the colors players will see when they are in the server, so if you want to use the color red inside a SendClientMessage function, then you can define it at the top so that you don't have to type the hexadecimal color every time you use the color red.

So, lets start making the define. I want to define red so I don't have to always keep typing its hexadecimal, so instead ill define it. Go to any color picker for hexadecimal colors your find online like this one, for example. And pick a color, then copy its hexadecimal. My red's hexadecimal is "D4224E". Type this at the top of your script, below your includes:
pawn Код:
#define red 0xD4224EFF
Notice that I added 0x to the front and FF to the end of the hexadecimal, well thats what PAWN uses to recognize a color, so thats why it is added. And there you go! You have just defined a color in your script! Congratulations!

- Making a command
So by this time, you would want to make a command for your server. Lets make a demo command so that when players type "/imbored", for example, it'll tell everyone in the server that "playername is bored" but of course it'll say the name of the player who typed /imbored. I reccommend for you to use ZCMD, which is a command processor, instead of the default strcmp. This whole tutorial will be based off of ZCMD. Be sure to make sure you include ZCMD at the top of your script or no commands will work and it won't compile!

So in ZCMD, making a command is much easier for people to type, especially if you are going to have a lot of commands in your server.

So, lets start making the command.
In ZCMD, you start off with "CMD:" by itself, and nothing but itself.
Now after "CMD:" type the command itself, but without the "/" so it'll be "CMD:imbored"
Now you need to let PAWN know that you will be using playerid and params[] inside the next lines for your command.
To do this, put "(playerid,params[])" after "CMD:imbored"
So, so far you should have:
pawn Код:
CMD:imbored (playerid,params[])
Now that we told PAWN that this is a command, we need to tell PAWN what the command does.
On anything, you need to tell PAWN where something starts, with an opening bracket "{"
That way, PAWN knows that anything after "{" is what the thing will be doing.
So, add the opening bracket below "CMD:imbored (playerid,params[])"
Now we will tell PAWN what to do when the player types /imbored.

So, we need to make a string, but lets put the length of the string at the top of the command line, below the opening bracket, to tell the whole command how long the string will be.
So, to do this, you will need to put "new" and then "string" so that we can tell PAWN what we are doing.
Then, we put the length of the string, if the length is too low, like "1", for example, it will only show 1 character!
The max characters inside the line will be 128 for this example. So, right now you would need to have:
pawn Код:
new string [128];
Next, we'll give "MAX_PLAYER_NAME" a name, pname, for example, "p" being "player". Now some of you are asking what the hell is "MAX_PLAYER_NAME", MAX_PLAYER_NAME is the player's name. We just want to give it a real, more simple to type, name. The name we will be using for it is pname. So, we'll do the same thing as we defined the string length, but without any numbers.
First, type "new" then after that type "pname" then "MAX_PLAYER_NAME" immediately after, but have it surrounded by brackets. Then, of course, end the line with a semi-colon ";". Now we have told PAWN that MAX_PLAYER_NAME is pname in this command we are making.
You should've made this if you are following along:
pawn Код:
new pname[MAX_PLAYER_NAME];
So, then we need to get the player's name so that we can use their name when they type /imbored.
To get a player's name use the "GetPlayerName" function. You can find this in your sidebar, below "// a_players.inc"
The function, unedited is:
pawn Код:
GetPlayerName()
but you need to fill the parentheses to tell PAWN what to do. We will be adding 3 things to it, the 3 things being, the playerid, const name, and the length.
Lets start with the playerid, playerid will tell PAWN that the player's name who typed, is the name we want to use. Not some random other innocent player who is having very much fun and isn't bored!

"const name" in this is the player's name. We told PAWN that MAX_PLAYER_NAME will be pname so we'll just use pname and only pname as the const name.

Next is the length of the line. Since the max characters allowed is 24 in player names, then we will make the length "24" at the end of GetPlayerName, but inside the parentheses
Now, all together, if you were following along, you should have:
pawn Код:
GetPlayerName(playerid, pname, 24);
Thanks to LaZ for pointing out my mistake

Now you have the player's name, but you want to put that to use. So, we'll start with formatting the text we will show to the whole server.
Of course, the function we will use will start with "format" then we need to tell it what to format, so we want to format the string, so we type "string", but of course put a parentheses before that.
Next is the length of the string. Now here is where "new string [128];" comes in to play. We already told PAWN that string's length will be "128" characters, so then we will tell it to use the string. Type "sizeof(string)"
Now we want to tell PAWN what to say to all of the players. For this example, I want it to say "playername is bored!"
So, I would put "playername is bored" in quotations after sizeof(string). Now I will replace "playername" with %s.
%s is now a variable. Right now we did not tell pawn what %s is, but basically it is the player's name.
So, in order to tell pawn what the variable is, we will put "pname" after "%s is bored!". Remember that pname is the player's name and we need to tell PAWN what it is before we use it! (see above if you missed it).
So all together you should have
pawn Код:
format(string, sizeof(string), "%s is bored!", pname);
You see, having pname at the end of it will tell PAWN that the first variable is the player's name. If, for example, we had 2 variables instead of one, we would tell PAWN what the 2nd variable is AFTER pname, since it must go in order. If you switched pname and the other variable's name around then everything would be messed up and it would not look right.
So all together the format line should look like:
pawn Код:
format(string, sizeof(string), "%s is bored!", pname);
Now we need to send the message.
Sending a message to people is fairly simple. There are 2 basic kinds of SendClientMessages, and here is what is required in them:
pawn Код:
SendClientMessage(playerid, COLOR, "Message to send");
and SendClientMessagetoAll:
pawn Код:
SendClientMessageToAll(COLOR, string, "Message to send");
But, since we have already formatted the text to send, we do not need to include the message. So, all we have to do is type
pawn Код:
SendClientMessageToAll(red, string);
which tells PAWN the color, then what to send, since we want to send the string above, we type "string".

So, then you must return, by typing "return 1;" then finally closing your command by using the closing bracket "}"

All together this command would look like:
pawn Код:
CMD:imbored(playerid,params[])
{
    new string[128];
    new pname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pname, 24);
    format(string, sizeof(string), "%s is bored!", pname);
    SendClientMessageToAll(red, string);
    return 1;
}
Simple, but complicated right?

Well thats all I have for now. I will add more to my series as time progresses. And I will link them on every tutorial I post. This is my first tutorial and I am new to scripting, so please point out any flaws. I do respect constructive crticisim but only that. Thank you and if I helped you in anyway, please +rep.

Thanks

nitrochegs © 2011, all rights reserved
Reply
#2

Good for Beginners, Rep'ed+
Reply
#3

Have you double checked what you have done? Look at GetPlayerName. The player's name length is 24 characters and not 128.
Reply
#4

Quote:
Originally Posted by [NoV]LaZ
Посмотреть сообщение
Have you double checked what you have done? Look at GetPlayerName. The player's name length is 24 characters and not 128.
GetPlayerName(playerid, pname, sizeof(string));
Lol'd xD
Reply
#5

Quote:
Originally Posted by [NoV]LaZ
Посмотреть сообщение
Have you double checked what you have done? Look at GetPlayerName. The player's name length is 24 characters and not 128.
Ah, my mistake, I see it. Thanks for pointing it out tho
Edited and fixed
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)