# Vehicle Pool

Each scene requires a Vehicle Pool to work.

A vehicle pool is a collection of all vehicles available on the current scene.

## Create a Vehicle Pool

Right-click on the Project Window -> Create -> Traffic System -> Vehicle Pool

<figure><img src="https://1500817731-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuflU4iIzTBjoLH93zTYD%2Fuploads%2FsDw43kjOAYSHPLRO4udn%2Fv2-CreateVehiclePool.jpg?alt=media&#x26;token=7242ec19-a23a-48e8-89b4-328bdc8a544b" alt=""><figcaption></figcaption></figure>

This will create a scriptable object inside the project folder.

## Add vehicles to the pool

There is no limit for the pool vehicles.

Each entry represents a type of vehicle that will be available on the roads. Each vehicle needs to be configured according to the [vehicle-implementation](https://gley.gitbook.io/mobile-traffic-system/setup-guide/vehicle-implementation "mention")

<figure><img src="https://1500817731-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuflU4iIzTBjoLH93zTYD%2Fuploads%2F3jtX31ntPyTXmc0kHJhQ%2Fv2-VehiclePool.PNG?alt=media&#x26;token=cfc913ca-329d-45cc-95f1-28d7973f2aac" alt=""><figcaption><p>Vehicle Pool</p></figcaption></figure>

If a vehicle type is added to the pool, it ensures that at least one vehicle of that type will be available on the roads, unless the maximum number of vehicles is less than the total number of vehicle types. For example, in the scenario mentioned above, the maximum number of vehicles needs to be equal to or greater than 5.

The **percentage** value represents the proportional allocation of each vehicle type out of the total instantiated vehicles, functioning more like a comparison slider than a true percentage. In the given example, assuming a maximum availability of 20 vehicles, the distribution of vehicles based on the given percentages results in a total 'percentage' value of 10+20+20+5+1 = 56.

It's important to note that 1 vehicle of each type will be instantiated regardless of the percentage specified. Therefore, recalculating the new percentage considering the already instantiated vehicles results in 9+19+19+4+0 = 51, leaving 15 additional cars to be instantiated.

This equates to approximately 51/15=3.4 'percent' per vehicle type, resulting in the instantiation of 9/3.4=2 Truck+Trailer, 5 Car, 5 Sedan, 1 SemiTruck, and an additional 2 vehicles, which might be Car and Sedan, depending on the rounding of the percentage.

The **Don't Instantiate** checkbox prevents the specified type of vehicles from being used by the system. These vehicles will be available but remain disabled by default until specifically activated by the developer. This option proves useful when you want certain types of vehicles available in specific areas of your map or cities. For instance, you might have a construction site where construction vehicles should only appear when the player is near that area.

<figure><img src="https://1500817731-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuflU4iIzTBjoLH93zTYD%2Fuploads%2FcGXSIjz0ZSYGaf0L9OfA%2Fimage_2023-12-20_130355328.png?alt=media&#x26;token=b7bb1435-df0b-4528-9c37-364d3b79fc9e" alt=""><figcaption><p>Don't instantiate option active</p></figcaption></figure>

Activating these vehicles requires coding skills as they can only be enabled using the [API](https://gley.gitbook.io/mobile-traffic-system/complete-api). Below is an example of how to use the API:

<pre class="language-csharp"><code class="lang-csharp">GameObject instantiateLaterVehicle;

//after the traffic system is initialized
<strong>void OnTrafficInitializedCallback()
</strong>{
    //get the complete list of excluded vehicle
    List&#x3C;VehicleComponent> excludedVehicle = API.GetExcludedVehicleList();

    foreach (VehicleComponent vehicleComponent in excludedVehicle)
    {
        if (vehicleComponent.vehicleType == VehicleTypes.SomeType)
        {
            //do something
            //save the vehicle
            //do whatever you want

            //I will save the GamaObject for later use
            instantiateLaterVehicle = vehicleComponent.gameObject;
        }
    }
}

//at the right time instantiate the vehicle using one of those 2 options
void InstantiateExcludedVehicle()
{
    //manually add vehicle to a specified position
    API.AddExcludedVehicle(API.GetExcludedVehicleIndex(instantiateLaterVehicle), position);

    //add the vehicle to the system and it will be automatically instantiated like any other vehicle
    API.AddExcludedVehicleToSystem(API.GetExcludedVehicleIndex(instantiateLaterVehicle));
}
</code></pre>
