20.04.2012, 23:53
(
Последний раз редактировалось ReneG; 12.02.2013 в 22:13.
)
How to: Implement Roleplay Chats
Getting Started
- This tutorial will show you step by step on how to implement roleplay chats for your server.
- This tutorial also assumes you have general knowledge on how to make commands using zcmd and sscanf 2.6.
- A couple of things you need.
- Basic understanding in PAWN for sa-mp scripting.
- foreach include by ******: https://sampforum.blast.hk/showthread.php?tid=92679
- sscanf 2.6 plugin by ******: https://sampforum.blast.hk/showthread.php?tid=120356
- zcmd include by Zeex: https://sampforum.blast.hk/showthread.php?tid=91354
Before we begin, we will also need a couple of useful functions that will make scripting tons easier.
Note: Stock and public functions are alike in many ways, however the main difference between the two is that stocks may not be called via SetTimer, CallRemoteFunction, etc...
- Removing "_" from a name.
In order to do so, we need a string replace function.
pawn Код:stock strreplace(string[], find, replace)
{
for(new i=0; string[i]; i++)
{
if(string[i] == find)
{
string[i] = replace;
}
}
}
pawn Код:new name[24];
GetPlayerName(playerid, name, sizeof(name));
strreplace(name, '_', ' ');
pawn Код:new
name[24];
pawn Код:GetPlayerName(playerid, name, sizeof(name));
pawn Код:strreplace(name, '_', ' ');
So now that we have that, we will make another stock called GetName that will be used later in the tutoral.
pawn Код:stock GetName(playerid)
{
new
name[24];
GetPlayerName(playerid, name, sizeof(name));
strreplace(name, '_', ' ');
return name;
} - ProxDetector
For those who don't know, the famous ProxDetector function was introduced in The Godfather Roleplay. Every time a player would type something into the main chat, it would send the message to the nearby players only, and according to their distance from a player, the message would appear darker or lighter. For instance, if I was 30 feet from a player and they typed in the normal chat, the message would come to me dark. If I were to move closer, the message would become lighter. This function was responsible for making the server chat 'realistic' by sending the message to only the players nearby.
Note: Using the easier-to-understand version, the changing in color in light of the player distance will not be featured.
pawn Код:stock ProxDetector(Float:radi, playerid, string[],color)
{
new Float:x,Float:y,Float:z;
GetPlayerPos(playerid,x,y,z);
foreach(Player,i)
{
if(IsPlayerInRangeOfPoint(i,radi,x,y,z))
{
SendClientMessage(i,color,string);
}
}
}
pawn Код:new Float:x,Float:y,Float:z;
pawn Код:GetPlayerPos(playerid, x, y, z);
pawn Код:foreach(Player, i)
pawn Код:if(IsPlayerInRangeOfPoint(i,radi,x,y,z))
pawn Код:SendClientMessage(i,color,string);
Now that we have all of our stocks, we will make what's called the 'local' chat. To do this, we will be using the callback called OnPlayerText since it is called every time a person types into the main chat. So go to your script and find "OnPlayerText".
- New Message
In order to make the chat realistic, we must format the text so that it is in 'roleplay' format, so we must define a new string that will be the message with a cell size of 128.pawn Код:new
message[128]; - Formatting the Message
Now that we have defined the the string that will hold our new message, we will use the function format to insert variables like name and text in it.
pawn Код:format(message, sizeof(message), "%s says: %s", GetName(playerid), text);
Код:NameHere says: Text here.
- Sending Message to Players In Range
Now that we have inserted the name and the text, we will now need to send the message to every player in range. To do this, we will now put the ProxDetector function into use!
pawn Код:ProxDetector(30.0, playerid, message, -1); - Final Step
Up to this point, we have made a new string, inserted the text and name of the player (with underscore removed), and have successfully sent the message to all the players nearby. Everything is working, but one thing. If you have compiled your script up to this point, and go in-game, you will not only see our formatted message, but you will also see the 'traditional' SAMP chat (message sent to all). To get rid of the default SAMP chat, go to the end of your OnPlayerText callback, and instead ofpawn Код:return 1;pawn Код:return 0; - Conclusion
So after everything we have added under OnPlayerText, this is what is should look like.
pawn Код:public OnPlayerText(playerid, text[])
{
new
message[128];
format(message, sizeof(message), "%s says: %s", GetName(playerid), text);
ProxDetector(30.0, playerid, message, -1);
return 0;
}
Before we start, we will need to define the RP purple color on the top of your script.
So now we have the local chat, but what about the other roleplay chat commands? We will now use the ProxDetector function in a command!pawn Код:#define COLOR_PURPLE 0xC2A2DAAA
- /me
We will be using zcmd, and sscanf 2.6 to create our /me command.
pawn Код:new
string[128],
action[100];
pawn Код:if(sscanf(params, "s[100]", action))
{
SendClientMessage(playerid, -1, "USAGE: /me [action]");
return 1;
}
pawn Код:else
{
format(string, sizeof(string), "* %s %s", GetName(playerid), action);
ProxDetector(30, playerid, string, COLOR_PURPLE);
}
So now we have a /me command and it should look like this. You should now know how to make a /do command!
pawn Код:CMD:me(playerid, params[])
{
new
string[128],
action[100];
if(sscanf(params, "s[100]", action))
{
SendClientMessage(playerid, -1, "USAGE: /me [action]");
return 1;
}
else
{
format(string, sizeof(string), "* %s %s", GetName(playerid), action);
ProxDetector(30, playerid, string, COLOR_PURPLE);
}
return 1;
} - /shout and /s command
To create a shout command, you would use the same method used above, but what if you want the shout command to be both /shout or /s? I'll show you how.
pawn Код:CMD:shout(playerid, params[])
{
new
string[128],
shout[100];
if(sscanf(params, "s[100]", shout))
{
SendClientMessage(playerid, -1, "USAGE: /(s)hout [message]");
return 1;
}
else
{
format(string, sizeof(string), "%s shouts: %s!",GetName(playerid),shout);
ProxDetector(50.0, playerid, string, -1);
}
return 1;
}
pawn Код:CMD:s(playerid, params[]) return cmd_shout(playerid, params);
- ****** for his foreach include, and his sscanf plugin.
- Zeex for his zcmd include.
- Original creator of the ProxDetector function (used as a reference).
- VincentDunn for writing the tutorial.