Trying to learn pawn... question about this piece of script
#1

***Sorry i put this in the wrong section.. meant for it to be in script discussion. can a moderator please move this? thanks



Ok, so first let me show u the script that I copied from a site

public OnPlayerDisconnect(playerid, reason)
{
new pName[MAX_PLAYER_NAME], string[56];
GetPlayerName(playerid, pName, sizeof(pName));
switch(reason)
{
case 0: format(string, sizeof(string), "%s has left the server. (Lost Connection)", pName);
case 1: format(string, sizeof(string), "%s has left the server. (Leaving)", pName);
case 2: format(string, sizeof(string), "%s has left the server. (Kicked)", pName);
}
SendClientMessageToAll(0xAAAAAAAA, string);
return 1;
}

Ok, so I am just going to tell you what I am guessing, please tell me if I am wrong or right.

1. the line "public OnPlayerDisconnect(playerid, reason)" is basically saying that whatever is written after this happens happens when a player disconnects
2. the line "new pName[MAX_PLAYER_NAME], string[56];" sets pName so that from here on whenever u use pName (don't know how to use it though) it replaces pName with the players name
3. the line "GetPlayerName(playerid, pName, sizeof(pName)); " is somehow linked to the line right before it...
4. the line "switch(reason)" no idea
5. the case 0, 1 and 2 is used so that later on, should A happen, go to case 0, should B happen, go to case 1
6 sendclientmessagetoall pretty easy to guess, just sends a message to every one
7. can someone please explain the "string" at the end of "SendClientMessageToAll(0xAAAAAAAA, string);"


Thanks so much for ur help, im trying to learn pawno and from past experience with languages it's best to teach ur self
Reply
#2

I'm gonna try to explain for you as good as i can.

Code:
public OnPlayerDisconnect(playerid, reason)
that callback explains for the server what the server is gonna do if a player leaves.

Code:
case 0: format(string, sizeof(string), "%s has left the server. (Lost Connection)", pName);
For an example, if a player is crashing for something, this string is sended to one or all players on the server and generates the message "XziPer2007 has left the server. (Lost Connection)". pName has been forwarded so the script recognize it, so pName makes %s to a name instead, XziPer2007 as i wrote abow.

Code:
SendClientMessageToAll(0xAAAAAAAA, string);
If you wanna make so that specific string is sended to all players connected on the server, SendClientMessageToAll makes so every player gets the message. 0xAAAAAAAA is the color of the next, and this one is white if i'm not completly wrong. String is the message that you wrote above, so you dont need to do like this:

Code:
public OnPlayerDisconnect(playerid, reason)
{
  SendClientMessageToAll{0xAAAAAAAA, "Someone has left the server"};
  return 1;
}
_________________________________________________

Also, Case 0, 1, 2 indicates how the player left the server, so if a player crash, the server recognize it as Case 0, If a player press esc, then presses exit or doing /q, the server recognize it as the player left the server, and Case 2 is when if someone is kicked or banned
Reply
#3


pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
}
This is a callback. Callbacks get executed on certain events. As its name says, this callback get executed when a player disconnects.

pawn Code:
new pName[MAX_PLAYER_NAME], string[56];
Here are 2 declarations. They declare variables, which will be used later in the script. They are declared as arrays; pName has 24 cells (MAx_PLAYER_NAME is defined as 24 in a_samp.inc) and string has 56 cells. Imagine them as tables

pawn Code:
GetPlayerName(playerid, pName, sizeof(pName));
https://sampwiki.blast.hk/wiki/GetPlayerName
This is a function. It gets the players name and stores it in a string (strings are a combination of letters. They are stored in arrays.) 'playerid' is the id of the player you want the name, 'pName' is the name of the array you want the name to be stored and sizeof(pName) calculates the size of the array so GetPlayerName can store the name in

pawn Code:
switch(reason)
  {
    case 0:
    case 1:
    case 2:
  }
This is a statement. Switch statements are used instead of alot of 'if's to conserve space and similar things. Switch has only one parameter, which is the number you want to compare the cases with. In this case, its 'reason'. Reason is pre-defined in OnPlayerDisconnect (because It's in the ( ), same as with 'playerid'. Reason is an integer (whole number) and you can only use integers in switch statements. If a player crashes, the reason will be 0, if he leaves it will be 1 and if he's kicked/banned, the reason will be 2. This is made like that by the samp team

pawn Code:
format(string, sizeof(string), "%s has left the server. (Lost Connection)", pName);
This is the format function. With it you can create sentences using strings stored in other arrays. The first parameter (string) is the name of the array you will be storing data in. The second parameter calculates the available size. The third parameter is the text itself. '%s' in the text means string, %d means integer, %f means float (decimal number) and %c means character (single letter). The continuing parameters are the strings/integers, etc you written in the text. In this case, It's just one, because the text needs just one string to replace the %s. And that will be the string pName, which contains the players name, you got earlier

pawn Code:
SendClientMessageToAll(0xAAAAAAAA, string);
This is pretty obvious, It sends a message to all connected players. 0xAAAAAAAA is the hex value of a color and 'string' is the name of the array which will be sent( playername has left the server. (reason) ).

pawn Code:
return 1;
This tells you if something has completed succesfuly
Reply
#4

OK thanks both of you guys I think I am beginning to pick up on more and more Pawno here and there.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)