[Tutorial] [TUT] How to create modded cars in your script!
#1

**Note: I know this Tut exists already, however i would like to write my own version because i didn't like i saw from other people.**

Introduction
This tutorial will show you how to spawn already modded cars into your script. This will mean that you can have a car that is already modded from the transfenders without you having to go do it yourself all the time. This is the result you will get from this tutorial:



This vehicle is fully modded with a paint job, spoiler, rims, roof, front bumper, back bumper. These modifications are from the mod shop in SF although they automatically go onto the car when the gamemode starts.

Part 1 - Adding the vehicle to begin with..
Okay, we will be making the car that i showed above so i will start of by going in game, using the /save command while in that position with my elegy (the vehicle i used). The co-ords i have gotten for my vehicle are this (after i changed it a bit to AddStaticvehicleEx):

Code:
AddStaticVehicleEx(562,1361.9299,-1660.2322,13.0449,317.4974,1,1,600000);
This car in game will just be a basic elegy that is white with no mods on it at all. In order for us to add the mods to this vehicle we want to define what the vehicle is called (in the script). So at the top of your script you need to add this:

Code:
new moddedCar;
This is a new variable which will store the ID of the vehicle you just added! (in basic terms..it is what your vehicle is called). Now you must add this to yuor AddStaticVehicleEx code:

Code:
moddedCar = AddStaticVehicleEx(562,1361.9299,-1660.2322,13.0449,317.4974,1,1,600000);
This tells the script that the vehicle that we have added is called 'moddedCar'. Now we can use this name later on in the script.

Part 2 - Adding the components..
Okay, so now we have given the car a name in the script. The next thing we need to do is give this vehicle some "components". A component is just a car part, such as a spoiler, front bumper, NOS e.t.c. To add these it is really simple, you just need to use one function called:

Code:
AddVehicleComponent(carID, ComponentID);
The carID is the ID of the vehicle we want to give the component to (the name of the vehicle, in this case "moddedCar". The componentID is the ID of the car part which you wish to give to your vehicle. A list of component ID's is found here: https://sampwiki.blast.hk/wiki/Car_Component_ID. For this example i am going to be using these:

Code:
 
  	AddVehicleComponent(moddedCar, 1035); // roof
  	AddVehicleComponent(moddedCar, 1079); // rim 
  	AddVehicleComponent(moddedCar, 1037); // exhaust 
  	AddVehicleComponent(moddedCar, 1039); // side 
  	AddVehicleComponent(moddedCar, 1172); // front 
  	AddVehicleComponent(moddedCar, 1148); // back 
  	AddVehicleComponent(moddedCar, 1146); // Spoiler
This will give us all of our components we need. However we also need a paint job. So we use a different function for this called "ChangeVehiclePaintjob(carid,paintjobid);".. the Id we are using is either 0,1 or 2. In this example i am using 0. So i will add this to the TOP of the list of components. We now have this:

Code:
  	ChangeVehiclePaintjob(moddedCar,0); // paint job 
  	AddVehicleComponent(moddedCar, 1035); // roof
  	AddVehicleComponent(moddedCar, 1079); // rim 
  	AddVehicleComponent(moddedCar, 1037); // exhaust 
  	AddVehicleComponent(moddedCar, 1039); // side 
  	AddVehicleComponent(moddedCar, 1172); // front 
  	AddVehicleComponent(moddedCar, 1148); // back 
  	AddVehicleComponent(moddedCar, 1146); // Spoiler
We are going to add these lines of code to the Bottom of "OnGameModeInit"

Part 3 - Adding them to OnVehicleSpawn..
Okay, so now if you test your GM the vehicle should work perfectly and you have your vehicle. However if you crash your car and it respawns. The mod's will be gone. This is because the vehicle components was added when the Gamemode initiated. so therefore they wont add again if your car explodes and has to respawn. In order to fix this we use a calledback (a public) called OnVehicleSpawn. It passes one parameter through which is the car id.. I will show you my OnVehicleSpawn callback and you can just copy and paste it somewhere in your code..Although you MUST remember that if yuo add a componenent on the ongamemodeinit section, you must add it here.

Code:
public OnVehicleSpawn(vehicleid)
{
  if(vehicleid == moddedCar){
  	ChangeVehiclePaintjob(moddedCar,0); // paint job
  	AddVehicleComponent(moddedCar, 1035); // roof 
  	AddVehicleComponent(moddedCar, 1079); // rim 
  	AddVehicleComponent(moddedCar, 1037); // exhaust
  	AddVehicleComponent(moddedCar, 1039); // side
  	AddVehicleComponent(moddedCar, 1172); // front
  	AddVehicleComponent(moddedCar, 1148); // back 
  	AddVehicleComponent(moddedCar, 1146); // Spoiler
  }
  return 1;
}
Part 4 - Finished..
Well that is it. We are finished. Your vehicle should now be modded when you go in game. The only thing i would like to remind everyone that when you choose your component ID, make sure that it is for the correct vehicle otherwise you may run into some problems.

If there is any mistakes in this tut or i have not made sense on a section, feel free to reply to this thread or PM me....Now i am off to watch Russel Howards good news

-Darren
Reply
#2

really nice bro i like your idea
Reply
#3

Nice tutorial
Reply
#4

cool, thanks
Reply
#5

nice
Reply
#6

Can you give me an idea of how to do this for cars that are loaded up via an array like MAX_VEH it loops that and goes through all the vehicles in a file and loads them up. Basically, I want it so when you mod your car In Game it will save to the userfile for the vehicle and load up when the server starts.
Reply
#7

Quote:
Originally Posted by MisterTickle
View Post
Can you give me an idea of how to do this for cars that are loaded up via an array like MAX_VEH it loops that and goes through all the vehicles in a file and loads them up. Basically, I want it so when you mod your car In Game it will save to the userfile for the vehicle and load up when the server starts.
Hey...this is a bit late, but the solution will help if anyone else searches for this thread and has the same problem...

Basicly, the way i would do it is have an array made like...

pawn Code:
new carIDs[99]; // 99 being how many vehicles are loaded in the file...
Then when you load the vehicles.. i asume you use a loop of some sort? so you use

pawn Code:
carIDs[i] = CreateVehicle(); // i being the variable you use to loop....
Now the vehicle id is stored in carIDs[]...


Hope that makes sense..
Reply
#8

Nice tutorial.
Reply
#9

I think its easier if you map them :P
Reply
#10

Nice tutorial.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)