SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 05/06/2008, 05:09 PM   #1
Sandra18[NL]
Gangsta
 
Join Date: Jun 2007
Posts: 806
Reputation: 45
Default Useful Functions

Useful Functions

Because the old topic was deleted, i got permission to create a new 'Useful Functions'-topic.
In this topic you can post selfmade functions.

Rules:
  • DO NOT POST UNTESTED CODE!
  • Do not spam
  • Do not go offtopic
  • If you post a function which is not created by yourself, you have to give the original author credits
  • And also if you use a function in your gamemode that you copied from this topic, don't forget to give the original author credits



Sandra18[NL] is offline   Reply With Quote
Old 05/06/2008, 05:15 PM   #2
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,517
Reputation: 2927
Default Re: Usefull Functions

Gotta love Kye deleting my account and thus all the topics that went with it. Anyway, some of my useful functions I've made over the years have been ported to YSI and put in the YSI_misc.own file. The file can be found:

here

Functions include:

swap(var1, var2) - Swap two variables without a third variable.
xor:==xor: - Do a logical xor check on two variables.
ceildiv - Divide two integers and round up.
isnull - Used for empty strings passed via CallRemoteFunction.
iseven - Obvious.
isodd - Obvious.
floordiv - Rounded down integer division.
chrfind - Find a single character in a string, faster than strfind for single characters.
chrnfind - Find the end of a set of characters in a string (for example to skip a set of spaces).
strcpy - Copy a string.
bernstein - Like Adler32 hash but faster and less collisions.
bernstein_copy - Mix of strcpy and bernstein for speed.
StripNL - Similar to PHP's rtrim.
strconcat - Stick two strings together.
QuickSort - Call wrapper for QSort, sorts an array into newmerical order.
QSort - Workhorse recursive function for QuickSort.
chrtolower - Convert a character to lowercase.
strtolower - Convert a string to lowercase.
hexstr - strval for hex strings.
binstr - Checks if a string is "0" or "false".
ReturnPlayerName - Obvious.
ishex - Checks if a string is hexadecimal.
numstr - Converts a number to a string, should use valstr but doesn't.
chrtoupper - Convert a character to upper case.
strtoupper - Convert a string to upper case.
timestamp - Get the unix timestamp for the current time.
mktime - Get the unix timestamp for a given time.
GetIP - Return a player's IP as a 32bit integer.
getintpar - Gets a number from a string apparently.
IsConnected - Like IsPlayerConnected but uses YSI's internal foreach system.
KickDelay - Kick a player after a given amount of time. Used to ensure they get the message sent.
KickDelayCall - public function KickDelay calls.
BanDelay - Like KickDelay but ban.
BanDelayCall - Like KickDelayCall but ban.
endofline - Checks if the current position is the end of non-whitespace data.
isnumeric - Checks if a string is numeric.
abs - Returns the absolute (positive) value of a number.
sscanf - Deserves (and has) it's own tutorial on use.
explode - Like PHP's explode function.
IsPlayerInRangeOfPoint - Checks if a player is near a point without calling floatsqroot for speed.

The library also has a couple of useful variables:

TRUE - Always true, used for infinate loops.
FALSE - Always false, used for large code macros.
NULL - The string passed instead of an empty string by CallRemoteFunction due to the way the PAWN VM works.
Y_Less is offline   Reply With Quote
Old 05/06/2008, 05:38 PM   #3
Yaheli_Faro
High-roller
 
Join Date: Feb 2008
Posts: 1,053
Reputation: 8
Default Re: Usefull Functions

CagePlayer(playerid);
this is function does the same as my /cage command in V-Admin (puts a player in a cage :P).

Code:
// Top of script
new cage[MAX_PLAYERS], cage2[MAX_PLAYERS], cage3[MAX_PLAYERS], cage4[MAX_PLAYERS], caged[MAX_PLAYERS];

// new function
stock CagePlayer(playerid)
{
	  if(IsPlayerConnected(playerid))
	  {
  	  new Float:X, Float:Y, Float:Z;
  	  GetPlayerPos(playerid, X, Y, Z);
  	  cage[playerid] = CreateObject(985, X, Y+4, Z, 0.0, 0.0, 0.0);
  	  cage2[playerid] = CreateObject(985, X+4, Y, Z, 0.0, 0.0, 90.0);
  	  cage3[playerid] = CreateObject(985, X-4, Y, Z, 0.0, 0.0, 270.0);
  	  cage4[playerid] = CreateObject(985, X, Y-4, Z, 0.0, 0.0, 180.0);
      caged[playerid] = 1; // Use this in a /cage command to prevent being caged twice and causing the cage to be unremovable.
  	  PlayerPlaySound(playerid, 1137, X, Y, Z);
	  }
}

UnCagePlayer(playerid);
This will remove the cage

Code:
stock UnCagePlayer(playerid)
{
	  cage[playerid] = DestroyObject(cage[playerid]);
	  cage2[playerid] = DestroyObject(cage2[playerid]);
	  cage3[playerid] = DestroyObject(cage3[playerid]);
	  cage4[playerid] = DestroyObject(cage4[playerid]);
      caged[playerid] = 0;
}
Yaheli_Faro is offline   Reply With Quote
Old 05/06/2008, 05:50 PM   #4
Rafelder_GRF
Huge Clucker
 
Join Date: Sep 2007
Posts: 220
Reputation: 4
Default Re: Useful Functions

DisableBadWord(word[])
word[] - word which will be disabled
Code:
#define DisableBadword(%1) for(new i=0; i<strlen(text); i++) if(strfind(text[i], %1, true) == 0) for(new a=0; a<256; a++) if (a >= i && a < i+strlen(%1)) text[a]='*'
Rafelder_GRF is offline   Reply With Quote
Old 05/06/2008, 05:54 PM   #5
Sandra18[NL]
Gangsta
 
Join Date: Jun 2007
Posts: 806
Reputation: 45
Default Re: Usefull Functions

Quote:
Originally Posted by Andre9977
PS: It's "Useful Functions", not "Usefull functions".
Thanks, i fixed typo now

SendClientMessageToAllEx(exception, color, const message[])
This function sends a message to all players, except for 1 player. (exception)

Code:
stock SendClientMessageToAllEx(exception, color, const message[])
{
  for(new i; i<MAX_PLAYERS; i++)
  {
    if(IsPlayerConnected(i))
 	{
	  if(i != exception)
	  {
	    SendClientMessage(i, color, message);
	  }
	}
  }
}

GetVehicleDriver(vehicleid)
Returns the ID of the player driving in the vehicle. Returns '-1' if nobody is driving in the vehicle.
Code:
stock GetVehicleDriver(vehicleid)
{
  for(new i; i<MAX_PLAYERS; i++)
  {
    if (IsPlayerInVehicle(i, vehicleid))
    {
      if(GetPlayerState(i) == 2)
      {
    		return i;
      }
	}
  }
  return -1;
}
Sandra18[NL] is offline   Reply With Quote
Old 05/06/2008, 06:12 PM   #6
Ophion
Little Clucker
 
Join Date: Nov 2007
Posts: 24
Reputation: 5
Default Re: Usefull Functions

Quote:
Originally Posted by =>Sandra<=
SendClientMessageToAllEx(exception, color, const message[])
This function sends a message to all players, except for 1 player. (exception)

Code:
stock SendClientMessageToAllEx(exception, color, const message[])
{
  for(new i; i<MAX_PLAYERS; i++)
  {
    if(IsPlayerConnected(i))
 	{
	  if(i != exeption)
	  {
	    SendClientMessage(i, color, message);
	  }
	}
  }
}
Need a 'c' in "if(i != ex(c)eption)"

EDIT: Please delete this post when noted.
Ophion is offline   Reply With Quote
Old 05/06/2008, 06:21 PM   #7
Yaheli_Faro
High-roller
 
Join Date: Feb 2008
Posts: 1,053
Reputation: 8
Default Re: Usefull Functions


Quote:
Originally Posted by =>Sandra<=
SendClientMessageToAllEx(exception, color, const message[])
This function sends a message to all players, except for 1 player. (exception)
Isn't this the same as Yom's "SendClientMessageToAllOthers"?
Yaheli_Faro is offline   Reply With Quote
Old 05/06/2008, 06:48 PM   #8
domidom
Gangsta
 
Join Date: Feb 2007
Posts: 512
Reputation: 12
Default Re: Useful Functions

Quote:
Originally Posted by Andre9977
GetName()
playerid - the player who's name we're getting
The name will be stored in "PlayerName" variable!
Code:
stock GetName(playerid)
{
  new PlayerName; 
  GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
  return PlayerName;
}
Have you tried compiling that?

Also:
Code:
printf("KickEx(): %s(%i) kicked, reason: %s", PlayerName, playerid);
Should be
Code:
printf("KickEx(): %s(%i) kicked, reason: %s", PlayerName, playerid, reason);
right?

And also, it seems you are using your own GetName function wrongly, you say its GetName(playerid, variable) in the NameCheck() thing, yet it is actually GetName(playerid).
domidom is offline   Reply With Quote
Old 05/06/2008, 06:50 PM   #9
Grove
Gangsta
 
Join Date: Jan 2007
Posts: 845
Reputation: 6
Default Re: Usefull Functions

Admin(msg) - Sends a message to the online rcon admins!
Code:
stock Admin(msg[])
{
  for(new i 0; i<GetMaxPlayers(); i++
  {
    if(IsPlayerConnected(i) && IsPlayerAdmin(i)) SendClientMessage(i,white,msg);
  }
}
Quote:
Originally Posted by Y_Leѕѕ
Gotta love Kye deleting my account and thus all the topics that went with it.
Sorry to go offtopic, but why did he do that?



Grove is offline   Reply With Quote
Old 05/06/2008, 07:05 PM   #10
domidom
Gangsta
 
Join Date: Feb 2007
Posts: 512
Reputation: 12
Default Re: Useful Functions

Quote:
Originally Posted by Andre9977
Quote:
Originally Posted by DragSta
< shizzle >
Yes, fixed now.

About the cells I forgot - I was about to write MAX_PLAYER_NAME instead of 24, but then something went... wrong.
Ok, well your half way there. But it still wont work, GetName(playerid) directly returns the players name, so it could be used like:
Code:
printf("Name: %s", GetName(playerid));
It wont work because you just call it so it gets the players name (yet you dont catch what it returns), then you try and read the NON Global variable...
domidom is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions? sciman001 Help Archive 7 19/03/2011 04:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 08:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 09:38 AM
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 11 03/05/2010 10:47 PM
What this functions do? harrold Help Archive 2 09/05/2009 12:30 AM


All times are GMT. The time now is 09:36 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.