---
title: "Replan a path when the map changes in C#"
url: https://recursiveai.org/onepath/cookbook/replanning-and-flow-fields/
description: "Replan a path in C# when the map changes, without searching from scratch: D* Lite keeps its search and repairs only what an edit affected. Runnable code, with OnePath."
version: "1.1.0"
---

# Replan a path when the map changes in C#

Create a replanner once with `grid.CreateReplanner(start, goal)` and call `Replan()` after each change to the map or each move of the unit. It runs D* Lite, which keeps its search state and repairs only what changed. The program below opens a yard, drops a wall across it, and moves the unit along.

```shell
dotnet new console -o Replan && cd Replan
dotnet add package RecursiveAI.OnePath --version 1.1.0
```

Put this in `Program.cs` and run `dotnet run`:

```csharp
using RecursiveAI.OnePath;

var yard = new Grid(16, 8);
using GridReplanner replanner = yard.CreateReplanner(new Cell(0, 0), new Cell(15, 0));
GridPath first = replanner.Replan();
Console.WriteLine($"open yard:   cost {first.Cost:F4}, {first.Cells.Length} cells");

yard.SetRect(7, 0, 1, 7, AreaCosts.Wall);   // a wall appears, open only at the bottom row
GridPath detour = replanner.Replan();
Console.WriteLine($"after wall:  cost {detour.Cost:F4}, {detour.Cells.Length} cells");

replanner.MoveStart(new Cell(4, 5));        // the unit has moved along
GridPath onward = replanner.Replan();
Console.WriteLine($"moved start: cost {onward.Cost:F4}, {onward.Cells.Length} cells");
```

```text
open yard:   cost 15.0000, 16 cells
after wall:  cost 21.3848, 17 cells
moved start: cost 14.7279, 12 cells
```

## The replanner

`grid.CreateReplanner(start, goal)` keeps its search state between calls (D\* Lite by default; LPA\* is the other choice). After an edit to the grid, `Replan()` repairs only what the edit affected, instead of searching again from scratch. `MoveStart` follows the unit as it walks, and the next `Replan()` reuses everything still valid.

## Limits

- **One start and one goal per replanner.** For many units heading to one place, a single [flow field](https://recursiveai.org/onepath/cookbook/flow-fields/) is cheaper.
- **It saves the most when edits are small and local.** Repair work grows with how much of the search an edit touches.
- **Dispose it** (`IDisposable`) when the unit or the goal goes away. It hears about edits to the grid it came from; its state saves to bytes and restores exactly, like grids and searches; see [saved search state](https://recursiveai.org/onepath/docs/grids-state/).
