SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Tutorials (https://forum.sa-mp.com/forumdisplay.php?f=70)
-   -   [Tutorial] Making a simple PM system with ZCMD & sscanf (https://forum.sa-mp.com/showthread.php?t=164500)

Toni 31/07/2010 05:06 PM

Making a simple PM system with ZCMD & sscanf
 
Hi there, I'll show you have to make a simple PM command system using ZCMD (Click Me!) and sscanf.

First off, lets include zcmd (download it if you don't have it).
pawn Code:
#include <zcmd>
So now we can use zcmd!

Now, lets create the command pm.
pawn Code:
CMD:pm(playerid, params[])
{
    return 1;
}

Now that we have the actual command, we can start declaring new things to add.

pawn Code:
new str[128], str2[128], id, Name1[MAX_PLAYER_NAME], Name2[MAX_PLAYER_NAME];

We need to make those, so we don't get "Unknown Symbol".

So, next up is using sscanf to check if the player typed anything after "/pm"
pawn Code:
if(sscanf(params, "us", id, str2))
{
    SendClientMessage(playerid, 0xFF0000FF, "Usage: /pm <id/name> <message>");
    return 1;
}
Lets sort it out:
u = ID, or Name.
s = string.

{Float,_}:...
ID/Name = The ID/Name they inserted into their PM.
str2 = The message they inserted into their PM.

------------------------------------------------

So now that we have checked if they inserted a ID and Message, sscanf automatically assigns it to the declaration.

so no need to do str2 = strrest(params, idx). (Thank god ******).

Next we check if the player they entered is connected or not:
pawn Code:
if(!IsPlayerConnected(id)) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: That player is not connected!");
//We did IsPlayerConnected(id)) because we are checking the player they want to send the pm too.

Now we check if they sent the PM to themselves, or another person.
pawn Code:
if(playerid != id) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: You cannot pm yourself!");

So now we move on to sending the messages!

First off, we need to get the player's name, and the receiver's name.
pawn Code:
GetPlayerName(playerid, Name1, sizeof(Name2)); //The Sender's Name so we use (playerid).
GetPlayerName(id, Name2, sizeof(Name2)); //The Receiver's Name so we use (id).


Then, now that we have their names, we format the string (message) to send/receive.
pawn Code:
//This is where we use string #1 (str). Now that we have created, we float The Receiver's name, their ID, and the message!
format(str, sizeof(str), "PM To %s(ID %d): %s", Name2, id, str2);
//Now we send the sender a message repeating what they sent to the receiver:
SendClientMessage(playerid, 0xFF0000FF, str); //Notice its (playerid, not id)

//Now we repeat the first format, but we change it a little, this is the message getting sent to the receiver:
format(str, sizeof(str), "PM From %s(ID %d): %s", Name1, playerid, str2);
//Float Sender's Name, Sender's ID, and Sender's Message.

//After we have formated the message, we send it to the receiver!
SendClientMessage(id, 0xFF0000FF, Message); //Notice its (id, not playerid).


And so, with all of this added, it should look something like this:
pawn Code:
CMD:pm(playerid, params[])
{
    new str[256], str2[256], id, Name1[MAX_PLAYER_NAME], Name2[MAX_PLAYER_NAME];
    if(sscanf(params, "us", id, str2))
    {
        SendClientMessage(playerid, 0xFF0000FF, "Usage: /pm <id> <message>");
        return 1;
    }
    if(!IsPlayerConnected(id)) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player not connected");
    if(playerid == id) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: You cannot pm yourself!")
    {
        GetPlayerName(playerid, Name1, sizeof(Name1));
        GetPlayerName(id, Name2, sizeof(Name2));
        format(str, sizeof(str), "PM To %s(ID %d): %s", Name2, id, str2);
        SendClientMessage(playerid, 0xFF0000FF, str);
        format(str, sizeof(str), "PM From %s(ID %d): %s", Name1, playerid, str2);
        SendClientMessage(id, 0xFF0000FF, str);
    }
    return 1;
}

Hoped this helped!

| - The Toni - |

Kevin_Joshen 31/07/2010 11:59 PM

Re: Making a simple PM system with ZCMD & sscanf
 
Excellent tutorial.

willsuckformoney 01/08/2010 12:21 AM

Re: Making a simple PM system with ZCMD & sscanf
 
teheh nice i can easily change that to dcmd xD tanks a lot

Kevin_Joshen 01/08/2010 12:34 AM

Re: Making a simple PM system with ZCMD & sscanf
 
Quote:

Originally Posted by willsuckformoney (Post 774169)
teheh nice i can easily change that to dcmd xD tanks a lot




would you mind giving me that dcmd coding when you finish it? =D (im too lazy to do it myself)

Kar 01/08/2010 12:56 AM

Re: Making a simple PM system with ZCMD & sscanf
 
just change CMD:pm to dcmd_pm

willsuckformoney 01/08/2010 02:45 AM

Re: Making a simple PM system with ZCMD & sscanf
 
^^ && sure i was busy with other stuff /me /finish now xD

hab2ever 01/08/2010 12:31 PM

Re: Making a simple PM system with ZCMD & sscanf
 
Awesome xD, Nice job ;)

Toni 01/08/2010 01:59 PM

Re: Making a simple PM system with ZCMD & sscanf
 
Thanks guy :)

M3ntaL 01/08/2010 04:30 PM

Re: Making a simple PM system with ZCMD & sscanf
 
I got a problem :(

Did everything right, i gues, put the include
Code:

#include <zcmd>
and the code
Code:

CMD:es(playerid, params[])
{
    new str[256], str2[256], id, Name1[MAX_PLAYER_NAME], Name2[MAX_PLAYER_NAME];
    if(sscanf(params, "us", id, str2))
    {
            SendClientMessage(playerid, 0xFF0000FF, "Vihje: /es <id> <sõnum>");
                  return 1;
    }
    if(!IsPlayerConnected(id)) return SendClientMessage(playerid, 0xFF0000FF, "VIGA: Player not connected");
    if(playerid != id) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: You cannot pm yourself!");
    {
                GetPlayerName(playerid, Name1, sizeof(Name1));
                GetPlayerName(id, Name2, sizeof(Name2));
        format(str, sizeof(str), "ES kasutajalt %s(ID %d): %s", Name2, id, str2);
        SendClientMessage(playerid, 0xFF0000FF, str);
        format(str, sizeof(str), "ES kasutajalt %s(ID %d): %s", Name1, playerid, str2);
        SendClientMessage(id, 0xFF0000FF, str);
    }
    return 1;
}


but i keep getting this error while i try to compile it ( just started scripting, i think i'm with good brain... just dont figure this out )

Code:

C:\Documents and Settings\Owner\Desktop\BBRP GM\gamemodes\bbrp.pwn(51) : error 017: undefined symbol "sscanf"
Pawn compiler 3.2.3664                          Copyright (c) 1997-2006, ITB CompuPhase


1 Error.


Btw. It's translated to Estonian, as you can see, dont mind that ^^.

Sergei 01/08/2010 04:35 PM

Re: Making a simple PM system with ZCMD & sscanf
 
Uh, uh. This would be working optimized code.

pawn Code:
CMD:pm(playerid, params[])
{
    new str[128],id,pname[MAX_PLAYER_NAME];
    if(sscanf(params, "us", id, params)) SendClientMessage(playerid, 0xFF0000FF, "Usage: /pm <id> <message>");
    else if(id == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player not connected");
    if(playerid == id) SendClientMessage(playerid, 0xFF0000FF, "ERROR: You cannot pm yourself!")
    else
    {
        GetPlayerName(id, pname, sizeof(pname));
        format(str, sizeof(str), "PM To %s(ID %d): %s", pname, id, params);
        SendClientMessage(playerid, 0xFF0000FF, str);
        GetPlayerName(playerid, pname, sizeof(pname));
        format(str, sizeof(str), "PM From %s(ID %d): %s", pname, playerid, params);
        SendClientMessage(id, 0xFF0000FF, str);
    }
    return 1;
}


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

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