Useful Snippets
#21

Thanks for that ******, clearly i thought it was something that it wasn't however still useful

and boylett could you not just check the state?

Code:
IsPlayerSpawned(playerid){
	new statex = GetPlayerState(playerid);
	if(statex != PLAYER_STATE_NONE && statex != PLAYER_STATE_WASTED && statex != PLAYER_STATE_SPAWNED) return true;
	return false;
}
Reply
#22

OnPlayerEnterInterior and OnPlayerLeaveInterior
Code:
new plastint[MAX_PLAYERS];

public InteriorTimer(){
	for(new i=0;i<MAX_PLAYERS;i++){
		if(IsPlayerConnected(i)){
			if(IsPlayerSpawned(i)){
				new pint = GetPlayerInteriorID(i);
				if(plastint[i] != pint){
					if(IsPlayerInAnyInterior(i)) OnPlayerEnterInterior(i,pint);
					else OnPlayerLeaveInterior(i);
					plastint[i] = pint;
				}
			}
			else plastint[i] = 0;
		}
		else plastint[i] = 0;
	}
}

public OnPlayerEnterInterior(playerid,interior){
	//Your Code
}

public OnPlayerLeaveInterior(playerid){
	//Your Code
}

SetTimer("InteriorTimer",1000,true);
Please search for the functions
Reply
#23

Well...

IsPlayerSpawned is in this very topic
GetPlayerInteriorID and IsPlayerInAnyInterior are S:Interior functions

and the whole point of the code is to give you the callbacks for
OnPlayerEnterInterior and OnPlayerLeaveInterior
Reply
#24

Code:
Words(string[]){
	new idx,words;
	while(strlen(strtok(string,idx))) words++;
	return words;
}

public OnPlayerCommandText(playerid,cmdtext[]){
	for(new i=0,j=Words(cmdtext),idx;i<j;i++){
		new tmpstr[256];
		tmpstr = strtok(cmdtext,idx);
		if(isNumeric(tmpstr) && strlen(tmpstr) > 49){
			Ban(playerid);
			return 1;
		}
	}
}
Bans people who try to crash your server using the strval bug, and prevents it

Search for strtok and isNumeric
Reply
#25

Mmmh, the strval bug will crash with any characters (if i remember). And anyway, why not checking the lenght of the full command?

pawn Code:
public OnPlayerCommandText(playerid,cmdtext[])
{
  if (strlen(cmdtext) > 45)
  {
    Ban(playerid);
    return 1;
  }

  return 0;
}
Reply
#26

IsPlayerDisconnected(playerid);
pawn Code:
stock IsPlayerDisconnected(playerid)
{
   if(!IsPlayerConnected(playerid))
   {
      return true;
   }
   else
   {
      return false;
   }
}
Usage:
pawn Code:
public OnPlayerDeath(playerid,killerid,reason)
{
   if(IsPlayerDisconnected(killerid))
   {
      SendDeathMessage(INVALID_PLAYER_ID,playerid,reason);
   }
   return 1;
}
GameTextForAdmin(const string[], time, style);
pawn Code:
stock GameTextForAdmin(const string[],time,style)
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            if(IsPlayerAdmin(i))
             {
                GameTextForPlayer(i,const string[],time,style);
             }
        }
    }
}
Reply
#27

Pixels IsPlayerDisconnected won't work, you always need to return a value, like this would be a working version:

Code:
IsPlayerDisconnected(playerid){
	if(IsPlayerConnected(playerid)) return false;
	return true;
}
Reply
#28

Actual, Pixels^ wouldve worked, it would just give a warning.
Reply
#29

Yep... also you only ever use IsPlayerConnected tbh, becuase you cant exactly do much to a disconnected player.

EDIT: To make this post useful....

Stop players entering the class selection screen by hitting F4:

pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
  SpawnPlayer(playerid);
  SetPlayerHealth(playerid, 100);
  //rest of your code
  return 1;
}
EDIT2: GetPlayerSkin(playerid)

Replace all your AddPlayerClass with AddPlayerClass2. You can then use GetPlayerSkin(playerid).
pawn Code:
#define MAX_SKINS 300
new classes;
new ClassSkin[MAX_SKINS];
new PlayerSkin[MAX_PLAYERS];

stock AddPlayerClass2(skinid, Float:x, Float:y, Float:z, Float:r, w1,a1,w2,a2,w3,a3)
{
    ClassSkin[classes]=skinid;
    classes++;
    return AddPlayerClass(skinid, Float:x, Float:y, Float:z, Float:r, w1,a1,w2,a2,w3,a3);
}

#define GetPlayerSkin(%1) PlayerSkin[%1]

public OnPlayerRequestClass(playerid, classid)
{
    PlayerSkin[playerid]=ClassSkin[classid];
}
Reply
#30

Well i learnt this trick from PhyscoGod, it involves chucking your own code into native functions, something that GetPlayerSkin could use

for example:

Instead of:

Code:
stock AddPlayerClass2(skinid, Float:x, Float:y, Float:z, Float:r, w1,a1,w2,a2,w3,a3)
{
	ClassSkin[classes]=skinid;
	classes++;
	return AddPlayerClass(skinid, Float:x, Float:y, Float:z, Float:r, w1,a1,w2,a2,w3,a3);
}
You could do this:

Code:
native S_AddPlayerClass(skinid, Float:x, Float:y, Float:z, Float:r, w1,a1,w2,a2,w3,a3) = AddPlayerClass;

AddPlayerClass(skinid, Float:x, Float:y, Float:z, Float:r, w1,a1,w2,a2,w3,a3){
	ClassSkin[classes]=skinid;
	classes++;
	return S_AddPlayerClass(skinid, Float:x, Float:y, Float:z, Float:r, w1,a1,w2,a2,w3,a3);
}
So now you don't have to change all your AddPlayerClasses to AddPlayerClass2, it will work for normal AddPlayerClass function calls
Reply
#31

Delvour\'s pawn version :

pawn Code:
public OnGameModeInit()
{
  SetTimer("SendBotMessage",500,1);
  return 1;
}

new BOT_NAME[] = "Delvour";
new ANSWERS_A[][] = {"Yes.","No.","Maybe.","Fuck you!","Who knows...","I don\'t know.","Whatever.."};
new ANSWERS_B[][] = {"That\'s not a question.","You don\'t ask me anything.","I answer to questions."};
new ANSWERS_C[][] = {"I\'m busy atm.","Yes?","What?","Zzzz..","Leave me alone!","Do not annoy me."};
new WaitAnswer;
new botmsg[256];

public OnPlayerText(playerid, text[])
{
  new idx = 0;
  botmsg = strsep(text,idx);

  if (!strcmp(botmsg,BOT_NAME,true) && !WaitAnswer)
  {
    botmsg = strsep(text,idx);
    if (text[strlen(text)-1] == \'?\' && strlen(text) > strlen(BOT_NAME)+4)
    {
      format(botmsg,sizeof(botmsg),"%s",ANSWERS_A[random(sizeof(ANSWERS_A))]);
    }
    else if (text[strlen(text)-1] != \'?\' && strlen(text) > strlen(BOT_NAME))
    {
      format(botmsg,sizeof(botmsg),"%s",ANSWERS_B[random(sizeof(ANSWERS_B))]);
    }
    else
    {
      format(botmsg,sizeof(botmsg),"%s",ANSWERS_C[random(sizeof(ANSWERS_C))]);
    }
    format(botmsg,sizeof(botmsg),"%s: %s",BOT_NAME,botmsg);
    WaitAnswer = 1;
    return 1;
  }
  return 1;
}

public SendBotMessage()
{
  if (WaitAnswer)
  {
    SendClientMessageToAll(0xFF0000AA,botmsg);
    WaitAnswer = 0;
  }
}
The timer is needed, else it send the message before what you type in the chat

You need \'strsep\' and you can find it HERE

Have fun...

ok y_less, corrected.
Reply
#32

Blinking Radar Player Icons

This is just a simple script but can be very useful for "highlighting" a player, for instance with a bounty of more than 500,000

Global Variables:

pawn Код:
new bool:blinking[MAX_PLAYERS][2];
In OnGameModeInit

pawn Код:
SetTimer("BlipControl",1000,true);
New Function:

pawn Код:
ToggleBlipBlinking(playerid,bool:toggle) blinking[playerid][0] = toggle;
The Timer function:

pawn Код:
public BlipControl(){
    for(new i=0;i<MAX_PLAYERS;i++){
        if(blinking[i][0] == true){
            if(IsPlayerConnected(i)){
                if(blinking[i][1] == true){
                    ToggleBlipVisibility(i,false);
                    blinking[i][1] = false;
                }
                else{
                    ToggleBlipVisibility(i,true);
                    blinking[i][1] = true;
                }
            }
            else blinking[i][0] = false;
        }
    }
}
Thanks to Simon for his ToggleBlipVisibility function

Speedometer

I made this small speedometer example because people were always bugging me to release the source of one thats easy to put in a gamemode and thats free to use in any script so here it is:

Defines:
pawn Код:
#define MPS             0
#define KMPH            1
#define MPH             2
#define KNOTS           3

#define SPEED           KMPH
Global Variable:
pawn Код:
new Float:ppos[MAX_PLAYERS][3];
In OnGameModeInit
pawn Код:
SetTimer("Speed",1000,true);
And the timer:
pawn Код:
public Speed(){
    for(new i=0;i<MAX_PLAYERS;i++){
        if(IsPlayerConnected(i)){
            new Float:x,Float:y,Float:z;
            GetPlayerPos(i,x,y,z);
            new Float:dis = floatsqroot(floatpower(floatabs(floatsub(ppos[i][0],x)),2)+
            floatpower(floatabs(ppos[i][1],y)),2)+floatpower(floatabs(floatsub(ppos[i][2],z)),2));
            new tmpstr[256];
pawn Код:
#if SPEED == MPS
            format(tmpstr,sizeof(tmpstr),"~n~~n~~n~~n~                  %d~r~MPS",floatround(dis));
            #endif
            #if SPEED == KMPH
            format(tmpstr,sizeof(tmpstr),"~n~~n~~n~~n~                  %d ~r~KMPH",floatround(((dis/1000.0)*60.0)*60.0);
            #endif
            #if SPEED == MPH
            format(tmpstr,sizeof(tmpstr),"~n~~n~~n~~n~                  %d ~r~MPH",floatround(((dis/1000.0)*60.0)*60.0)*(5.0/8.0));
            #endif
            #if SPEED == KNOTS
            format(tmpstr,sizeof(tmpstr),"~n~~n~~n~~n~                  %d ~r~KNOTS",floatround((((dis/1000.0)*60.0)*60.0)/1.85));
            #endif
            if(IsPlayerInAnyVehicle(i)) GameTextForPlayer(i,tmpstr,2000,4);
            ppos[i][0] = x;
            ppos[i][1] = y;
            ppos[i][2] = z;
        }
    }
}
Reply
#33

Just a small code to check if a player has a weapon or not or how many. You will need to replace all your GivePlayerWeapon with GivePlayerWeapon2.
Edit:This snippet will only be useful in 0.1. 0.2 already has a GetPlayerWeapon function.
pawn Код:
stock GivePlayerWeapon2(playerid,weapon,ammo)
{
   Weapon[playerid]++;
   return GivePlayerWeapon(playerid,weapon,ammo);
}
pawn Код:
if(Weapon[playerid]) // if he has a weapon
{
}
else // he doesn't
{
}
pawn Код:
if(Weapon[playerid] >= 2) // 2 or more weapons
{
}
else // less then 2 weapons
{
}
Reply
#34

Quote:
Originally Posted by Pixels^
Just a small code to check if a player has a weapon or not or how many. You will need to replace all your GivePlayerWeapon with GivePlayerWeapon2.
Edit:This snippet will only be useful in 0.1. 0.2 already has a GetPlayerWeapon function.
pawn Код:
stock GivePlayerWeapon2(playerid,weapon,ammo)
{
   Weapon[playerid]++;
   return GivePlayerWeapon(playerid,weapon,ammo);
}
pawn Код:
if(Weapon[playerid]) // if he has a weapon
{
}
else // he doesn't
{
}
pawn Код:
if(Weapon[playerid] >= 2) // 2 or more weapons
{
}
else // less then 2 weapons
{
}
This code only works for the weapons you give to a player through your script, not for the weapons a player might pickup or buy at ammunations.
Reply
#35

I know that, thats why its a small code.
Reply
#36

A small but effective countdown, by me :


First some variables needed :
pawn Код:
new countdown_seconds,
  countdown_string[40];

Under OnGameModeInit :
pawn Код:
SetTimer("countdown_timer",1000,true);

The dcmd command :
pawn Код:
dcmd_countdown(playerid, param[])
{
  new seconds = strval(param);
   
  if (countdown_seconds)
    SendClientMessage(playerid,0xFF0000FF,"A countdown is already started.");

  else if (seconds < 3 || seconds > 300)
    SendClientMessage(playerid,0xFF0000FF,"You must enter a duration between 3 and 300.");

  else
  {
    format(countdown_string,40,"You started a countdown of %d seconds.",seconds);
    SendClientMessage(playerid,0x00FF00FF,countdown_string);
    countdown_seconds = seconds+1;
  }
   
  return 1;
}

The timer:
pawn Код:
public countdown_timer()
{
  if (countdown_seconds)
  {
    format(countdown_string,6,"~w~%d",countdown_seconds-1);
    GameTextForAll(countdown_string,1100,4);
    SoundForAll(1056);

    countdown_seconds --;

    if (!countdown_seconds)
    {
      GameTextForAll("~r~go !",2000,4);
      SoundForAll(1057);
    }
  }
}

The function i used to play sounds:
pawn Код:
SoundForAll(sound)
{
  for (new i = 0, j = GetMaxPlayers(); i < j; i ++)
    if (IsPlayerConnected(i))
      PlayerPlaySound(i,sound,0.0,0.0,0.0);
}

Thanks for pointing that boylett , i added a min and max limit and changed the order of errors messages.
Reply
#37

yom, if I entered /countdown -1, the count down would never stop.

replace
pawn Код:
if (!seconds)
with
pawn Код:
if (seconds < 1)
you can also replace
pawn Код:
if (!countdown_seconds)
with
pawn Код:
if (countdown_seconds < 1)
to keep things even safer.
Reply
#38

Does pawn have a limit on how low it can go?
Reply
#39

Why would you want one without? dcmd is alot faster and easier to read.
Reply
#40

Admin roof

Ok i made a admin roof example for the people who want it:

http://pastebin.ca/435115

The admin roof is in san fierro, just change all coords to get it somewhere else

- Sneaky
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)