#1

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.
Reply
#2

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
Reply
#3

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?
Reply
#4

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

Thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)