The purpose of IsPlayerConnected?
#1

I don't know why is this function used in most of the commands and other callbacks such as OnPlayerKeyStateChange. Obviously, the player wouldn't be able to use that command if he's not actually connected to the server. Please if someone could explain to me it's purpose and function. And also if it's really important to add in commands and what will happen if I don't. Thanks.
Reply
#2

Same for me, I have never understood need of it.
Reply
#3

Alright Your thinking that the 'IsPlayerConnected' function is only for executing commands. But it's also used for a majority of things. Example, when executing a command which renders another 'integer' (for the player id as an example), this function will check if the player is connected. If he is not, you would most likely insert a return function with a Message.
Reply
#4

I think you didn't read my post properly. I said, that I've seen it there in several callbacks, but didn't understood what it did.
Let's assume this is the command, which is actually taken out from GF:
pawn Код:
if(strcmp(cmd, "/charity", true) == 0)
    {
        if(IsPlayerConnected(playerid))
        {
            tmp = strtok(cmdtext, idx);
            if(!strlen(tmp))
            {
                SendClientMessage(playerid, COLOR_RED, "(( USAGE: /charity [amount] ))");
                return 1;
            }//....
It's pretty much obvious that the player entering the command /charity is connected, that is why he is able to enter it. Please correct me if I'm wrong.
Reply
#5

In earlier versions of SA-MP, there was an alleged flaw that you could execute commands if you weren't fully connected to the server, OnPlayerCommandText didn't require a connected player check - but you could execute it remotely. People never got over the 'trend' of doing so.
Reply
#6

Well that explains it for the OnPlayerCommand callback as you said, but I can see it in numerous callbacks. Anyway, it's perfectly fine if I don't use IsPlayerConnected function anywhere right?
Reply
#7

Alright thanks, just some last simple questions:
-Will it be okay if I don't use IsPlayerConnected in OnPlayerCommandText, since Calgoone said it was only needed in older versions of samp?
-I should only be using IsPlayerConnected on another subject than playerid right?
-It's not neccessary to use IsPlayerConnected on OnPlayerKeyStateChange to detect if the one who presses the newkeys is connected right, since he will be the playerid.
Reply
#8

Quote:
Originally Posted by ||123||
Посмотреть сообщение
Alright thanks, just some last simple questions:
-Will it be okay if I don't use IsPlayerConnected in OnPlayerCommandText, since Calgoone said it was only needed in older versions of samp?
-I should only be using IsPlayerConnected on another subject than playerid right?
-It's not neccessary to use IsPlayerConnected on OnPlayerKeyStateChange to detect if the one who presses the newkeys is connected right, since he will be the playerid.
For the first question, if your planning you use the original 'strcmp' method to call your commands, then you have learnt nothing. Those older methods which were included in gamemodes such as the 'Godfather' are obsolete. Command processors such as ZCMD and YCMD can check, call and execute the command faster than using the 'original' method.

Most likely for 'arbitrary input' as stated by ******, yes.
Reply
#9

Are there any disadvantage of ZCMD? And which one is better ZCMD or YCMD or CMD or W/e..?
Reply
#10

Personally, I'd recommend ZCMD as this is what I am used to (though I have not even attempted to use YCMD), yet people 'claim' that Zcmd is the 'fastest' processor out there, yet I haven't really seen any tests in comparison of the two.

You can turn a Command such as this:

pawn Код:
if(strcmp(cmd, "/freeze", true) == 0)
    {
        if(IsPlayerConnected(playerid))
        {
            tmp = strtok(cmdtext, idx);
            if(!strlen(tmp))
            {
                SendClientMessage(playerid, COLOR_GRAD2, "USAGE: /freeze [playerid/PartOfName]");
                return 1;
            }
            new playa;
            playa = ReturnUser(tmp);
            if(PlayerInfo[playa][pAdmin] > 0)
            {
                SendClientMessage(playerid, COLOR_GRAD2, "Admins can not be frozen");
                return 1;
            }
            if (PlayerInfo[playerid][pAdmin] >= 1)
            {
                if(IsPlayerConnected(playa))
                {
                    if(playa != INVALID_PLAYER_ID)
                    {
                        GetPlayerName(playa, giveplayer, sizeof(giveplayer));
                        GetPlayerName(playerid, sendername, sizeof(sendername));
                        TogglePlayerControllable(playa, 0);
                        format(string, sizeof(string), "AdmCmd: %s Freezes %s",sendername,  giveplayer);
                        printf("%s",string);
                        format(string, sizeof(string), "AdmCmd: %s was Frozen by %s",giveplayer ,sendername);
                    }
                }
            }
            else
            {
                SendClientMessage(playerid, COLOR_GRAD1, "   you are not authorized to use that command!");
            }
        }
        return 1;
    }
To

pawn Код:
CMD:freeze(playerid, params[]) //freeze command
{
    if(PlayerInfo[playerid][pAdmin] >= 2)
    {
        new PID, pName[MAX_PLAYER_NAME], Sender[MAX_PLAYER_NAME];
        if(sscanf(params, "u", PID)) return SendClientMessage(playerid, COLOR_GREY, "USAGE: /freeze [playerid]");
        if(!IsPlayerConnected(PID)) return SendClientMessage(playerid, COLOR_GREY, "Player is not connected!");
        new Str[128];
        GetPlayerRPName(PID, pName, sizeof(pName));
        GetPlayerRPName(playerid, Sender, sizeof(Sender));
        format(Str, sizeof(Str), "AdmCmd: %s has been frozen by %s.", pName, Sender);
        SendClientMessageToAll(COLOR_YELLOW, Str);
        TogglePlayerControllable(PID,0);
    }
    else
    {
        SendClientMessage(playerid, COLOR_GREY, "You are not authorized to use that command!");
    }
    return 1;
}
I think you can observe the amount of lines being used in each method.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)