[Tutorial] How to make simple vehicle lights system
#1

Selamat Pagi Semuanya (Good Morning All)
Today i be showing you guys an tutorial to make simple vehicle lights system.
I dunno if this tutorial will helpful or not, but i want to help you guys if you don't know about it xD

So, the first thing you need to do is add ManualVehicleEngineAndLights(); in OnGameModeInit for example like this :

PHP Code:
public OnGameModeInit()
{
  
ManualVehicleEngineAndLights();
  
//another function
  
return 1;

Why must we add ManualVehicleEngineAndLights(); ?
That's function make every vehicle (except bicycle), turn on engine & lights manually.
Done? Not yet, now you need to make a function to turn on the lights.

Code:
forward VehicleLights(playerid, vehicleid);
PHP Code:
public VehicleLights(playeridvehicleid)
{
  return 
1;

What function should we add? Ok, first thing you need to do is GetVehicleParamsEx, and also an variable example like this :

PHP Code:
public VehicleLights(playeridvehicleid)
{
  new 
enginelightsalarmdoorsbonnetbootobjective;
  
GetVehicleParamsEx(vehicleidenginelightsalarmdoorsbonnetbootobjective);
  return 
1;

So what is GetVehicleParamsEx is? A function to check vehicle information like engine, lights, alarm, doors any many more. After you GetVehicleParamsEx, you need to check if the vehicle turn on / not.

PHP Code:
public VehicleLights(playeridvehicleid)
{
  new 
enginelightsalarmdoorsbonnetbootobjective;
  
GetVehicleParamsEx(vehicleidenginelightsalarmdoorsbonnetbootobjective);
  if(
lights == VEHICLE_PARAMS_ON)
  {
  }
  else if(
lights == VEHICLE_PARAMS_OFF || lights == VEHICLE_PARAMS_UNSET)
  {
    
  }
  return 
1;

VEHICLE_PARAMS_ON it's mean if the lights turn on, also VEHICLE_PARAMS_OFF it's mean if the lights turn off. What about the VEHICLE_PARAMS_UNSET? that mean if the lights is not set.
Now we need to add an function to turn on / off the lights with SetVehicleParamsEx.

PHP Code:
public VehicleLights(playeridvehicleid)
{
  new 
enginelightsalarmdoorsbonnetbootobjective;
  
GetVehicleParamsEx(vehicleidenginelightsalarmdoorsbonnetbootobjective);
  if(
lights == VEHICLE_PARAMS_ON)
  {
    
SetVehicleParamsEx(vehicleidengineVEHICLE_PARAMS_OFFalarmdoorsbonnetbootobjective);
    
SendClientMessage(playeridCOLOR_LIGHTYELLOW"Vehicle light turned off.");
  }
  else if(
lights == VEHICLE_PARAMS_OFF || lights == VEHICLE_PARAMS_UNSET)
  {
    
SetVehicleParamsEx(vehicleidengineVEHICLE_PARAMS_ONalarmdoorsbonnetbootobjective);
    
SendClientMessage(playeridCOLOR_LIGHTYELLOW"Vehicle light turned on.");
  }
  return 
1;

Now the CMD to turn on / off the lights, before turning the lights, we need to check the player in vehicle and also check the vehicle information too.

PHP Code:
CMD:lights(playeridparams[])
{
    new 
vehicleid GetPlayerVehicleID(playerid);
    new 
enginelightsalarmdoorsbonnetbootobjective;
    
GetVehicleParamsEx(vehicleidenginelightsalarmdoorsbonnetbootobjective);
    if(
IsPlayerInAnyVehicle(playerid))
    {
        
    }
    else
    {
        
SendClientMessage(playeridCOLOR_GREY"You are not in any vehicle.");
    }
    return 
1;

Almost finish, now you need add a function when player in vehicle. But 1 problem is, bicycle doesn't have lights. So we need to check are the player is not using bike.

PHP Code:
CMD:lights(playeridparams[])
{
    new 
vehicleid GetPlayerVehicleID(playerid);
    new 
enginelightsalarmdoorsbonnetbootobjective;
    
GetVehicleParamsEx(vehicleidenginelightsalarmdoorsbonnetbootobjective);
    if(
IsPlayerInAnyVehicle(playerid))
    {
        if(
GetVehicleModel(vehicleid) != 481 || GetVehicleModel(vehicleid) != 509 || GetVehicleModel(vehicleid) != 510)
        {
        }
    }
    else
    {
        
SendClientMessage(playeridCOLOR_GREY"You are not in any vehicle.");
    }
    return 
1;

What is 481, 509, and 510? It's a BMX, Mountain Bike and Bike they all don't have lights.
After check the vehicle model, time to add the VehicleLights function.

PHP Code:
CMD:lights(playeridparams[])
{
    new 
vehicleid GetPlayerVehicleID(playerid);
    new 
enginelightsalarmdoorsbonnetbootobjective;
    
GetVehicleParamsEx(vehicleidenginelightsalarmdoorsbonnetbootobjective);
    if(
IsPlayerInAnyVehicle(playerid))
    {
        if(
GetVehicleModel(vehicleid) != 481 || GetVehicleModel(vehicleid) != 509 || GetVehicleModel(vehicleid) != 510)
        {
            
VehicleLights(playeridvehicleid);
        }
    }
    else
    {
        
SendClientMessage(playeridCOLOR_GREY"You are not in any vehicle.");
    }
    return 
1;

Don't forget to add and clientmessage if player using bike.

PHP Code:
CMD:lights(playeridparams[])
{
    new 
vehicleid GetPlayerVehicleID(playerid);
    new 
enginelightsalarmdoorsbonnetbootobjective;
    
GetVehicleParamsEx(vehicleidenginelightsalarmdoorsbonnetbootobjective);
    if(
IsPlayerInAnyVehicle(playerid))
    {
        if(
GetVehicleModel(vehicleid) != 481 || GetVehicleModel(vehicleid) != 509 || GetVehicleModel(vehicleid) != 510)
        {
            
VehicleLights(playeridvehicleid);
        }
        else
        {
            
SendClientMessage(playeridCOLOR_GREY"This vehicle doesn't have lights.");
        }
    }
    else
    {
        
SendClientMessage(playeridCOLOR_GREY"You are not in any vehicle.");
    }
    return 
1;

Ok, maybe just that it's really simple. I dunno if this a tutorial or not, but i hope this will help you. Thanks
Reply
#2

I came here for a tutorial, but what i got, was copy & paste ready code.
Reply
#3

Quote:
Originally Posted by [Bios]Marcel
View Post
I came here for a tutorial, but what i got, was copy & paste ready code.



OT: nice work
Reply
#4

Quote:
Originally Posted by [Bios]Marcel
View Post
I came here for a tutorial, but what i got, was copy & paste ready code.
Oh sorry, updated the tutorial
Reply
#5

Nice.

Edit: A one thing popping me is that we already created new variables engine, etc.. to get info about the player vehicle. I didn't understand why you created the variables again and checked the information about a vehicle? Plus I think it can be done just by calling function inside of if else statement without checking vehicle information again inside of command, Why you created function I think it can also be done by stock
Reply
#6

Quote:
Originally Posted by BlackbirdXd
View Post
Nice.
Thanks
Reply
#7

why the function VehicleLights public?
Reply
#8

Quote:
Originally Posted by Sreyas
View Post
why the function VehicleLights public?
Making the function can be use on any classes, i don't really understand what public is.
Reply
#9

Quote:
Originally Posted by haikalbintang
View Post
i don't really understand what public is.
Means you shouldn't be writing tutorials at this stage. Knowing when to (not) use public is a quite fundamental aspect.
Reply
#10

Quote:
Originally Posted by haikalbintang
View Post
Making the function can be use on any classes, i don't really understand what public is.
you should read and understand basics before writing tutorials because you are teaching others.
https://sampwiki.blast.hk/wiki/Public_functions
Reply
#11

Quote:
Originally Posted by Sreyas
View Post
you should read and understand basics before writing tutorials because you are teaching others.
https://sampwiki.blast.hk/wiki/Public_functions
Ok, really sorry about that
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)