Think about the steps you need to take to achieve this:
Step 1: Check whether the player has typed a message with a ; in front of it, ie, check the first character of "text" under the OnPlayerText callback:
pawn Код:
public OnPlayerText(playerid, text[])
{
if(text[0] == ';')
{
// player has typed a message starting with ;
}
return 1;
}
Step 2: Think about what variables you will need for this. You want to know the
player's name, the
player's team name, and you will need the
original message:
pawn Код:
public OnPlayerText(playerid, text[])
{
if(text[0] == ';')
{
// player has typed a message starting with ;
new msg[128], pname[MAX_PLAYER_NAME], teamname[MAX_TEAM_NAME];
}
return 1;
}
Step 3: Assign the corresponding data to these variables (using the help of a new method - GetTeamName. If you already have your own method like this, just use that one):
pawn Код:
#define MAX_TEAM_NAME 32 // defines the max length of a team name as 32 characters
stock GetTeamName(playerid, tname[], len) // Returns a player's team name depending on their team id (gTeam[playerid])
{
switch(gTeam[playerid]) // Checks the player's team id
{
case 0: format(tname, len, "RedGang"); // teamid 0 has the teamname RedGang
case 1: format(tname, len, "BlueGang"); // teamid 1 has the teamname BlueGang
case 2: format(tname, len, "GreenGang"); // teamid 2 has the teamname GreenGang
case 3: format(tname, len, "YellowGang"); // teamid 3 has the teamname YellowGang
default: printf("Invalid team name"); // this will only print if there is an error
}
return 1;
}
public OnPlayerText(playerid, text[])
{
if(text[0] == ';')
{
// player has typed a message starting with ;
new msg[128], pname[MAX_PLAYER_NAME], teamname[MAX_TEAM_NAME];
GetPlayerName(playerid, pname, sizeof(pname)); // gets the player's name
GetTeamName(playerid, teamname, sizeof(teamname)); // gets the player's teamname
strmid(msg, text, 1, strlen(text), sizeof(msg)); // saves what the player has typed without the ; at the start
format(msg, sizeof(msg), "[%s]%s: %s", teamname, pname, msg); // puts all the info together to create the message to send to players on the same team
}
return 1;
}
Step 4: Cycle through all players, and print the message to those who are on the same team:
pawn Код:
#define MAX_TEAM_NAME 32
stock GetTeamName(playerid, tname[], len)
{
switch(gTeam[playerid])
{
case 0: format(tname, len, "RedGang");
case 1: format(tname, len, "BlueGang");
case 2: format(tname, len, "GreenGang");
case 3: format(tname, len, "YellowGang");
default: printf("Invalid team name");
}
return 1;
}
public OnPlayerText(playerid, text[])
{
if(text[0] == ';')
{
// player has typed a message starting with ;
new msg[128], pname[MAX_PLAYER_NAME], teamname[MAX_TEAM_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
GetTeamName(playerid, teamname, sizeof(teamname));
strmid(msg, text, 1, strlen(text), sizeof(msg));
format(msg, sizeof(msg), "[%s]%s: %s", teamname, pname, msg);
for(new i=0; i<MAX_PLAYERS; i++) // loops through all players
{
if(gTeam[i] == gTeam[playerid]) // checks to see if the current player is on the same team as the player who typed the message
{
SendClientMessage(i, -1, msg); // prints the message for the player on the same team
}
}
return 0; // return 0 so that the original message with ; in front of it isn't shown to everybody
}
return 1;
}
And voila. It's really not that difficult when you break it down!