SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Filterscripts (https://forum.sa-mp.com/forumdisplay.php?f=17)
-   -   Useful Snippets (https://forum.sa-mp.com/showthread.php?t=281)

Pixels^ 27/01/2007 11:33 PM

Useful Snippets
 
As i see that the Post your interesting/weird... commands is flooded with not just commands,
but other snippets that go in different areas of the SA:MP Scripts, i decided to make this
topic with Y_Less' approval for people to post there useful, weird, or interesting snippets that not just
go into the OnPlayerCommandText Area but other areas of the SA:MP scripts. Ill start it out.

Connect icon in DeathStats from Y_Less
pawn Code:
public OnPlayerConnect(playerid)
{
   SendDeathMessage(playerid, INVALID_PLAYER_ID, 200);
   return 1;
}


Disconnect icon in DeathStats from Y_Less
pawn Code:
public OnPlayerDisconnect(playerid)
{
  SendDeathMessage(INVALID_PLAYER_ID, playerid, 201);
}

Pixels^ 28/01/2007 12:32 AM

Re: Useful Snippets
 
Admin Lock

pawn Code:
new aVehicle; //Above Main

pawn Code:
//gamemodeinit
aVehicle = AddStaticVehicle(598,2256.0903,2477.0115,10.5668,178.5022,0,1); //gamemodeinit
pawn Code:
public AdminLock(playerid) //Anywhere in script
{
 if(IsPlayerConnected(playerid))
 {
   if(IsPlayerAdmin(playerid))
   {
    SetVehicleParamsForPlayer(aVehicle, playerid,0,0);
   }
   else
   {
    SetVehicleParamsForPlayer(aVehicle, playerid,0,1);
   }
 }
}
pawn Code:
public OnPlayerSpawn(playerid)
{
   AdminLock(playerid);
   return 1;
}

pawn Code:
AddStaticVehicleEx(598,2256.0903,2477.0115,10.5668,178.5022,0,1,never);

Sacky 28/01/2007 10:49 AM

Re: Useful Snippets
 
Well this is a small thing but i find it a bit more efficient,

instead of using for(new i=0;i<MAX_PLAYERS

I use this:

Code:

new maxplayers; //Global Variable

public OnPlayerConnect(playerid){
        if(playerid > maxplayers-1) maxplayers = playerid+1; //Getting the maximum playerid
}

public OnPlayerDisconnect(playerid){
        if(playerid == maxplayers+1) for(new i=0;i<maxplayers;i++) if(IsPlayerConnected(i)) maxplayers = i+1; //Getting the minimum playerid
}

for(new i=0;i<maxplayers;i++) //the new more efficient loop


Sacky 28/01/2007 02:18 PM

Re: Useful Snippets
 
Quote:

That won't work, you're assuming that all players are consecutive, which is rarely the case, you could have 10 players max, but only players 1, 4, 7 and 9 connected, which would make the max thing 9, saving almost nothing, and you would still need to check for connection.
No I'm not assuming there consecutive, check my code again, i find the highest playerid according to OnPlayerConnect and make the max loop that, then when a player disconnects i reevaluate the highest playerid

since basically all MAX_PLAYERS loops try to loop according to playerids you only need the highest one, hence this is more efficient

-Sneaky- 28/01/2007 06:38 PM

Re: Useful Snippets
 
ok about the admin vehcile's
i added 13 admin vehicle's and i can open them when im not admin.. now i realized that i had to add this, before it would work:

Code:

AddStaticVehicleEx(425,2137.0598,1729.9246,20.5675,59.3952,0,1,never);
    AddStaticVehicleEx(522,2164.1125,1720.9868,20.1695,161.4324,0,1,never);
        AddStaticVehicleEx(522,2168.9771,1719.4484,19.9998,169.0915,0,1,never);
        AddStaticVehicleEx(522,2174.8657,1718.4332,20.1868,165.7749,0,1,never);
        AddStaticVehicleEx(522,2179.6699,1717.1212,19.9994,166.9654,0,1,never);
        AddStaticVehicleEx(444,2208.9463,1654.0419,20.0004,2.0738,0,1,never);
        AddStaticVehicleEx(556,2184.9636,1654.0623,20.0956,2.9973,0,1,never);
        AddStaticVehicleEx(425,2201.3828,1716.8575,34.2190,88.4603,0,1,never);
        AddStaticVehicleEx(520,2141.1396,1637.6151,20.1024,120.4169,0,1,never);
        AddStaticVehicleEx(541,2175.4431,1613.0690,20.0886,182.6934,0,1,never);
        AddStaticVehicleEx(411,2168.8147,1614.7198,20.0885,181.9470,0,1,never);
        AddStaticVehicleEx(451,2163.6968,1613.2709,20.0878,175.1219,0,1,never);
        AddStaticVehicleEx(447,2141.5942,1591.2502,31.6095,124.6433,0,1,never);

And get this error:

Code:

(19229) : error 021: symbol already defined: "AddStaticVehicleEx"
I searched thru the forums how i could fix this error but found nothing.

Greetz, Sneaky



Pixels^ 28/01/2007 06:43 PM

Re: Useful Snippets
 
only 1 vehicle can be locked with the lock i posted
you need maxvehicles for all of them to be unlocked

Everybody needs sleep Code


pawn Code:
enum pInfo
{
    Sleeping,
    Awake,
    Tired
}
new Info[MAX_PLAYERS][pInfo];
pawn Code:
SetTimer("Motels",5000,1);
SetTimer("SleepTime",300000,1);
pawn Code:
public SleepTime()
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
      SendClientMessage(i,COLOR_GREY,"* You are starting to feel sleep *");
        SendClientMessage(i,COLOR_GREY,"* Go to the nearest /hotel and get a rest *");
        Info[i] [Tired] = 1;
        Info[i] [Awake] = 0;
        Info[i] [Sleeping] = 0;
    }
}
pawn Code:
public Motels()
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
      if(IsPlayerInCube(i,2236,-1076,1040,2245,-1066,1100))
      {
        if(Info[i][Tired] == 1)
        {
                GameTextForPlayer(i,"~w~Please wait while your energy is ~b~restored",3000,3);
                Info[i] [Tired] = 0;
                Info[i] [Sleeping] = 1;
                Info[i] [Awake] = 0;
                TogglePlayerControllable(i,0);
        }
        if(Info[i] [Awake] == 1)
        {
                GameTextForPlayer(i,"~w~You ~g~don't ~w~need to rest",2000,3);
        }
        if(Info[i] [Sleeping] == 1)
        {
                Info[i] [Awake] = 1;
                Info[i] [Tired] = 0;
                Info[i] [Sleeping] = 0;
                GameTextForPlayer(i,"~w~Rise and Shine",2000,3);
                TogglePlayerControllable(i,1);
        }
      }
      if(IsPlayerInCube(i,2202,-1070,1000,2211,-1078,1100))
      {
        if(Info[i][Tired] == 1)
        {
                GameTextForPlayer(i,"~w~Please wait while your energy is ~b~restored",3000,3);
                Info[i] [Tired] = 0;
                Info[i] [Sleeping] = 1;
                Info[i] [Awake] = 0;
                TogglePlayerControllable(i,0);
        }
        if(Info[i] [Awake] == 1)
        {
                GameTextForPlayer(i,"~w~You ~g~don't ~w~need to rest",2000,3);
        }
        if(Info[i] [Sleeping] == 1)
        {
                Info[i] [Awake] = 1;
                Info[i] [Tired] = 0;
                Info[i] [Sleeping] = 0;
                GameTextForPlayer(i,"~w~Rise and Shine",2000,3);
                TogglePlayerControllable(i,1);
        }
      }
    }
  return 1;
}

yom 28/01/2007 07:01 PM

Re: Useful Snippets
 
Why not simply:

pawn Code:
if ( IsPlayerInCube(i,2236,-1076,1040,2245,-1066,1100) || IsPlayerInCube(i,2202,-1070,1000,2211,-1078,1100) )

So you can delete half of the timer func. Also check if they are connected before doing anything.

Pixels^ 28/01/2007 07:03 PM

Re: Useful Snippets
 
I made it a long time ago so i didn't know about that then.

-Sneaky- 28/01/2007 08:05 PM

Re: Useful Snippets
 
ok pixels, how can i make all vehicles unlocked for admin and locked for normal players then?
i really dont know how :(

Pixels^ 28/01/2007 08:13 PM

Re: Useful Snippets
 
Something like this

pawn Code:
#define MAX_VEHICLES 255 //amount of cars in script
new aVehicle[MAX_VEHICLES] //define of aVehicle
pawn Code:
new never = 1000000;
    aVehicle[1] = AddStaticVehicleEx(522,2164.1125,1720.9868,20.1695,161.4324,0,1,never);
    aVehicle[2] = AddStaticVehicleEx(522,2168.9771,1719.4484,19.9998,169.0915,0,1,never);

pawn Code:
public AdminLock(playerid) //Anywhere in script
{
  if(IsPlayerConnected(playerid))
  {
   if(IsPlayerAdmin(playerid))
   {
     SetVehicleParamsForPlayer(aVehicle[MAX_VEHICLES], playerid,0,0);
   }
   else
   {
     SetVehicleParamsForPlayer(aVehicle[MAX_VEHICLES], playerid,0,1);
   }
  }
}


All times are GMT. The time now is 06:29 PM.

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