[FUNCTIONS] A few, but really useful -
Garf. - 28.06.2009
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.
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;
}
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:
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 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:
pawn Код:
new number=2,another_number=3;
SendPlayerText(playerid;"* Number %i plus number %i id %i"; number,another_number,number+another_number);
And the last one:
PlayerName(playerid)
pawn Код:
stock PlayerName(playerid)
{
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid,name,sizeof(name));
return name;
}
Nothing really special, just returns a player name. Useful for saving time, an example usage can be seen below.
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;
}
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.
Re: [FUNCTIONS] A few, but really useful -
Garf. - 28.06.2009
Also, here's some more examples for GetValue:
pawn Код:
print(GetValue("Hello, my name is John",0)); // Will print "Hello,".
print(GetValue("Hello, my name is John",1)); // Will print "my".
print(GetValue("Hello, my name is John",2)); // Will print "name".
print(GetValue("Hello, my name is John",3)); // Will print "is".
print(GetValue("Hello, my name is John",4)); // Will print "John".
print(GetValue("Hello, my name is John",5)); // Will print "-1".
print(GetValue("Hello, my name is John",3,true)); // Will print "is John".
print(GetValue("Hello, my name is John",1,false,",")); // Will print " my name is John".
Re: [FUNCTIONS] A few, but really useful -
[HiC]TheKiller - 28.06.2009
Please post them here:
http://forum.sa-mp.com/index.php?topic=61574.0
It saves the time of the topic getting deleted ETC.
Re: [FUNCTIONS] A few, but really useful -
Ignas1337 - 28.06.2009
uhh, givecash comand sucks, one can give an amount of -10000000 which woukd make money for him, couldn't he?
Re: [FUNCTIONS] A few, but really useful -
Garf. - 28.06.2009
It's just for testing anyways

You can add a check to see if player is giving more than 0.
Re: [FUNCTIONS] A few, but really useful -
godknightx - 26.09.2009
But if i write this as a command, its work with GetValue? (With more than 1 spaces)
Re: [FUNCTIONS] A few, but really useful -
MenaceX^ - 26.09.2009
Nice, the GetValue is really cool
Re: [FUNCTIONS] A few, but really useful -
XCultz - 26.09.2009
cool
Re: [FUNCTIONS] A few, but really useful -
nuriel6633 - 26.09.2009
Nice,Very usefull
Im gonna use it [ =