public OnPlayerCommandText(playerid, cmdtext[])
{/*declaring the command*/
dcmd(pcarcolor, 9, cmdtext); // the number 9 is the number of characters in "pcarcolor"
return 0;
}
dcmd_pcarcolor(playerid, params[])
{
new col1, col2;
/*params is the string, which you wrote after "/pcarcolor". Lets say you wrote "/pcarcolor 0 1"*/
/*params is then " 0 1"*/
/*the first parameter in sscanf is the string which we will be looking at it. It will be params*/
/*The second parameter is what we will be looking for. 'd' means we will be looking for an integer*/
/*Check here for other stuff you can search with sscanf*/
/*[url]https://sampwiki.blast.hk/wiki/Fast_Commands#Data_types*/[/url]
/*There are 2 'd' because we will be searching for 2 integers. If you would be looking for an integer and a string, it would be "ds"*/
/*The other parameters are the places where the integers will be saved. The first integer ('0', if you typed "/pcarcolor 0 1") will be saved in 'col1' and the second (1) will be saved in 'col2'*/
if (sscanf(params, "dd", col1, col2))
{
/*sscanf returns TRUE if the user imputed the text WRONG (eg, did /pcarcolor lol 2),
and returns FALSE if the user IMPUTED THE TEXT CORRECTLY (eg, did /pcarcolor 1 2)*/
SendClientMessage(playerid, COLOR_RED, "USAGE: /pcarcolor [color1] [color2]");
return 1; /*he imputed it wrong, so lets end the code here*/
}
/*he imputed it wright, so lets continue*/
/*lets change the colors*/
ChangeVehicleColor(GetPlayerVehicleID(playerid),col1, col2); //col1 contains 0, col2 contains 1
return 1;
|
Originally Posted by Outbreak
I've already read through that...
It says that "u" would work for a players id or name... it doesnt, i had to use "i" or "d" instead... It mentioned optional strings, but nothing about optional integer. strtok maybe slower, by .000000000000000000000000001 of a second, but seems way more flexable for things like this... Where as sscanf seems alright for more basic commands. |
public OnPlayerCommandText(playerid, cmdtext[])
{
new string[128];
new cmd[128];
new tmp[128];
new giveplayerid, idx;
cmd = strtok(cmdtext, idx);
if(strcmp(cmd, "/announce", true) == 0)
{
if(IsPlayerAdmin(playerid))
{
new length = strlen(cmdtext);
while ((idx < length) && (cmdtext[idx] <= ' '))
{
idx++;
}
new offset = idx;
new result[64];
while ((idx < length) && ((idx - offset) < (sizeof(result) - 1)))
{
result[idx - offset] = cmdtext[idx];
idx++;
}
result[idx - offset] = EOS;
if(!strlen(result))
{
SendClientMessage(playerid, COLOR_GREY, "USAGE: /announce [text]");
return 1;
}
GameTextForAll(string, 5000, 4);
return 1;
}
else
{
SendClientMessage(playerid, COLOR_GREY, "You are not an admin!");
}
return 1;
}
return 0;
}
strtok(const string[], &index)
{
new length = strlen(string);
while ((index < length) && (string[index] <= ' '))
{
index++;
}
new offset = index;
new result[20];
while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = string[index];
index++;
}
result[index - offset] = EOS;
return result;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
dcmd(announce, 8, cmdtext);
return 0;
}
dcmd_announce(playerid, params[])
{
new text[128];
if(sscanf(params, "s", text))
{
SendClientMessage(playerid, COLOR_GREY, "USAGE: /announce [text]");
return 1;
}
if(IsPlayerAdmin(playerid))
{
GameTextForAll(text, 5000, 4);
}
else
{
SendClientMessage(playerid, COLOR_GREY, "You are not an admin!");
}
return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
dcmd(announce, 8, cmdtext);
return 0;
}
dcmd_announce(playerid, params[])
{
if(params[0]==0) {SendClientMessage(playerid, COLOR_GREY, "USAGE: /announce [text]"); return 1;}
if(IsPlayerAdmin(playerid)) GameTextForAll(params, 5000, 4);
else SendClientMessage(playerid, COLOR_GREY, "You are not an admin!");
return 1;
}
|
Originally Posted by Outbreak
I have that, for changing my own vehicle colour.
I need to use /pcarcolor for changing a players car colour.. /pcarcolor [playerid] [col1] [col2] but if col2 isnt specified then col2 = col1 I dont know how to make col2 an optional integer.. strtok seems a lot more flexable.. |
dcmd_pcarcolor(palyerid,params[])
{
new id,pName[MAX_PLAYER_NAME],idName[MAX_PLAYER_NAME],string[256],string2[256],color1,color2;
if(sscanf(params,"uzz",id,color1,color2)) return SendClientMessage(playerid,0xA9A9A9AA,".: Usage: /pcarcolor [playerid/partofname] [color1] [color2] :.");
if(id == INVALID_PLAYER_ID) return SendClientMessage(playerid,0xAA3333AA,".: Info: Player not found :.");
GetPlayerName(playerid,pName,256);
GetPlayerName(id,idName,256);
new vehicle = GetPlayerVehicleID(playerid);
ChangeVehicleColor(vehicle,color1,color2);
format(string,256,".: Info: %s has changed your car color :.",pName);
format(string2,256,".: Info: You've changed %s's car color :.",idName);
SendClientMessage(playerid,0x9ACD32AA,string2);
SendClientMessage(id,0x9ACD32AA,string);
return 1;
}