SA-MP Forums Archive
Doubt - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Doubt (/showthread.php?tid=665168)



Doubt - SymonClash - 24.03.2019

Ok so, i have almost 50 admin commands and i'm planning to add more. Since most of them (i'd say 99%), have some basilar checks, this to be clear:

pawn Code:
if(!IsPlayerConnected(id)) return SendClientMessage(playerid, -1, "Player is not connected.");
if(id == playerid) returnSendClientMessage(playerid, -1, "You can't use this command on yourself.");
if(id == INVALID_PLAYER_ID) return SendClientMessage(playerid, -1, "Invalid playername/id.");
I was thinking, it's possible to create a function that has this checks and return error messages in case?

I mean something like: BasicPlayerChecks(id);

Then in the command, imagine i type an invalid playername/id, it will return: "Invalid playername/id" calling the id == INVALID_PLAYER_ID check.

How? Hope i've been clear.


Re: Doubt - bgedition - 24.03.2019

It is possible for sure.

This should work:
pawn Code:
BasicPlayerChecks(playerid, targetid) {
    if(!IsPlayerConnected(targetid)) return 1;
    if(targetid == playerid) return 2;
    if(targetid == INVALID_PLAYER_ID) return 3;
   
    return false; //false and 0 are the same thing
}

SendErrorMessage(playerid, messageid, color = -1) {
    new message[64];
    switch(messageid) {
        case 1: strcat(message, "Player is not connected.");
        case 2: strcat(message, "You can't use this command on yourself.");
        case 3: strcat(message, "Invalid playername/id.");
    }
   
    return SendClientMessage(playerid, color, message);
}
Use it like so:
pawn Code:
if(BasicPlayerChecks(playerid, id)) return SendErrorMessage(playerid, BasicPlayerChecks(playerid, id));
or (don't call the function twice)
pawn Code:
new BPC = BasicPlayerChecks(playerid, id);
if(BPC) return SendErrorMessage(playerid, BPC);
Alternatively you can try SmartCMD by Yashas: https://sampforum.blast.hk/showthread.php?pid=3786819#pid3786819


Re: Doubt - SymonClash - 24.03.2019

Perfect, thank you. Gonna try it later.

EDIT: Works perfectly, thanks again. A question.

What's the difference between:

pawn Code:
if(!IsPlayerConnected(targetid))
And

pawn Code:
if(targetid == INVALID_PLAYER_ID)
Aren't the same?

First one checks if ID is connected, and second if is valid. (I mean, if an ID is connected, obviously is valid).

What's the difference? Should i use them both?


Re: Doubt - Hazon - 25.03.2019

It's basically 'the same'. No much difference between these two functions.


Re: Doubt - SymonClash - 25.03.2019

Thanks.