How to code a simple PM system, using sscanf2
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.
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.
That’s almost everything what we need.
Now we have to create the commands itself, simplest way is to use the strcmp function.
So our new public will loke like this.
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.
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.
After we have declared the strings, we need to check if all information is given by the player’s input.
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.
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.
Everything was executed successfully, so we add our return 1; to conclude.
So our command now looks like that, including the public.
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.