How to code a simple PM system, using sscanf2

Veröffentlicht von Knogle am

Many people use different ways to deploy a messaging system to their players.
Now i’ll show you the way i do it.

First of all we have to check if our script is ready to handle the sscanf function we will use.
Check your pawno/includes folder to get sure.

Otherwise get the latest release of sscanf here: https://github.com/maddinat0r/sscanf/releases.

Most of us may use alternative command generators like ZCMD, to handle with players.
I like to keep the default C-like syntax, the reason why we won’t handle with other command generators in this tutorial.

In addition we also won’t use deprecated functions like strtok, so we’ll avoid the use of them completely.

So let’s start.

To get your script ready for sscanf we need to simply include it at the beginning of our gamemode.

//Beginning of your mode


#include <a_samp>
#include <sscanf2>

Now we will continue with our OnPlayerCommandText public.
We need a new string as well as our sscanf function, to avoid the use of strtok.
Using sscanf we can seperate numbers, from the remaining string.

 

public OnPlayerCommandText(playerid, cmdtext[])
{
	new cmd[32], idx;
	sscanf(cmdtext,"s[32]d",cmd,idx);
	

}

 

That’s almost everything what we need.

Now we have to create the commands itself, simplest way is to use the strcmp function.

if(strcmp(cmd, "/pm", true) == 0)
{

}

So our new public will loke like this.

 

public OnPlayerCommandText(playerid, cmdtext[])
{
	new cmd[32], idx;
	sscanf(cmdtext,"s[32]d",cmd,idx);
		if(strcmp(cmd, "/pm", true) == 0)
	{


        }
}

So what will we do here? We get our input by the player, (e.g. /pm 2  [..]) and for now the sscanf function will write „/pm“ into the cmd string, and our number, or playerid 2, into „idx“.

So we have a string and an int to deal with.

Now we need to declare a transmitting as well as a receiving player.

Also we may not forget the message itself.

Let’s continue with our strings.

new str[256], str2[256], id, Name1[MAX_PLAYER_NAME], Name2[MAX_PLAYER_NAME];

We need one string to let the sender know, the message has been sent, another string to let the receiver know the message.

2 more strings to display the sender and the receiver before the message itself.

Now the most important part, our sscanf function comes into play.

 

if(sscanf(cmdtext[strlen("/pm")+1], "us", id, str2))

 

After we have declared the strings, we need to check if all information is given by the player’s input.

return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /pm <id> <message>");
if(!IsPlayerConnected(id))
return SendClientMessage(playerid, COLOR_WHITE, "SERVER: Player not connected");
if(playerid == id)
return SendClientMessage(playerid, COLOR_WHITE, "SERVER: You cannot pm yourself!");

 

In case, our if condition is not true, means the playerid does’nt exist, we receive an error message.

I also added the case, if the sender id and receiver id are the same, the message won’t be sent, because it doesn’t make any sense to do that.

So if nothing went wrong until this part, we can continue to execute our PM command.

We now have our playerids written into each int, playerid, and id(the receiver).

 

Now we want to know, who sent the message, and who received it.

GetPlayerName(playerid, Name1, sizeof(Name1));
GetPlayerName(id, Name2, sizeof(Name2));

Using these functions, we get the name of the player, using the playerid, given by the var after [..]Name(var,… and write it into the given string Name1/Name2.

 

To conclude we’ll format our str1 and str2, to display the sender’s and receiver’s string.

format(str, sizeof(str), "PM To %s(ID %d): %s", Name2, id, str2);
SendClientMessage(playerid, COLOR_YELLOW, str);
format(str, sizeof(str), "PM From %s(ID %d): %s", Name1, playerid, str2);
SendClientMessage(id, COLOR_YELLOW, str);
return 1;

 

Everything was executed successfully, so we add our return 1; to conclude.

So our command now looks like that, including the public.

 

public OnPlayerCommandText(playerid, cmdtext[])
{
	new cmd[32], idx;
	sscanf(cmdtext,"s[32]d",cmd,idx);	
        if(strcmp(cmd, "/pm", true) == 0)
	{
		new str[256], str2[256], id, Name1[MAX_PLAYER_NAME], Name2[MAX_PLAYER_NAME];
		if(sscanf(cmdtext[strlen("/pm")+1], "us", id, str2))
		return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /pm <id> <message>");
		if(!IsPlayerConnected(id))
		return SendClientMessage(playerid, COLOR_WHITE, "SERVER: Player not connected");
		if(playerid == id)
		return SendClientMessage(playerid, COLOR_WHITE, "SERVER: 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, COLOR_YELLOW, str);
		format(str, sizeof(str), "PM From %s(ID %d): %s", Name1, playerid, str2);
		SendClientMessage(id, COLOR_YELLOW, str);
		return 1;
	}
}

That’s all you need to create a basic messaging system, nothing complicated!

To understand all the uses and functions, and ways how to use sscanf, check out it’s GitHub page.

https://github.com/maddinat0r/sscanf/releases

 

 

 

 

Kategorien: Software

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.