28.06.2009, 05:22
I decided to make a new topic for these functions, because I think that they are very useful for all the begginers out there, first one sort of replaces strtok, although it has a different usage. Another two are just useful for lazy people like me.
The function returns a word or some other properly separated content from a string.
This is basicly how you use it:
GetValue(string[],number,bool:tillend=false,separator[]=" ")
Parameters here read: string, number, tillend and separator, that's kind of intuitive, but just in case you don't get something, I'll explain:
string is the string in which we will look for the word or other content.
number defines the number of word counting left to righ (1st word is 0, second is 1 and so on).
tillend is a boolean telling the function whether to look for another seporator when it goes to a word ir just get the remaining string.
the separator is what separates your words or content (so we could count up to the word you want), by default its a simple space.
now let's go to another one:
A simple command to send player formatted text, without the hassle of formatting a string everytime and only then sending it.
A sample usage would be:
And the last one:
Nothing really special, just returns a player name. Useful for saving time, an example usage can be seen below.
Thanks for your time, and I hope some of my work is useful for you. Please comment if you like it and excuse my poor english. Thanks.
Cheers,
Garf.
GetValue
pawn Код:
stock GetValue(string[],number,bool:tillend=false,separator[]=" ")
{
new index=0,oldidx=0,result[128]="-1";
for(new word=0; word<=number; word++)
{
index = strfind(string,separator,true,index);
if(index == -1)
{
if(word == number)
{
strmid(result,string,oldidx,128,128);
}
break;
}
if(!string[index]) break;
if(word==number)
{
if(tillend)
strmid(result,string,oldidx,128,128);
else
strmid(result,string,oldidx,index,128);
break;
}
index++;
oldidx=index;
}
return result;
}
This is basicly how you use it:
GetValue(string[],number,bool:tillend=false,separator[]=" ")
Parameters here read: string, number, tillend and separator, that's kind of intuitive, but just in case you don't get something, I'll explain:
string is the string in which we will look for the word or other content.
number defines the number of word counting left to righ (1st word is 0, second is 1 and so on).
tillend is a boolean telling the function whether to look for another seporator when it goes to a word ir just get the remaining string.
the separator is what separates your words or content (so we could count up to the word you want), by default its a simple space.
now let's go to another one:
SendPlayerText(playerid;const string[];parameter1,parameter2...)
pawn Код:
new msg[128];
#define COLOR SOME_COLOR
#define SendPlayerText(%1;%2;%3); format(msg,sizeof(msg),%2,%3); SendClientMessage(%1,COLOR,msg);
A sample usage would be:
pawn Код:
new number=2,another_number=3;
SendPlayerText(playerid;"* Number %i plus number %i id %i"; number,another_number,number+another_number);
PlayerName(playerid)
pawn Код:
stock PlayerName(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
return name;
}
Let's try them out!
A givecash command is a lot more simple with these functions:pawn Код:
if(!strcmp(cmdtext,"/givecash",true,9))
{
// Let's get the arguments
new player = strval(GetValue(cmdtext,1)), money = strval(GetValue(cmdtext,2));
// Some errors may occur
if(player == -1 || money == -1) return SendClientMessage(playerid,SOME_COLOR,"* Usage: /givecash [playerid] [cash]");
if(GetPlayerMoney(playerid)<money) return SendClientMessage(playerid,SOME_COLOR,"* You don't have that much.");
if(!IsPlayerConnected(player)) return SendClientMessage(playerid,SOME_COLOR,"* That player is not online.");
// The giving part
GivePlayerMoney(playerid,-money); GivePlayerMoney(player,money);
//And now the messages
SendPlayerText(player,"* %s has wired you $%i.";PlayerName(playerid),money);
SendPlayerText(playerid,"* You have wired $%i to %s.";money,PlayerName(player));
return 1;
}
Cheers,
Garf.