Tutorial of strtok

From SA-MP Wiki

Jump to: navigation, search

Image:50px-Ambox_outdated_serious.png This article is outdated. It may use methods or functions which no longer exist or which are deemed obsolete by the community. Caution is advised. There are many new methods for completing such tasks. For example: sscanf


strtok tutorial

strtok is a function made to control space between the command and the value following it. Always thought that doing those commands that have a space and then something is hard? This guide shall help you.

public OnPlayerCommandText(playerid, cmdtext[])
{
    new tmp[128], string[128], cmd[128], idx, giveplayerid, sendername, giveplayer;
    cmd = strtok(cmdtext, idx);
    GetPlayerName(playerid, sendername, sizeof(sendername));
    if(strcmp(cmd, "/akill", true) == 0)
    {
        if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0x919191FF, "You must be admin to admin-kill someone.");
        tmp = strtok(cmdtext, idx);
        if(!strlen(tmp)) return SendClientMessage(playerid, 0x919191FF, "Usage: \"/akill [ID]\"");
        giveplayerid = strval(tmp);
        GetPlayerName(giveplayerid, giveplayer, sizeof(giveplayer));
        if(!IsPlayerConnected(giveplayerid))SendClientMessage(playerid, 0x919191FF, "Player not found.");
        SetPlayerHealth(giveplayerid, 0); 
        format(string, sizeof(string), "Admin %s[%i] killed you!", sendername, playerid);
        SendClientMessage(giveplayerid, 0xE21D2CFF, string);
        format(string, sizeof(string), "Killed %s[%i]!", giveplayer, giveplayerid);
        SendClientMessage(playerid, 0xE21D2CFF, string);
        return 1;
   }
}

Explanation

giveplayerid = strval(tmp);

That means that 'giveplayerid' is assigned to tmp's value.

if(!strlen(tmp)) return SendClientMessage();

When no 'giveplayerid' is inserted (aka. when you write /akill instead of /akill ID)

GetPlayerName(playerid, sendername, sizeof(sendername));
GetPlayerName(giveplayerid, giveplayer, sizeof(giveplayer));

To form a correct message (e.g: "Kyeman(ID 4) killed !damo!spiderman(ID 2)") we'll need to get the name into variables. PS: Always get the 'giveplayerid' name AFTER you've assigned it to 'tmp'.

Not good enough? Watch this:

if(strcmp(cmd, "/ban", true) == 0)
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0x919191FF, "BE ADMIN!"); // when player isn't admin, don't let to use command
    tmp = strtok(cmdtext, idx); // We assign that we use strtok in this command to 'tmp'
    if(!strlen(tmp)) return SendClientMessage(playerid, 0x919191FF, "USAGE: /ban [id]"); // If no ID is given
    giveplayerid = strval(tmp); // Now we assign the 'giveplayerid'
    if(!IsPlayerConnected(giveplayerid)) return SendClientMessage(playerid, 0x919191FF, "That player isn't connected!"); // when 'giveplayerid' isn't online
    Ban(giveplayerid);
    return 1;
}

Explanation

if(!IsPlayerAdmin(playerid))

If the player isn't admin, return the message and stop processing

tmp = strtok(cmdtext, idx);

We assign the usage of strtok to 'tmp'

if(!strlen(tmp))

If any ID isn't included in command, return the message and stop processing

giveplayerid = strval(tmp);

We assign 'giveplayerid' to tmp

if(!IsPlayerConnected(giveplayerid))

If 'giveplayerid' isn't connected, return message and stop processing

Ban(giveplayerid);

Finally the effect of command, everyone understands it.

Personal tools
Navigation
Toolbox
In other languages