Useful Functions

Quote:
Originally Posted by ******
Посмотреть сообщение
RyDeR` - may I suggest you read the code optimisations topic? There is a section in there on looping through strings, and one on loops in general (i.e. don't keep calling strlen in the second section of the loop):

Good:

pawn Код:
replaceChar(string[128], findchar, replacechar)
{
    for(new i, j = strlen(string); i != j; ++i)
    {
        if(string[i] == findchar) string[i] = replacechar;
    }
    return string;
}
Better:

pawn Код:
replaceChar(string[128], findchar, replacechar)
{
    new i = 0;
    while (string[i])
    {
        if (string[i] == findchar) string[i] = replacechar;
        ++i;
    }
    // Don't really need this as it modifies the string passed to the function.
    return string;
}
Sure, I will read it. Nice way of doing this, thanks.
Will change my post.
Reply

with this ReplaceChar thing, why is the string limited at 128?
Reply

Quote:
Originally Posted by Daren_Jacobson
Посмотреть сообщение
with this ReplaceChar thing, why is the string limited at 128?
You can change it.. You know that right?
Reply

I think he was wondering why you just didn't use an undetermined array size in your function definition (i.e. string[]). I believe the answer for that is that Pawn doesn't like unknown sized arrays being returned.

However, if you don't want to limit the string length then don't return an array and take advantage that arrays are always passed by reference in Pawn i.e the assignment in the function happens outside it also. The function could be modified for those who don't want the limit, but it would have to be used like GetPlayerName which means it couldn't be used 'like' a read-only variable.

****** already touched on this, so just remove the 'return' statement and remove the 128 from 'string[128]'.
Reply

For removing the '_' thingies in name, like Firstname_Lastname becomes Firstname Lastname:

pawn Код:
stock RemUnderLine(name[MAX_PLAYER_NAME])
{
    new UL = strfind(name, "_", true);
    while(UL != -1)
    {
        name[UL] = ' ';
        UL = strfind(name, "_", true);
    }
    return name;
}
Reply

Hiddos nice one but here an easier and faster version:
pawn Код:
new
    pName[20]
;
GetPlayerName(playerid, pName, sizeof(pName));
pName[strfind(pName, "_")] = ' ';
// printf("%s", pName);
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Hiddos nice one but here an easier and faster version:
pawn Код:
new
    pName[20]
;
GetPlayerName(playerid, pName, sizeof(pName));
pName[strfind(pName, "_")] = ' ';
// printf("%s", pName);
And when there isn't any underlines in the player's name and strfind returns -1?

Or if there are more than 1 underline in the name?

Hiddos' code is fine, except that he's using a function which loops through the string in a loop, so the script might loop through the string many times when only 1 loop would be enough:

pawn Код:
func(name[])
{
    new i;
    while(name[i])
    {
        if(name[i] == '_')  name[i] = ' ';
        i++;
    }
    return 1;
}
Reply

Just some very simple functions I made.

SetMapName
Sets the server variable "mapname"
pawn Code:
forward SetMapName(mapName[]);

public SetMapName(mapName[])
{
    new string[30];
    format(string, sizeof(string), "mapname %s", mapName);
    SendRconCommand(string);
    return 1;
}
SetHostName
Sets the server variable "hostname"
pawn Code:
forward SetHostName(hostName[]);

public SetHostName(hostName[])
{
    new string[30];
    format(string, sizeof(string), "hostname %s", hostName);
    SendRconCommand(string);
    return 1;
}
SetServerURL
Sets the server variable "weburl"
pawn Code:
forward SetServerURL(url[]);

public SetServerURL(url[])
{
    new string[30];
    format(string, sizeof(string), "weburl %s", url);
    SendRconCommand(string);
    return 1;
}
ReturnPlayerName
Returns the player name without an underscore.
pawn Code:
stock ReturnPlayerName(playerid)
{
    var1="Invalid_Player_ID";
    if(IsPlayerConnected(playerid))
    {
        GetPlayerName(playerid, var1, sizeof(var1));
        for(new i = 0; i < sizeof(var1); i++)
        { if(var1[i] == \'_\') { var1[i] = \' \'; } }
    }
    return var1;
}


EDIT: Looks as if Hiddos beat me to it, but oh well, i\'ll keep this anyway.
Reply

SetMapIcon

Set its the map icon for everyone. I hope it works, it might not work, I mean im not very sure its untested

heres the code:

Код:
stock SetMapIcon(iconid, Float:x, Float:y, Float:z, markertype)
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
    		SetPlayerMapIcon(i, iconid, x, y, z, markertype, 0); 
		}
	}
}
kthxbai
Reply

pawn Код:
stock replacestring(str[], const find[],const finish[])
{
    #define MAXLEN 128
    new start = strfind(str,find);
    do
    {
        new end = start+strlen(find);
        strdel(str,start,end);
        strins(str,finish,start,MAXLEN);
        start = strfind(str,find);
    } while(start != -1);
    return str;
}
This is my own function wich replaces a string to an other string in a string

Example:
pawn Код:
new str[128] = "This is a facked string to censore!";
    print(str);
    replacestring(str,"facked","[censored]");
    print(str);
Will output

Код:
This is a facked string to censore!
This is a [censored] string to censore!
Reply

Sweet function Epsilon
Reply

pawn Код:
stock TeleportPlayerToPlayer(player, player2);
{
    new Float:pos[3];
    GetPlayerPos(player2, pos[0], pos[1], pos[2]);
    if(IsPlayerInAnyVehicle(player)) SetVehiclePos(GetPlayerVehicleID(player), pos[0], pos[1], pos[3];
    else SetPlayerPos(player, pos[0]+1, pos[1]-1, pos[3]+2);
    return 1;
}
For example:
TeleportPlayerToPlayer(0, 1);
Ingame it will be:
ID 0 will be teleported to ID 1, this is my first own made function so please don't be rude
Reply

Only 0.3b function

pawn Код:
stock GetPlayerSwimmingStyle(playerid)
{
    new animname[32];
    GetAnimationName(GetPlayerAnimationIndex(playerid),animname,32,animname,32);
    if( strfind(animname,"Swim_Breast",true) != -1 ) return 1;
    else if( strfind(animname,"SWIM_crawl",true) != -1 ) return 2;
    else if( strfind(animname,"Swim_Dive_Under",true) != -1 ) return 3;
    else if( strfind(animname,"Swim_Glide",true) != -1 ) return 4;
    else if( strfind(animname,"Swim_Tread",true) != -1 ) return 5;
    else if( strfind(animname,"Swim_Under",true) != -1 ) return 6;
    return 0;
}
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Hiddos nice one but here an easier and faster version:
pawn Код:
new
    pName[20]
;
GetPlayerName(playerid, pName, sizeof(pName));
pName[strfind(pName, "_")] = ' ';
// printf("%s", pName);

@Ryder
pawn Код:
new pName[24];//MAX Caracters in Name is 24 no 20... (Correct (MAX_PLAYER_NAME))
GetPlayerName(playerid, pName,24);//Otimize (no called Function sizeof)
for(new i = 0, j =  strlen(pName); i <= j; ++ i)//Otimize (no  function called multiple times)
if(pName[i] == '_')  pName[i] = ' ';//Otimize (No Use called function strfind)
//SetPlayerName(playerid,pName); // Optional
//printf("%s", pName);// Optional
TEST

DraKoN_S > DraKoN S


@Finn
Use instead of While, Loop
Reply

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
@Ryder
pawn Код:
new pName[24];//MAX Caracters in Name is 24 no 20... (Correct (MAX_PLAYER_NAME))
GetPlayerName(playerid, pName,24);//Otimize (no called Function sizeof)
for(new i = 0, j =  strlen(pName); i <= j; ++ i)//Otimize (no  function called multiple times)
if(pName[i] == '_')  pName[i] = ' ';//Otimize (No Use called function strfind)
//SetPlayerName(playerid,pName); // Optional
//printf("%s", pName);// Optional
@Finn
Use instead of While, Loop


TEST

DraKoN_S > DraKoN S


¬¬
https://sampwiki.blast.hk/wiki/Strfind - Read how strfind works & returns, RyDer is correct.

A while and for loop do the same exact thing. Except it's broken up into pieces, if you notice a for loop, you'll see me setting the variables inside the parenthesis. If you notice a while loop, you will see i'm not, but just setting the variable outside and increasing it inside the brackets, just like a for loop does. Example:

pawn Код:
new i;
while(i < MAX_PLAYERS) {
   i++;
}
pawn Код:
for(new i; i < MAX_PLAYERS; i++ ) {

}
Reply

@CarltonTheGG
Yes I know

Ryder the if purpose not work code does

Ryder_DraKoN_CarltonTheGG
Output
Ryder DraKoN_CarltonTheGG

Not everything goes
My code works well.. (more is slower 4x..,LoL)

Time of Functions:
Ryder 0.39 Milesceconds
My 0.129 Milesceconds
_______________________________
#MyCode

pawn Код:
#define replaceonecar(%1,%2,%3)\
for(new i = 0, j =  strlen(%1); i <= j; ++ i)\
if(%1[i] == '%2')\
%1[i] = '%3';
Example:
pawn Код:
replaceonecar(string,D,O);//Use
Output:
DraKoN -> OraKoN
Reply

OMG, I know. I was going out that a RP name always gets checked for one underscore when you join and that's the reason why I didn't use a loop to check all underscores. Secondly the same story again, a RP name (always) mostly has an underscore and that's the reason I didn't do something when it returns -1.
Reply

Yes, indeed most often is '_', more is always excesses, LoL
Reply

This will return in what range a file size is in.. supports Bytes, KB, MB, and GB...

Код:
GetFileSizeInBytes(file[])
{
	if(!fexist(file)) return 0;
	new t;
	new kb,mb,fl;
	new File:raw;
	raw = fopen(file);
	fl = flength(raw);
	mb = 1073741824/fl;
	kb = 1024/fl;
	if(fl > 1024 && fl < 1048576)
	{
		return 1; // 1 = file is in KB
	}
	if(fl >= 1048576)
	{
		return 2; // 2 = file is in MB
	}
	if(fl >= 1073741824)
	{
	    return 3; // 3 = file is in GB
	}
	return 4;
}
Reply

Some codes by me.


Television Head:

Код:
if(!strcmp("/tv-head", cmdtext, true)) return SetPlayerHoldingObject(playerid, 1518, 15, 0, 0, 0.3);
Remove the TV Head:

Код:
if(!strcmp("/tvhead-off", cmdtext, true)) return StopPlayerHoldingObject(playerid);

Nationality code:

Код:
public SetPlayerNameWithCountry(playerid, country[]);
public SetPlayerNameWithCountry(playerid, country[])
{
	new string[128], playername[16];
	GetPlayerName(playerid, playername, 16);
	format(string, 128, "[%s]%s", country, playername);
	SetPlayerName(playerid, string);
	return 1;
}
Example for this:

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
      if(!strcmp("/im-hungarian", cmdtext, true)) return SetPlayerNameWithCountry(playerid, "HUN");
     return 0;
}

IV-MP Messages

A little code, for IV-MP fans.

Код:
stock SendPlayerMessage(playerid, msg[], color = 0xFFFFFFAA) return SendClientMessage(playerid, color, msg);
stock SendMessageToAll(msg[], color = 0xFFFFFFAA) return SendClientMessageToAll(color, msg);
Example for this:

Код:
SendPlayerMessage(playerid, "This is an white message! :D");
or
Код:
SendPlayerMessage(playerid, "This is a lightblue message! :D", 0x33CCFFAA);
Good fun with there functions!
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)