[Tutorial] Vehicle System "Engine & Lights".
#1

Let's start with making a loop throuth all vehicles in

pawn Code:
for(new i; i < MAX_VEHICLES; i++) // This is the loop if i is greater then MAX VEHICLES it will continue.
    {
        new engine, lights, alarm, doors, bonnet, boot, objective; // Required variables.
        engine = 0; // 0 means OFF, so when the server start's all car's are off.
        lights = 0; // Same as engine.
        SetVehicleParamsEx(i,engine,lights,alarm,doors,bonnet,boot,objective);
    }
    return 1;
// Put the code above at end of InGameInit().
Now all we got to do is when we press 2 while we are inside a car so we turn the engine and lights on.
pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys) // This is when the player taps a button in keyboard.
{
    if(newkeys & KEY_SUBMISSION) Number 2 or + in your keyboard.
    {
        if(IsPlayerInAnyVehicle(playerid)) // If the player is in any vehicle (I should have done this to state driver but anyways).
        {
            new engine, lights, alarm, doors, bonnet, boot, objective; // Variables to store the status.
            new vid = GetPlayerVehicleID(playerid); // We need to get players vehicle to get the params.
            GetVehicleParamsEx(vid, engine, lights, alarm, doors, bonnet, boot, objective); // We need to get vehicle params in order to change them.
            if(engine == 0) // If engine is OFF the code below turns on the engine.
            {
                SetVehicleParamsEx(vid,1,1,alarm,doors,bonnet,boot,objective);
                SendClientMessage(playerid,-1,"You turned ON your vehicle engine and lights.");
                return 1;
            }
            else if(engine == 1) // If the engine is ON so we turn it off.
            {
                SetVehicleParamsEx(vid,0,0,alarm,doors,bonnet,boot,objective);
                SendClientMessage(playerid,-1,"You turned OFF your vehicle engine and lights.");
                return 1;
            }
            else if(engine == -1) // If the params are unset let's take a example when you spawn a car as admin.
            {
                SetVehicleParamsEx(vid,1,1,alarm,doors,bonnet,boot,objective);
            }
        }
    }
    return 1;
}
Reply
#2

This is not a tutorial
Reply
#3

Well COPY & Paste for beginners.
Reply
#4

Well, my compiler isn't responding after i put this code.
Why?
Reply
#5

@MrBlake:
Cuz
Quote:

if(newkeys & KEY_SUBMISSION) Number 2 or + in your keyboard.

needs to be
Quote:

if(newkeys & KEY_SUBMISSION) //Number 2 or + in your keyboard.

Reply
#6

Would be better if you use ManualVehicleEngineAndLights
Reply
#7

Quote:
Originally Posted by LivingLikeYouDo
View Post
@MrBlake:
Cuz

needs to be
I did that, not my problem.
Reply
#8

In that for-loop, shouldn't you check if the vehicle exists first before setting parameters on it?
You "might" get unexpected results if you're trying to set vehicleparameters on a non-existing vehicle.

Also, use GetVehicleParamsEx first, as some people may create vehicles and set objective to 1 for example when the server starts.
Setting all default values would overwrite all those settings.

Finally, place "new engine, lights, alarm, doors, bonnet, boot, objective; // Required variables." outside the loop.
It has no use to re-create the variables 2000 times (for each iteration of the loop).
Just create them once, then enter the loop.

pawn Code:
new engine, lights, alarm, doors, bonnet, boot, objective; // Required variables.
for(new i; i < MAX_VEHICLES; i++) // This is the loop if i is smaller then MAX VEHICLES it will continue.
{
    if (GetVehicleModel(i) != 0) // Check if the vehicle exists
    {
        GetVehicleParamsEx(i, engine, lights, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(i, 0, 0, alarm, doors, bonnet, boot, objective); // Keep all parameters, but turn engine and lights off
    }
}
return 1;
// Put the code above at end of InGameInit().
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)