SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 31/07/2010, 05:06 PM   #1
Toni
High-roller
 
Toni's Avatar
 
Join Date: Aug 2009
Location: United States
Posts: 1,498
Reputation: 156
Default 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).
Code:
#include <zcmd>
So now we can use zcmd!

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

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

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"
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 Y_Less).

Next we check if the player they entered is connected or not:
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.
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.
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.
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:
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 - |
__________________

Last edited by Toni; 19/09/2010 at 04:04 PM.
Toni is offline   Reply With Quote
Old 31/07/2010, 11:59 PM   #2
Kevin_Joshen
Huge Clucker
 
Kevin_Joshen's Avatar
 
Join Date: Jul 2010
Posts: 321
Reputation: 0
Default Re: Making a simple PM system with ZCMD & sscanf

Excellent tutorial.
__________________


Check out the Forums!



Banner made by Zh3R0
Kevin_Joshen is offline   Reply With Quote
Old 01/08/2010, 12:21 AM   #3
willsuckformoney
High-roller
 
willsuckformoney's Avatar
 
Join Date: Dec 2009
Posts: 1,518
Reputation: 65
Default Re: Making a simple PM system with ZCMD & sscanf

teheh nice i can easily change that to dcmd xD tanks a lot
__________________
Fuck the system.
willsuckformoney is offline   Reply With Quote
Old 01/08/2010, 12:34 AM   #4
Kevin_Joshen
Huge Clucker
 
Kevin_Joshen's Avatar
 
Join Date: Jul 2010
Posts: 321
Reputation: 0
Default Re: Making a simple PM system with ZCMD & sscanf

Quote:
Originally Posted by willsuckformoney View Post
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)
__________________


Check out the Forums!



Banner made by Zh3R0
Kevin_Joshen is offline   Reply With Quote
Old 01/08/2010, 12:56 AM   #5
Kar
High-roller
 
Kar's Avatar
 
Join Date: May 2010
Location: Black Asylum
Posts: 3,211
Reputation: 498
Default Re: Making a simple PM system with ZCMD & sscanf

just change CMDm to dcmd_pm
Kar is offline   Reply With Quote
Old 01/08/2010, 02:45 AM   #6
willsuckformoney
High-roller
 
willsuckformoney's Avatar
 
Join Date: Dec 2009
Posts: 1,518
Reputation: 65
Default Re: Making a simple PM system with ZCMD & sscanf

^^ && sure i was busy with other stuff /me /finish now xD
__________________
Fuck the system.
willsuckformoney is offline   Reply With Quote
Old 01/08/2010, 12:31 PM   #7
hab2ever
Huge Clucker
 
hab2ever's Avatar
 
Join Date: Feb 2010
Posts: 287
Reputation: 5
Default Re: Making a simple PM system with ZCMD & sscanf

Awesome xD, Nice job
hab2ever is offline   Reply With Quote
Old 01/08/2010, 01:59 PM   #8
Toni
High-roller
 
Toni's Avatar
 
Join Date: Aug 2009
Location: United States
Posts: 1,498
Reputation: 156
Default Re: Making a simple PM system with ZCMD & sscanf

Thanks guy
__________________
Toni is offline   Reply With Quote
Old 01/08/2010, 04:30 PM   #9
M3ntaL
Little Clucker
 
Join Date: Jul 2010
Posts: 44
Reputation: 0
Default 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 ^^.
M3ntaL is offline   Reply With Quote
Old 01/08/2010, 04:35 PM   #10
Sergei
High-roller
 
Sergei's Avatar
 
Join Date: Mar 2008
Location: Slovenia
Posts: 2,911
Reputation: 230
Default Re: Making a simple PM system with ZCMD & sscanf

Uh, uh. This would be working optimized code.

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;
}
__________________
Watch out, scouts everywhere!
Sergei 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
ZCMD/SSCANF PROBLEM Zeromanster Help Archive 2 27/02/2010 11:26 PM
Simple ZCMD Tutorial Script (.Aztec); Filterscripts 5 13/01/2010 12:58 AM
ZCMD + SSCANF PROBLEM Zeromanster Help Archive 6 22/11/2009 01:13 AM
sscanf and zcmd question about commands with multi parameters Dujma Help Archive 5 21/11/2009 01:24 AM
[Help]Zcmd & sscanf Dirty_bum Help Archive 3 28/10/2009 12:10 PM


All times are GMT. The time now is 11:27 AM.


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