[Tutorial] Locking Vehicles - Custom Way
#1

Since I had some troubles with "SetVehicleParamsForPlayer" or other SetVehicleParams function I decided to make my own easy-to-do locking system, which works perfectly so far, this is my first tutorial, don't be rude :P please tell me if I have forgot something

if you want to test if it works, just change this line
pawn Код:
if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid)
to this one
pawn Код:
if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] == vehicleid)
after that just lock vehicle, go out and try to enter

Global Variables
First of all we have to create global variables that will store our data for every player and vehicle (every player can own only one vehicle) and text variable for TextDraw, 2 more variables we will be using later and function to hide TextDraw

pawn Код:
new PlayerCar[MAX_PLAYERS], LockedCar[1000];
new Text:Locked;
new MsgLocked[MAX_PLAYERS], LockedTimer[MAX_PLAYERS];
forward RemoveLock(playerid);
change length of LockedCar to amount of cars you are using or just some high number to be sure you can lock any car

We have to set PlayerCar[playerid] to -1 when player connects, this will tell our server that this player don't have any locked cars
pawn Код:
public OnPlayerConnect(playerid)
{
    PlayerCar[playerid] = -1;
    return 1;
}
Commands
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/lock", true))
    {
        if(!IsPlayerInAnyVehicle(playerid))
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle."); // player has to be in a vehicle
        if(GetPlayerVehicleSeat(playerid) != 0)
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver."); // player has to be a driver
        if(PlayerCar[playerid] != -1)
        {
            LockedCar[PlayerCar[playerid]] = 0; // if player has any locked vehicle it will unlock it, allowing him to lock current vehicle - every player can have only one locked vehicle
        }
        PlayerCar[playerid] = GetPlayerVehicleID(playerid); // bounds vehicle to a player
        LockedCar[GetPlayerVehicleID(playerid)] = 1; // locks vehicle
        return 1;
    }
    if(!strcmp(cmdtext, "/unlock", true))
    {
        if(!IsPlayerInAnyVehicle(playerid))
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle.");
        if(GetPlayerVehicleSeat(playerid) != 0)
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver.");
        PlayerCar[playerid] = -1; // tells server that this player don't have any locked vehicle
        LockedCar[GetPlayerVehicleID(playerid)] = 0; // unlocks current vehicle
        return 1;
    }
    return 0;
}
pretty much self-explanatory

now for the main part;

Checking vehicle Lock
pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid) // if player is trying to enter locked vehicle and he is not bound to that vehicle
    {
        if(MsgLocked[playerid] == 1) // if there's already message showing for player
        {
            TextDrawHideForPlayer(playerid, Locked); // hides message
            MsgLocked[playerid] = 0; // there's no more any message regarding locked car
            KillTimer(LockedTimer[playerid]); // kills timer
        }
        TogglePlayerControllable(playerid, true); // stops player from entering vehicle (it's not his vehicle, why should he enter it?)
        Locked = TextDrawCreate(10.0, 250.0, "This Vehicle is Locked"); // pretty much self-explanatory
        TextDrawUseBox(Locked, 1);
        TextDrawBoxColor(Locked, 0x00000055);
        TextDrawTextSize(Locked, 125, 5);
        TextDrawShowForPlayer(playerid, Locked); // shows TextDraw to player
        LockedTimer[playerid] = SetTimer("RemoveLock", 4000, false); // sets timer for hiding TextDraw
        MsgLocked[playerid] = 1; // we have TextDraw showing
    }
    return 1;
}

public RemoveLock(playerid)
{
    if(MsgLocked[playerid] == 1) // if there's message showing for player
    {
        TextDrawHideForPlayer(playerid, Locked); // hides message
        MsgLocked[playerid] = 0; // there's no more any message regarding locked car
    }
}

public OnVehicleSpawn(vehicleid)
{
    LockedCar[vehicleid] = 0; // when vehicle has respawned (nobody has entered it for a while) server will unlock it
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerCar[i] == vehicleid)
            PlayerCar[i] = -1; // unbounds player from this vehicle so anyone can enter it
    }
    return 1;
}
so our Lock system looks like this:

Final Version

pawn Код:
new PlayerCar[MAX_PLAYERS], LockedCar[1000];
new Text:Locked;
new MsgLocked[MAX_PLAYERS], LockedTimer[MAX_PLAYERS];
forward RemoveLock(playerid);

public OnPlayerConnect(playerid)
{
    PlayerCar[playerid] = -1;
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/lock", true))
    {
        if(!IsPlayerInAnyVehicle(playerid))
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle."); // player has to be in a vehicle
        if(GetPlayerVehicleSeat(playerid) != 0)
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver."); // player has to be a driver
        if(PlayerCar[playerid] != -1)
        {
            LockedCar[PlayerCar[playerid]] = 0; // if player has any locked vehicle it will unlock it, allowing him to lock current vehicle - every player can have only one locked vehicle
        }
        PlayerCar[playerid] = GetPlayerVehicleID(playerid); // bounds vehicle to a player
        LockedCar[GetPlayerVehicleID(playerid)] = 1; // locks vehicle
        return 1;
    }
    if(!strcmp(cmdtext, "/unlock", true))
    {
        if(!IsPlayerInAnyVehicle(playerid))
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be inside a vehicle.");
        if(GetPlayerVehicleSeat(playerid) != 0)
            return SendClientMessage(playerid,0xFFFFFFAA,"You have to be a driver.");
        PlayerCar[playerid] = -1; // tells server that this player don't have any locked vehicle
        LockedCar[GetPlayerVehicleID(playerid)] = 0; // unlocks current vehicle
        return 1;
    }
    return 0;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(LockedCar[vehicleid] == 1 && PlayerCar[playerid] != vehicleid) // if player is trying to enter locked vehicle and he is not bound to that vehicle
    {
        if(MsgLocked[playerid] == 1) // if there's already message showing for player
        {
            TextDrawHideForPlayer(playerid, Locked); // hides message
            MsgLocked[playerid] = 0; // there's no more any message regarding locked car
            KillTimer(LockedTimer[playerid]); // kills timer
        }
        TogglePlayerControllable(playerid, true); // stops player from entering vehicle (it's not his vehicle, why should he enter it?)
        Locked = TextDrawCreate(10.0, 250.0, "This Vehicle is Locked"); // pretty much self-explanatory
        TextDrawUseBox(Locked, 1);
        TextDrawBoxColor(Locked, 0x00000055);
        TextDrawTextSize(Locked, 125, 5);
        TextDrawShowForPlayer(playerid, Locked); // shows TextDraw to player
        LockedTimer[playerid] = SetTimer("RemoveLock", 4000, false); // sets timer for hiding TextDraw
        MsgLocked[playerid] = 1; // we have TextDraw showing
    }
    return 1;
}

public RemoveLock(playerid)
{
    if(MsgLocked[playerid] == 1) // if there's message showing for player
    {
        TextDrawHideForPlayer(playerid, Locked); // hides message
        MsgLocked[playerid] = 0; // there's no more any message regarding locked car
    }
}

public OnVehicleSpawn(vehicleid)
{
    LockedCar[vehicleid] = 0; // when vehicle has respawned (nobody has entered it for a while) server will unlock it
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(PlayerCar[i] == vehicleid)
            PlayerCar[i] = -1; // unbounds player from this vehicle so anyone can enter it
    }
    return 1;
}
Reply


Messages In This Thread
Locking Vehicles - Custom Way - by SEnergy - 28.07.2012, 15:45
Re: Locking Vehicles - Custom Way - by CoDeZ - 28.07.2012, 15:49
Re: Locking Vehicles - Custom Way - by Tuntun - 28.07.2012, 16:59
Re: Locking Vehicles - Custom Way - by SEnergy - 28.07.2012, 17:04
Re: Locking Vehicles - Custom Way - by Tuntun - 28.07.2012, 17:16
Re: Locking Vehicles - Custom Way - by SEnergy - 28.07.2012, 17:17
Re: Locking Vehicles - Custom Way - by Tuntun - 28.07.2012, 17:33
Re: Locking Vehicles - Custom Way - by SomebodyAndMe - 28.07.2012, 17:45
Re: Locking Vehicles - Custom Way - by SEnergy - 28.07.2012, 18:01
Re: Locking Vehicles - Custom Way - by Jakku - 28.07.2012, 18:26

Forum Jump:


Users browsing this thread: 2 Guest(s)