OnPlayerStateChange, locking vehicles
#1

pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    new vehicleID = GetPlayerVehicleID(playerid);
    if(newstate == PLAYER_STATE_DRIVER && PlayerInfo[playerid][pFac] != 1)
    {
        if(vehicleID == 596 || 597 || 598 || 599 || 523)
        {
            RemovePlayerFromVehicle(playerid);
            SendClientMessage(playerid, COLOR_RED, "You have to be a Police officer to drive this vehicle!");
        }
        else if(vehicleID == 490 && PlayerInfo[playerid][pFacRank] < 3)
        {
            RemovePlayerFromVehicle(playerid);
            SendClientMessage(playerid, COLOR_RED, "You need to be a police officer at rank 3 or above to drive this vehicle!");
        }
    }
    return 1;
}
That's the above code and all the vehicle IDS are locked when not in faction 1... but you can be any 'FacRank' when entering ID 490... why is that?
Reply
#2

There are vehicleids and then there are modelids. A single vehicle has both a vehicleid, which is unique and a modelid, which is not unique. Don't confuse them. Furthermore, you cannot check a range of values like this:
PHP код:
if(vehicleID == 596 || 597 || 598 || 599 || 523
Each operand is self-contained. Meaning the above will basically compile to:
PHP код:
if(vehicleID == 596 || true || true || true || true
And as a result that statement will always be true. You should check as follows:
PHP код:
if(model == 596 || model == 597 || ...) 
However I would advice using either a switch or an array if you intend to add more items to that list otherwise you'll end up with very long lines which are really, really ugly.
Reply
#3

pawn Код:
new PoliceCars[50];
pawn Код:
PoliceCars[1] = AddStaticVehicle(598,2312.0000000,2431.1001000,3.1000000,0.0000000,-1,-1);
Could i actually do:

pawn Код:
if(vehicleID == PoliceCars[50])...etc
Thank you for explaining it!

EDIT:

pawn Код:
if(newstate == PLAYER_STATE_DRIVER)
    {
        for(new i=0;i<sizeof(PoliceCars);i++)
        {
            if(vehicleID == PoliceCars[i])
            {
                if(PlayerInfo[playerid][pFac] != 1)
                {
                    RemovePlayerFromVehicle(playerid);
                    SendClientMessage(playerid, COLOR_RED, "You have to be a Police officer to drive this vehicle!");
                }
            }
        }
    }
I figure what I just writ wouldn't loop through all the vehicles, but only 44th item in the array. So if I create a loop, to loop through the array and then check if the player is in one of those vehicles, will that work?
Reply
#4

Another interesting way to do a comparison to multiple values is this:
PHP код:
static const valid_vehid = (1<<596) | (1<<597) | (1<<598) | (1<<599) | (1<<523);//makes bit mask of valid ids
//and do 
if((1<<vehicleID)&valid_vehid )
{
//true
}
else
{
//not

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)