/me command explanation
#1

Код:
stock SendRangedMessage(sourceid, color, message[], Float:range) {
	new Float:x, Float:y, Float: z; // we create 3 float variables to store the x, y, z coordinates in;
	GetPlayerPos(sourceid, x, y, z); // we use the 'GetPlayerPos' function to store the sourceid's coordinates (player who sends the ranged message) into x, y and z;
	for (new ii = 0; ii < MAX_PLAYERS; ii++) { // we loop through all connected players - could use foreach for faster processing
	    if(IsPlayerConnected(ii)) { // we check players availability (online)
	        if(GetPlayerVirtualWorld(sourceid) == GetPlayerVirtualWorld(ii)) { // we check if the virtual worlds of the players within the range match
				if(IsPlayerInRangeOfPoint(ii, range, x, y, z)) { // we check if the player you want to send the message to is in the given range (range = the furthest distance the player can be from the point to be in range. -> distance between the farthest distance point and sourceid coordinates is practically the range )
			    	SendClientMessage(ii, color, message); // if he meets all those "requirements", the message will be sent.
				}
			}
		}
	}
}

CMD:me(playerid, params[]) {
	new string[128]; // we create an array named string that has 128 cells. on each cell, a character will be placed. in empty cells, /0 (null) is located.
	if(isnull(params)) return SendClientMessage(playerid, -1, "Usage: /me [action]"); // if the params are null, a client message will be returned to the player that typed the command
	format(string, sizeof(string), "* %s %s", GetPlayerNameEx(playerid), params); // we format the message so it can include variables and other strings to it
	SendRangedMessage(playerid, -1, string, 5.0); // we call SendRangedMessage not native function.
	return 1;
}
This is a code I've made and I usually comment each line as I'm a newbie and I wanna understand everything. Are the explanations correct?
Reply
#2

Yes the explanations are correct.
Reply
#3

All right! +rep for the answer.
Reply
#4

Make short and descriptive comments. Scripts have to look clear!!
Reply
#5

This is wrong:
pawn Код:
for (new ii = 0; ii < MAX_PLAYERS; ii++) { // we loop through all connected players - could use foreach for faster processing
You aren't looping through connected players, you are just looping 500 times, and then checks and continues if there's a playerid with that number (0-500)
As seen in the a_samp.inc:
pawn Код:
#define MAX_PLAYERS  (500)
Using MAX_PLAYERS is just the same as using 500.
That's what the foreach loop is good for, as it only loops through connected players.
Although if you want, you can re-define MAX_PLAYERS in your script, by doing this:
pawn Код:
#undefine MAX_PLAYERS
#define MAX_PLAYERS 200
Will define MAX_PLAYERS as 200 instead, which is a good method if your server doesn't have 500 slots.
Reply
#6

I have a .pwn file which is clean (without any comments), whereas the other does have comments. So, if I don't understand anything, I'll open the second file right away and check it out.
Reply
#7

Or, just simply use foreach.
Reply
#8

Thanks for the advice! I'll be using foreach from now on.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)