Useful Snippets

Quote:
Originally Posted by funky1234
Посмотреть сообщение
Lock the players vehicle for everyone but the player.

pawn Код:
if (IsPlayerInAnyVehicle(playerid) == 1)
{
        for(new i; i < MAX_PLAYERS; i++)
    {
        SetVehicleParamsForPlayer(GetPlayerVehicleID(playerid), i, 0, 1);
        SetVehicleParamsForPlayer(GetPlayerVehicleID(playerid), playerid, 0, 0);
    }
    return 1;
}
There is another function for this, in a way. But i find the above more efficient
but its not very efficient to call GetPlayerVehicleID(playerid) 1000 times

you just could put the second SetVehicleParamsForPlayer outside the loop
(but exactly the car is already unlocked [for himself] so its quite useless to unlock it again)
and assign GetPlayerVehicleID(playerid) to a variable
Reply

pawn Код:
for(new i=0;i<MAX_PLAYERS;i++) if(IsPlayerConnected(i) && i != playerid) SetVehicleParamsForPlayer(GetPlayerVehicleID(playerid), i, 0, 1);
I think this is the best solution for this.
Reply

pawn Код:
stock IsVehicleUpsideDown(vehicleid)
{
    new Float:quat_w,Float:quat_x,Float:quat_y,Float:quat_z;
    GetVehicleRotationQuat(vehicleid,quat_w,quat_x,quat_y,quat_z);
    new Float:y = atan2(2*((quat_y*quat_z)+(quat_w*quat_x)),(quat_w*quat_w)-(quat_x*quat_x)-(quat_y*quat_y)+(quat_z*quat_z));
    return (y > 90 || y < -90);
}
Reply

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
but its not very efficient to call GetPlayerVehicleID(playerid) 1000 times

you just could put the second SetVehicleParamsForPlayer outside the loop
(but exactly the car is already unlocked [for himself] so its quite useless to unlock it again)
and assign GetPlayerVehicleID(playerid) to a variable
True, thanks for the comment - At least it unlocking 1000 times will make sure it definetly unlocks eh?
Reply

pawn Код:
On The top of the script

#define COLOR_XFX 0x33FF00
#define ads "-Advertisement-"
#define ads_timer 200000 // should be two minutes, edit it w/e you want.

public OnGameModeInit()
{
   SetTimer("ads",ads_timer,1);
   return 1;
}

forward ads();
public ads()
{
   new str[88];
   format(str,88,"%s",ads);
   SendClientMessageToAll(COLOR_XFX,str);
}
Some little advertisement code, hope someone can use it
Reply

@JatoDie
Use Macro
pawn Код:
#define PlayerExecute(playerid,%2) if(%2[0] == '/')  OnPlayerText(playerid,%2)
¬¬


@MenaceX:

pawn Код:
PlayerName(playerid)
{
  new n[24];
  GetPlayerName(playerid,n,24);
  for(new i=0,j = strlen(n); i <= j;++i)
  if(n[i]=='_') n[i]=' ';
  return n;
}
Otimize Your Loop ¬¬

@Hiddos

pawn Код:
public OnGameModeInit()
{
   SetTimer("JPD",7500,true);
   return 1;
}
forward JPD();
public JPD()
{
   for(new i, j = MAX_PLAYERS; i < j ; ++ i)
   {
     if(IsPlayerConnected(i) && GetPlayerSpecialAction(i) == 2)
     {
       //Do your thing
     }
   }
   return 1;
}

Quote:
Originally Posted by Weirdosport
Посмотреть сообщение
pawn Код:
stock TagDetect(playerid)
{
    new string[MAX_PLAYER_NAME];
    GetPlayerName(playerid, string, sizeof(string));
    if(strfind(string, "]", true) > strfind(string, "[", true)) return 1;
    else return 0;
}
No Called Functions ¬¬

Use:

pawn Код:
stock TagDetect(playerid)
{
    new string[MAX_PLAYER_NAME];
    GetPlayerName(playerid, tring,MAX_PLAYER_NAME);//No Use sizeof (called function)
        if(string[0] == '[' > string[0] == ']')  return 1;//No Use strfind (called function)
    else return 0;
}

Final:

pawn Код:
stock RetireTag(playerid)
{
    new string[MAX_PLAYER_NAME];
    GetPlayerName(playerid, tring,MAX_PLAYER_NAME);//No Use sizeof (called function)
        if(string[0] == '[' && string[0] == ']')
        {
            strdel(name, string[0] == '[', string[0] == ']' + 1);
            if(string[0])
            {
               SetPlayerName(playerid, name);
            }
        }
}
Or

pawn Код:
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid, name, sizeof name);
new
startpos = strfind(name, "[", true),
endpos = strfind(name, "]", true);
    if(startpos != -1 && endpos != -1)
    {
       strdel(name, startpos, endpos + 1);
       if(name[0])
       {
          SetPlayerName(playerid, name);
       }
     }
Reply

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
@JatoDie
Use Macro
pawn Код:
#define PlayerExecute(playerid,%2) if(%2[0] == '/')  OnPlayerText(playerid,%2)
With this I can't use return PlayerExecute(...);

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
@MenaceX:

pawn Код:
PlayerName(playerid)
{
  new n[24];
  GetPlayerName(playerid,n,24);
  for(new i=0,j = strlen(n); i <= j;++i)
  if(n[i]=='_') n[i]=' ';
  return n;
}
Otimize Your Loop ¬¬
You got worse this function. If you both use 24, SA:MP can change the max player name. If you use strlen, you're using two loops, bacause strlen is actually a loop. If you put it in a variable, it gets worse, because you create one more variable.

pawn Код:
PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof name);
    for(new i = 0; name[0]; ++i)
        if(name[i] == '_')
            n[i] = ' ';
    return n;
}
Quote:
Originally Posted by DraKoN
Посмотреть сообщение
@Hiddos

pawn Код:
public OnGameModeInit()
{
   SetTimer("JPD",7500,true);
   return 1;
}
forward JPD();
public JPD()
{
   for(new i, j = MAX_PLAYERS; i < j ; ++ i)
   {
     if(IsPlayerConnected(i) && GetPlayerSpecialAction(i) == 2)
     {
       //Do your thing
     }
   }
   return 1;
}
You're creating two variables at this loop, and one of them is defined with a constant: USELESS. for(new i = 0; i < MAX_PLAYERS; ++i) is still better.
And why using IsPlayerConnected? GetPlayerSpecialAction won't return 2 when a player isn't connected, duh. And remember that you have a constant defining the special action 2, in case that SA:MP changes it.

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
No Called Functions ¬¬

Use:

pawn Код:
stock TagDetect(playerid)
{
    new string[MAX_PLAYER_NAME];
    GetPlayerName(playerid, tring,MAX_PLAYER_NAME);//No Use sizeof (called function)
        if(string[0] == '[' > string[0] == ']')  return 1;//No Use strfind (called function)
    else return 0;
}
sizeof ISN'T a function, it's an operator. PAWN compiler will optimize it to 24 anyway. And how can you use two operators comparing the same operation? This won't even work. string[0] is the first cell, duh.

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
Final:
pawn Код:
stock RetireTag(playerid)
{
    new string[MAX_PLAYER_NAME];
    GetPlayerName(playerid, tring,MAX_PLAYER_NAME);//No Use sizeof (called function)
        if(string[0] == '[' && string[0] == ']')
        {
            strdel(name, string[0] == '[', string[0] == ']' + 1);
            if(string[0])
            {
               SetPlayerName(playerid, name);
            }
        }
}
And the sizeof operator again...
And the string[0] thing again... how can string[0] can hold a '[' and a ']' at the same time?
"string[0] == '['" will return a boolean value. strdel uses a integer value.

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
Or

pawn Код:
new name[MAX_PLAYER_NAME];
GetPlayerName(playerid, name, sizeof name);
new
startpos = strfind(name, "[", true),
endpos = strfind(name, "]", true);
    if(startpos != -1 && endpos != -1)
    {
       strdel(name, startpos, endpos + 1);
       if(name[0])
       {
          SetPlayerName(playerid, name);
       }
     }
Your indentation sucks.

Learn the programming language first before trying to correct others. You're being so lame.
Reply

Quote:

With this I can't use return PlayerExecute(...);

I think not accurate because I just used OnPlayerText (playerid, 2%)

Quote:

You're creating two variables at this loop, and one of them is defined with a constant: USELESS. for(new i = 0; i < MAX_PLAYERS; ++i) is still better.
And why using IsPlayerConnected? GetPlayerSpecialAction won't return 2 when a player isn't connected, duh. And remember that you have a constant defining the special action 2, in case that SA:MP changes it.

I was wrong when it comes to have a function (Ex: GetMaxPlayers..) I use 2 Variavels to not check several times..

Quote:

You got worse this function. If you both use 24, SA:MP can change the max player name. If you use strlen, you're using two loops, bacause strlen is actually a loop. If you put it in a variable, it gets worse, because you create one more variable.

Yes, sizeof can be standard, more do not optimize to compile?

Quote:

Your indentation sucks.

Yes is Fуrum is hard Indentation, (This Code is Your ¬¬)
Reply

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
I think not accurate because I just used OnPlayerText (playerid, 2%)
You used "if".

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
I was wrong when it comes to have a function (Ex: GetMaxPlayers..) I use 2 Variavels to not check several times..
Still sucks (if I could understand you).

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
Yes, sizeof can be standard, more do not optimize to compile?
What?

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
Yes is Fуrum is hard Indentation, (This Code is Your ¬¬)
Forum's indentation is the same as every script editor with tabsize 8. You didn't use tabs, so it will be the same thing in Pawno or Notepad. I didn't understand some things in this phrase...
Reply

WHAT? You're so confusing.
Reply

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
@JatoDie
Use Macro
pawn Код:
#define PlayerExecute(playerid,%2) if(%2[0] == '/')  OnPlayerText(playerid,%2)
more like:

pawn Код:
#define PlayerExecute(%0,%1) if(%1[0] == '\0')  OnPlayerText(%0,%1)
?
Reply

Quote:
Originally Posted by DraKoN
Посмотреть сообщение
@JatoDie
Use Macro
pawn Код:
#define PlayerExecute(playerid,%2) if(%2[0] == '/')  OnPlayerText(playerid,%2)
I think this should be OnPlayerCommandText instead of OnPlayerText and %0 instead of %2.
Quote:
Originally Posted by [03]Garsino
Посмотреть сообщение
more like:

pawn Код:
#define PlayerExecute(%0,%1) if(%1[0] == '\0')  OnPlayerText(%0,%1)
?
The fist character is a string terminator? that would be an empty string.
Reply

Get the time in seconds between player connect & player disconnect without using any timers.
Код:
// OnPlayerConnect
SetPVarInt(playerid, "Join Time", gettime());

// OnPlayerDisconnect
new time = gettime() - GetPVarInt(playerid, "Join Time");

// Now you can use "time" variable to get the players playing time in seconds
Reply

Loop From Top!
This code is a regular loop, which loops from top(e.g. starts from 100 and goes down to 1.)
pawn Код:
#define LoopFromTop(%1,%2) \
        for(new %1=%2;%1>0;%1--) // %1 is a symbol(ex. playerid), %2 is max(ex. MAX_PLAYERS)
Example:
pawn Код:
stock Allowance()
{
    LoopFromTop(number,MAX_PLAYERS)
    {
        printf("%i",number); // will print 200,199,198...
    }
    return 1;
}
I'm sure you can find it usefull
Reply

/cvm - Change Vehicle Model

pawn Код:
command(cvm, playerid, params[])
{
    new vehicle[128];
    if(sscanf(params, "s", vehicle)) SendClientMessage(playerid, 0xAA3333AA, "USAGE: /cvm <vehicle>");
    else if(GetVehicleModelIDFromName(vehicle) < 400 || GetVehicleModelIDFromName(vehicle) > 611) SendClientMessage(playerid, 0xAA3333AA, "ERROR: Invalid vehicleid");
    else if(GetPlayerState(playerid) != PLAYER_STATE_DRIVER) SendClientMessage(playerid, 0xAA3333AA, "ERROR: You must be in a vehicle!");
    else
    {
    new Float: VelocityX, Float: VelocityY, Float: VelocityZ, Float: VehAngle, Float: PlayerX, Float: PlayerY, Float: PlayerZ, newveh;
    GetVehicleVelocity(GetPlayerVehicleID(playerid), VelocityX, VelocityY, VelocityZ);
    GetVehicleZAngle(GetPlayerVehicleID(playerid), VehAngle);
    GetVehiclePos(GetPlayerVehicleID(playerid), PlayerX, PlayerY, PlayerZ);
    DestroyVehicle(GetPlayerVehicleID(playerid));
    newveh = CreateVehicle(GetVehicleModelIDFromName(vehicle), PlayerX, PlayerY, PlayerZ, VehAngle, -1, -1, 10);
    PutPlayerInVehicle(playerid, newveh, 0);
    SetVehicleVelocity(newveh, VelocityX*1.1, VelocityY*1.1, VelocityZ*1.1);
    LinkVehicleToInterior(newveh, GetPlayerInterior(playerid));
    SetVehicleVirtualWorld(newveh, GetPlayerVirtualWorld(playerid));
    }
    return 1;
}
With this command you can 'change' your vehicle model with /cvm <vehicle name>.
Basically it creates a new vehicle, puts you in it and sets back your old speed using SetVehicleVelocity and you old angle. It looks like the vehicle model changes.

You also need the codes GetVehicleModelIDFromName and the vehicle name list, which I took from fsdebug, so credits to who made that. You can find those here: http://pastebin.com/17CYGReZ

Have fun with it!
Reply

Replace ErrorMessage with SendClientMessage
Reply

Thanks, forgot to change that.
Updated now ;p
Reply

@Rzzr

Do not called GetVehicleModelIDFromName(vehicle) * 3 times and GetPlayerVehicleID(playerid) * 4 times
and
pawn Код:
GetVehicleModelIDFromName(vname[])
{
    for(new i = 0; i < 211; i++)
    {
        if ( strfind(aVehicleNames[i], vname, true) != -1 )
            return i + 400;
    }
    return -1; // replace -1 with INVALID_VEHICLE_ID
}
pawn Код:
//replace
else if(GetVehicleModelIDFromName(vehicle) < 400 || GetVehicleModelIDFromName(vehicle) > 611) SendClientMessage(playerid, 0xAA3333AA, "ERROR: Invalid vehicleid");
//whit:
else if(GetVehicleModelIDFromName(vehicle) == INVALID_VEHICLE_ID) SendClientMessage(playerid, 0xAA3333AA, "ERROR: Invalid vehicleid");
I thought of:
pawn Код:
command(cvm, playerid, params[])
{
    if(GetPlayerVehicleSeat(playerid) != 0) return SendClientMessage(playerid, 0xAA3333AA, "ERROR: You must be in a vehicle!");
    //------    
    new
        vehicle[128];
    if(sscanf(params, "s[128]", vehicle)) return SendClientMessage(playerid, 0xAA3333AA, "USAGE: /cvm <vehicle>");
    //------
    new
        Get_Vehicle_Model_ID = GetVehicleModelIDFromName(vehicle);
    if(Get_Vehicle_Model_ID == INVALID_VEHICLE_ID) SendClientMessage(playerid, 0xAA3333AA, "ERROR: Invalid vehicleid");
    else
    {
        new
            Float: VelocityX,
            Float: VelocityY,
            Float: VelocityZ,
            Float: VehAngle,
            Float: PlayerX,
            Float: PlayerY,
            Float: PlayerZ,
            newveh,
            Get_VehicleID = GetPlayerVehicleID(playerid);
        GetVehicleVelocity(Get_VehicleID, VelocityX, VelocityY, VelocityZ);
        GetVehicleZAngle(Get_VehicleID, VehAngle);
        GetVehiclePos(Get_VehicleID, PlayerX, PlayerY, PlayerZ);
        DestroyVehicle(Get_VehicleID);
        newveh = CreateVehicle(Get_Vehicle_Model_ID, PlayerX, PlayerY, PlayerZ, VehAngle, -1, -1, 10);
        PutPlayerInVehicle(playerid, newveh, 0);
        SetVehicleVelocity(newveh, VelocityX*1.1, VelocityY*1.1, VelocityZ*1.1);
        LinkVehicleToInterior(newveh, GetPlayerInterior(playerid));
        SetVehicleVirtualWorld(newveh, GetPlayerVirtualWorld(playerid));
    }
    return 1;
}
Reply

Here is a simple /me command with zcmd. The reason i put it here, is because i had hell with it and finally got it to work.

pawn Code:
COMMAND:me(playerid, params[])
{
    new id;
    if(sscanf(params,"u", id))return SendClientMessage(playerid, 0xFF0000AA, "Usage: /me [message]");
    {
        new str[128];
        new mename = GetPlayerName(playerid, str, sizeof(str));
        format(str, sizeof(str), "%s %s.",mename,params);
        SendClientMessageToAll(0xFFFFFFFF, str);
    }
    return 1;
}
Reply

Quote:
Originally Posted by Tee
View Post
Here is a simple /me command with zcmd. The reason i put it here, is because i had hell with it and finally got it to work.

pawn Code:
COMMAND:me(playerid, params[])
{
    new id;
    if(sscanf(params,"u", id))return SendClientMessage(playerid, 0xFF0000AA, "Usage: /me [message]");
    {
        new str[128];
        new mename = GetPlayerName(playerid, str, sizeof(str));
        format(str, sizeof(str), "%s %s.",mename,params);
        SendClientMessageToAll(0xFFFFFFFF, str);
    }
    return 1;
}
This is wrong, you don\'t need to check for an "id" in a /me command.

Pretty simple example:
pawn Code:
CMD:me(playerid, params[])
{
    if(isnull(params)) return SendClientMessage(playerid, 0xFFFFFFFF, "Usage: /me [action]");
    else if((strlen(params) + 24) > 127) return SendClientMessage(playerid, 0xFFFFFFFF, "The action has to be between 0 and 123 characters long!");
    new
        name[24],
        string[128];
       
    GetPlayerName(playerid, name, 24);
    format(string, sizeof(string), "%s %s", name, params);
    SendClientMessageToAll(0xFFFFFFFF, string);
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)