# Common API Examples

## Runtime Navmesh Baker – Common API Examples

This page shows the most common tasks you’ll perform in game code when working with the **BakerCoordinator**.

***

### Getting a Coordinator

```csharp
// Recommended: reference the one you’ve placed in your scene
var coord = BakerCoordinator.Active;

// Fallback: search the scene (slower, but safe if only one exists)
var coord = FindObjectOfType<BakerCoordinator>();
```

If you expect there might not be one in the scene, you can call `BakerCoordinator.Ensure()` to auto-create one.

***

### Setting the Target to Follow

Tell the baker which object (usually your **player** or **camera**) should act as the bake center:

```csharp
coord.SetCenterTarget(player.transform);
```

* By default this **immediately triggers a bake**.
* You can disable the immediate rebake:

```csharp
coord.SetCenterTarget(player.transform, rebakeNow: false);
```

***

### Starting Baking Manually

If `autoStart` is off (or you want to trigger manually):

```csharp
coord.StartBaking();
```

This will:

* Bake **around the current target** (Continuous / Combined modes).
* Bake the **active grid cells** (Grid mode).

Aliases:

```csharp
coord.BakeNow();     // same as StartBaking()
coord.BakeAllNow();  // force all registered surfaces at once
```

***

### Marking Surfaces Dirty

If you modify geometry at runtime:

```csharp
coord.MarkSurfaceDirty(surface);
```

Optional overloads let you pass bounds, agent type, or priority.

***

### Checking Bake Status

```csharp
Debug.Log($"Queued: {coord.QueuedCount}, In-Flight: {coord.InFlightCount}");

if (coord.TryGetLastCombinedBounds(out var b))
    Debug.Log($"Last baked bounds: {b}");
```

***

### Example: Spawning a Player Dynamically

```csharp
void SpawnPlayer()
{
    var player = Instantiate(playerPrefab);
    var agent = player.GetComponent<UnityEngine.AI.NavMeshAgent>();
    agent.enabled = false; // disable until navmesh is ready

    var coord = BakerCoordinator.Active;
    coord.SetCenterTarget(player.transform);

    // Enable the agent after the first bake completes
    NavMeshBakerService.OnBakeCompleted += OnBakeCompletedOnce;
    void OnBakeCompletedOnce(NavMeshBakerService.BakeResult r)
    {
        agent.enabled = true;
        NavMeshBakerService.OnBakeCompleted -= OnBakeCompletedOnce;
    }
}
```

***

### Quick Reference

* **`SetCenterTarget(Transform, bool)`** → set follow target.
* **`StartBaking()` / `BakeNow()`** → enqueue an initial bake.
* **`BakeAllNow()`** → bake all registered surfaces.
* **`RegisterSurface()` / `UnregisterSurface()`** → manage surfaces.
* **`MarkSurfaceDirty()`** → force rebake of a surface region.
* **`QueuedCount` / `InFlightCount`** → runtime stats.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://megacrush.gitbook.io/megacrush-unity-assets/runtime-navmesh-baker/runtime-navmesh-baker-user-manual/getting-started/common-api-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
