# Spawn Waypoint Selector

## Description

Controls the selection of a free waypoint to instantiate a new vehicle on.

## Declaration

```csharp
public delegate int SpawnWaypointSelector(List<Vector2Int> neighbors, Vector3 position, Vector3 direction, VehicleTypes carType, bool useWaypointPriority);
```

## Returns

**int** - a waypoint index

## Parameters

<table><thead><tr><th width="203">Name</th><th>Description</th></tr></thead><tbody><tr><td><strong>neighbors</strong></td><td>A list of the cell indexes that are neighbors to the player cell.</td></tr><tr><td><strong>position</strong></td><td>The player position.</td></tr><tr><td><strong>direction</strong></td><td>The player direction.</td></tr><tr><td><strong>vehicleType</strong></td><td>The vehicle type for which the waypoint is being selected.</td></tr><tr><td><strong>useWaypointPriority</strong></td><td>Specifies whether waypoint priority is enabled for selection or not.</td></tr></tbody></table>

## Set Method

```csharp
public static void SetSpawnWaypointSelector(SpawnWaypointSelector spawnWaypointSelectorDelegate)
```

## Example

```csharp
using Gley.TrafficSystem;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Delegates.SetSpawnWaypointSelector(MyTrafficLightBehaviour);
    }

    public static int MySpawnWaypointSelector(List<Vector2Int> neighbors, Vector3 position, Vector3 direction, VehicleTypes vehicleType, bool useWaypointPriority)
    {
        //select a random cell
        Vector2Int selectedNeighbor = neighbors[Random.Range(0, neighbors.Count)];

        //return a random waypoint from that cell
        return GetPossibleWaypoint(selectedNeighbor, vehicleType, useWaypointPriority);
    }
}
```

{% hint style="info" %}
Default delegate implementation can be found inside **DefaultDelegates.cs**
{% endhint %}
