Useful Functions

Useful C/C Functions that you think others can benefit from.

Remember to use the CODE BBS tags

Also when there is a "" in the code you must change it to "" before posting it so it shows correctly and doesnt get filtered out.
Ex.
pspDebugScreenPrintf"HellonNext Linen";
Should be changed to:
pspDebugScreenPrintf"HellonNext Linen";
Reply

i got something great for beginners

Код:
stock SetMapNameText(command[])
{
	new string[48];
	return format(string,sizeof(string),"mapname %s",command), SendRconCommand(string);
}
Reply

pawn Код:
new
    FALSE = 0;
pawn Код:
#if !defined SendRconCommandEx
  #define SendRconCommandEx(%0,%1) \
  do \
  { \
    new \
        string[64]; \
    if(strlen(%0) > 0) \
    { \
      format(string, sizeof(string), %0, %1); \
      SendRconCommand(string); \
    } \
  } \
  while(FALSE)
#endif
pawn Код:
stock SetServerPassword(password[])
{
  SendRconCommandEx("password %s", password);
  return true;
}
pawn Код:
// example:
SetServerPassword("pass1024");
Reply

Quote:
Originally Posted by Don Correlli
pawn Код:
new
    FALSE = 0;
pawn Код:
#if !defined SendRconCommandEx
  #define SendRconCommandEx(%0,%1) \
  do \
  { \
    new \
        string[64]; \
    if(strlen(%0) > 0) \
    { \
      format(string, sizeof(string), %0, %1); \
      SendRconCommand(string); \
    } \
  } \
  while(FALSE)
#endif
pawn Код:
stock SetServerPassword(password[])
{
  SendRconCommandEx("password %s", password);
  return true;
}
pawn Код:
// example:
SetServerPassword("pass1024");
Why creating a new variable (FALSE) if you can just type zero?
Reply

Quote:
Originally Posted by Luka™
Why creating a new variable (FALSE) if you can just type zero?
Is it so hard to compile and see the results without the variable?

Well, you can't. If you do it like that, you'll get the warning 205 - (redundant code: constant expression is zero).
Reply

Quote:
Originally Posted by Don Correlli
Quote:
Originally Posted by Luka™
Why creating a new variable (FALSE) if you can just type zero?
Is it so hard to compile and see the results without the variable?

Well, you can't. If you do it like that, you'll get the warning 205 - (redundant code: constant expression is zero).
Yeah, it is.
Reply

GetPos

pawn Код:
Float:GetPos(playerid,posid)
{
  new Float:id[3];
  if(IsPlayerInAnyVehicle(playerid)) GetVehiclePos(GetPlayerVehicleID(playerid),id[0],id[1],id[2]);else GetPlayerPos(playerid,id[0],id[1],id[2]);
  return id[posid-1];
}
With this function you can simply get one coordinate of a player.

_________________________________________________

Posids:

X - 1
Y - 2
Z - 3


_________________________________________________

For example if you want get the player Z coord;

new Float: Z = GetPos(playerid,3);

_________________________________________________



GetClosestPlayer

pawn Код:
GetClosestPlayer(except,Float: radius = 99999.9999)
{
    new Float:dis[2],e[2];
    e[0] = INVALID_PLAYER_ID;
    dis[0] = radius;
    for (e[1]=0;e[1]<MAX_PLAYERS;e[1]++)
        if(IsPlayerConnected(e[1]) && e[1] != except)
            dis[1] = floatsqroot(floatpower(floatabs(floatsub(GetPos(e[1],1),GetPos(except,1))),2)+floatpower(floatabs(floatsub(GetPos(e[1],2),GetPos(except,2))),2)+floatpower(floatabs(floatsub(GetPos(e[1],3),GetPos(except,3))),2));
            if(dis[1]< dis[0])
            {
                dis[0] = dis[1];
                e[0] = e[1];
            }
    return e[0];
}
This function returns the closest player to you, except you.

If you only want to search in a radius just set that if not just miss that. If you missed that, the function will search all of SA.

Epsilon
Reply

pawn Код:
new
    FALSE = 0;
pawn Код:
#if !defined SendRconCommandEx
  #define SendRconCommandEx(%0,%1) \
  do \
  { \
    new \
        string[64]; \
    if(strlen(%0) > 0) \
    { \
      format(string, sizeof(string), %0, %1); \
      SendRconCommand(string); \
    } \
  } \
  while(FALSE)
#endif
pawn Код:
#if !defined CallRemoteFunctionEx
  #define CallRemoteFunctionEx(%0,%1,%2,%3,%4) \
  do \
  { \
    new \
        string[64]; \
    format(string, sizeof(string), %3, %4); \
    CallRemoteFunction(%0, %1, %2, string); \
  } \
  while(FALSE)
#endif
pawn Код:
stock SetServerName(name[])
{
  SendRconCommandEx("hostname %s", name);
  printf("Setting server name to: \"%s\"", name);
  return true;
}
pawn Код:
// example:
SetServerName("SA-MP v0.3a server");
pawn Код:
stock SetServerGamemode(gamemode[])
{
  SendRconCommandEx("gamemodetext %s", gamemode);
  printf("Setting server gamemode name to: \"%s\"", gamemode);
  return true;
}
pawn Код:
// example:
SetServerGamemode("San Andreas Role-Play");
pawn Код:
stock SetServerMap(map[])
{
  SendRconCommandEx("mapname %s", map);
  printf("Setting server map name to: \"%s\"", map);
  return true;
}
pawn Код:
// example:
SetServerMap("Liberty City");
pawn Код:
stock ForcePlayerCommand(playerid, command[])
{
  if(IsPlayerConnected(playerid))
  {
    new
        playername[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playername, sizeof(playername));
    CallRemoteFunctionEx("OnPlayerCommandText", "is", playerid, "/%s", command);
    printf("Forcing player command: Player: \"%s (%i)\", Command: \"/%s\"", playername, playerid, command);
  }
  else printf("ERROR: That player is not connected to the server!");
  return true;
}
pawn Код:
// example:
ForcePlayerCommand(playerid, "/ooc Hello!");
Reply

pawn Код:
stock ForcePlayerCommand(playerid, command[])
Clever, I've never thought about this before.
Reply

Quote:
Originally Posted by _Xerxes_
Clever, I've never thought about this before.
Well, yes. You can call any local or remote callback with CallLocalFunction/CallRemoteFunction-function.
Reply

PlayerDirection(playerid)

This function gets the direction a player is going:

Returns:
N - North
E - East
S - South
W - West

Note that the function has been setup backwards because SA-MP's directions are backwards.

pawn Код:
stock PlayerDirection(playerid)
{
  new
        Float:ang,
        dir[2];
    if(IsPlayerInAnyVehicle(playerid))
        GetVehicleZAngle(GetPlayerVehicleID(playerid), ang);
    else
        GetPlayerFacingAngle(playerid, ang);
    if (ang > 45 && ang < 135)
        format(dir, sizeof(dir), "W");
    else if (ang > 135 && ang < 225)
        format(dir, sizeof(dir), "S");
    else if (ang > 225 && ang < 315)
        format(dir, sizeof(dir), "E");
    else
        format(dir, sizeof(dir), "N");
  return dir;
}
Reply

Quote:
Originally Posted by <__Ǝthan__>
Note that the function has been setup backwards because SA-MP's directions are backwards.
I forgot if thats true but I know that the wiki try to tell me something different :S -> SetPlayerFacingAngle
Reply

Yes, and that's wrong, this is how it is:

Код:
    north (0)
     |
(90)west- -east (270)
     |
   south (180)
Reply

pawn Код:
public DecreaseHealth(playerid, decreaseby)
{
    new Float:Health;
    GetPlayerHealth(playerid, Health);
    SetPlayerHealth(playerid, Health-decreaseby);
    return 1;
}
Reply

Quote:
Originally Posted by lolumadd [cod5server.tk
]
pawn Код:
public DecreaseHealth(playerid, decreaseby)
{
    new Float:Health;
    GetPlayerHealth(playerid, Health);
    SetPlayerHealth(playerid, Health-decreaseby);
    return 1;
}
you forgot the tag for floating numbers
and you could add a connection check
Reply

I take advantage of your code:

[size=14px]ChangePlayerHealth(playerid, method=increase, Float:amount);[/size]
playerid - id of the player you want to change health for.
method - increase or decrease
Float:amount - how much you want to decrease/increase health with (in float)

EDIT:
I forgot a little important piece of code...
Add this on top:
pawn Код:
#define increase 1
#define decrease 2
pawn Код:
stock ChangePlayerHealth(playerid, Float:amount, method=increase)
{
  new Float:health;
  GetPlayerHealth(playerid, health);
  switch(method)
  {
    case increase: SetPlayerHealth(playerid, health+amount);
    case decrease: SetPlayerHealth(playerid, health-amount);
  }
  return true;
}
Correct me if this code is totally wrong, unefficient and not working.

EDIT2: Oh and if the code actually works, here's example usage:
pawn Код:
//increase
dcmd_ihealth(playerid, p[])
  return ChangePlayerHealth(playerid, strval(p));
//decrease
dcmd_dhealth(playerid, p[])
  return ChangePlayerHealth(playerid, strval(p), decrease);
I know these commands are pretty unefficient, lol. Just some simple examplesxD

****NOTE: This code is NOT compiled nor tested!****

It's probally not even compiling, but oh well...


Sorry if my english is fcked, lol


Reply

Код:
SetPlayerHealth(playerid, GetPlayerHealth(playerid)-20);
To give a player 20 health less, so youDoSomethingWrong == true {I_Forgive_You_:P}
Reply

Quote:
Originally Posted by lrZ^ aka LarzI
pawn Код:
stock ChangePlayerHealth(playerid, method=increase, Float:amount)
{
  new Float:health;
  GetPlayerHealth(playerid, health);
  switch(method)
  {
    case increase: SetPlayerHealth(playerid, health+amount);
    case decrease: SetPlayerHealth(playerid, health-amount);
  }
  return true;
}
Now, SetPlayerHealth aren't a define, correlli. So why not;

pawn Код:
stock ChangePlayerHealth(playerid, method=increase, Float:amount)
{
  new Float:health;
  if(GetPlayerHealth(playerid, health))
  { //EDIT
    switch(method)
    {
      case increase: return SetPlayerHealth(playerid, health+amount);
      case decrease: return SetPlayerHealth(playerid, health-amount);
    }
  }
}
so that you can do:

pawn Код:
if(ChangePlayerHealth(playerid, increase, strval(tmp))) //it will actually return if SetPlayerHealth succeeded.
{
  SendClientMessage(playerid, COLOR_WHAT, "Changing player health successful!");
}//so it's not always returning true.
Reply

Omg, I can't see why I didn't return the switch case statements, lol.
Reply

Quote:
Originally Posted by lrZ^ aka LarzI
Omg, I can't see why I didn't return the switch case statements, lol.
but his code will give an warning, something with missing return at the end
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)