Fast command processor

Please read the DCMD tutorial before reading this.


SSCANF is made by Y_Less/Alex and is probably the easiest way to make commands that require parameters. You first need to be able to use DCMD, if you don't read the tutorial here, if you do then copy and paste the SSCANF function in to your gamemode somewhere (i put mine all my custom functions at the bottom).


Ok, i will be showing you how to make a kick command with the parameters, [playerid] [reason]. This is made very simple using DCMD and SSCANF together.

First we need to create the basic "skeleton" of the command, like this:

dcmd_kick(playerid, params[])
{

    return 1;
}

Next thing is to add variables for the playerid and reason, like this:

dcmd_kick(playerid, params[])
{
    new id, reason[64];
    return 1;
}

Once that is done we need to add the admin checks, i will use the sa-mp standard function, IsPlayerAdmin, it should now look something like this:

dcmd_kick(playerid, params[])
{
    new id, reason[64];
    if(IsPlayerAdmin(playerid))
    {

    }
    else
    {
        SendClientMessage(playerid, 0xC20000FF, "Admins only!");
    }
    return 1;
}

That makes sure that only admins can execute the /kick command or else it would be manic if everyone could! Next we can add the SSCANF part, like this:

dcmd_kick(playerid, params[])
{
    new id, reason[64];
    if(IsPlayerAdmin(playerid))
    {
        if (sscanf(params, "dz", id, reason)) SendClientMessage(playerid, COLOUR_YELLOW, "Usage: \"/kick [playerid] [reason]\"");
        else
        {

        }
    }
    else
    {
        SendClientMessage(playerid, COLOUR_RED, "Admins only!");
    }
    return 1;
}

A breakdown of this function is
if (sscanf(params, "dz", id, reason))

params
The parameters of the command
"dz"
The types the parameters are, in this case, integer and ban reason (basically string)
id
The first parameter
reason
The second parameter

The only problem is that an admin could ban any player that isnt even connected so we need to add this line:

else if (!IsPlayerConnected(id)) SendClientMessage(playerid, COLOUR_RED, "Invalid id");

Your command should now look something like this:

dcmd_kick(playerid, params[])
{
    new id, reason[64];
    if(IsPlayerAdmin(playerid))
    {
        if (sscanf(params, "dz", id, reason)) SendClientMessage(playerid, COLOUR_YELLOW, "Usage: \"/kick [playerid] [reason]\"");
        else if (!IsPlayerConnected(id)) SendClientMessage(playerid, COLOUR_RED, "Invalid id");
        else
        {

        }
    }
    else
    {
        SendClientMessage(playerid, COLOUR_RED, "Admins only!");
    }
    return 1;
}

That checks that the id typed in by the admin is actually connected to the server. That last thing we need to do isa actually kick them and tell them and the admin what has happened, i will use this:

SendClientMessage(id, COLOUR_RED, "You have been kicked from the server");
Kick(id);
SendClientMessage(playerid, COLOUR_YELLOW, "Player kicked");

Now my whole command looks like this

dcmd_kick(playerid, params[])
{
    new id, reason[64];
    if(IsPlayerAdmin(playerid))
    {
        if (sscanf(params, "dz", id, reason)) SendClientMessage(playerid, COLOUR_YELLOW, "Usage: \"/kick [playerid] [reason]\"");
        else if (!IsPlayerConnected(id)) SendClientMessage(playerid, COLOUR_RED, "Invalid id");
        else
        {
            SendClientMessage(id, COLOUR_RED, "You have been kicked from the server");
            Kick(id);
            SendClientMessage(playerid, COLOUR_YELLOW, "Player kicked");
        }
    }
    else
    {
        SendClientMessage(playerid, COLOUR_RED, "Admins only!");
    }
    return 1;
}

And thats it! Thats all you need for a basic kick command. You can add different parts to tell the player what admin kicked them etc but i just wanted to show you a simple one. If you still dont understand why DCMD and SSCANF are better than STRCMP then look for a STRCMP version of /kick and see the difference in length and how complex it is.


Back | Home