07.07.2011, 10:28
Making a CB Radio System
Just recently, I've had several "new players" requesting help on creating CB radio systems (as I'm sure other servers have too). I actually got offered an administration position to help someone on this.Aimed mainly at Trucking/Logistical Servers
If I'm honest, it is a pretty easy thing. When I first started scripting for SAMP, this was one of my first accomplishments.
The Variables
You only need one, global, array. This will store the players virtual "CB Channel".
Call it something sensible, obviously.
For example:
pawn Code:
new pChannel[MAX_PLAYERS]; //MAX_PLAYERS, to create the array with that size (default to 500)
You will want to reset the CB Channel when a player disconnects. You'll be using the OnPlayerDisconnect callback to do this.
Set it to "0". So people later connecting with the same player id, won't see the CB messages that are being sent.
Stick this, in your "OnPlayerDisconnect" callback. (If you don't have this callback, add it in)
pawn Code:
pChannel[playerid] = 0;
Okay, I'll be using DCMD to create the commands, although I would recommend using something like ZCMD, because it is faster.
The /cbchannel (or /setchannel - whatever you want to call it) is a pretty easy thing to make! Just, when a player sends that command, check to see if there "parameters" are correct (between or equal to 1 and 40) and that it is not "" (blank).
I will show you this, however, don't copy it. Understand what it does, this way you won't have to figure out the /cb command aswell.
pawn Code:
dcmd_cbchannel(playerid, params[])
{
if(!strlen(params) && /*!IsNumeric(params)*/) return SendClientMessage(playerid, COLOUR_RED, "No parameters (Usage: /cbchannel [1-40]");
//If the length of params is 0 (non existent) - it returns an error message
//I have commeneted our "IsNumeric" because I am not supplying it with the tutorial (because I can't find it!)
if(strval(params) < 1 || strval(params) > 40) return SendClientMessage(playerid, COLOUR_RED, "Only channels 1 upto 40 exist");
//If the integer value of the parameters string is lower than 1 or bigger than 40 then it returns another error message
pChannel[playerid] = strval(params);
//Set the players channel
//You may also want to add a "Your channel is now %i" message.
return 1;
//Return 1 (command complete)
}
pawn Code:
dcmd_cb(playerid, params[])
{
if(!strlen(params)) return SendClientMessage(playerid, COLOUR_RED, "No parameters (Usage: /cb [message]");
//Error message if the player did not specify any parameters
static str[128];
//Why static? It stops it being created more than once (in fact if you decompiled an AMX script you would find the "static" variable has become GLOBAL)
format(str, sizeof(str), "[CB]%s: %s", ReturnName(playerid), params);
//Format the output message (str)
//ReturnName, is a function to return a players name - I will include it toward the end of this post
//You could use colour embedding here!
for(new i; i < GetMaxPlayers(); i++)
{
//Start the loop (GetMaxPlayers will return the MAXIMUM player slots on your server - you could use Foreach here instead)
if(!IsPlayerConnected(i)) continue;
//If the player is not connected, skip to the next one
if(pChannel[i] == pChannel[playerid])
{
//If the player channel matches the other players channel
SendClientMessage(i, COLOUR_YELLOW, str);
}
}
return 1;
//End the command (return 1)
}
To add a command into OPCT using DCMD, you must follow this format:
pawn Code:
dcmd(COMMAND_NAME, COMMAND_NAME_LENGTH, cmdtext);
pawn Code:
dcmd(cbchannel, 9, cmdtext);
dcmd(cb, 2, cmdtext);
//Of course, if you have renamed any commands, change them here too!
Some people will want the players channel set when they spawn. It's pretty simple to do.
In "OnPlayerSpawn", add this:
pawn Code:
pChannel[playerid] = YOUR WANTED CHANNEL!;
pawn Code:
if(GetPlayerTeam(playerid) == ...) //If statement
switch(GetPlayerTeam(playerid))
{
case TEAM..: { }
}//Switch statement
( (GetPlayerTeam(playerid) == TEAM_...) ? pChannel[playerid]=1 : ... ) //Ternary / Triadic operator
Anything else?
No, you're good to go!
One thing you will need to do though... I have used colour defines in my examples, you will need to change them to the hexadecimal values or make them correspond with your colour defines.
I also said I would give the "ReturnName" function and the DCMD definition:
pawn Code:
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
stock ReturnName(id)
{
static name[24];
GetPlayerName(id, name, 24);
return name;
}
Final Word
I hope that helped. If anyone asks how to create a CB radio system, I will forward them here.
Here is a filterscript I made by following my own tutorial (lol): https://sampforum.blast.hk/showthread.php?pid=1280326#pid1280326
Happy Scripting!