# Baking (code generation)

## Overview

Out of the box, **coherence** can use *C# reflection* to sync data at runtime. This is a great way to get started but is very costly performance-wise and has a number of limitations on what features can be used through this system.

For optimal runtime performance and a complete feature set, we need to **create a schema** and perform **code generation** specific to our project. **coherence** calls this mechanism **Baking.**

{% hint style="info" %}
Learn more about schemas in the [How does coherence work](https://docs.coherence.io/1.2/overview/how-does-coherence-work) section.
{% endhint %}

### How to Bake

Click on the *coherence > Bake* menu item.

This will go through all indexed `CoherenceSync` GameObjects (Resources folders and Prefab Mapper) in the project and generate a schema file based on the selected variables, commands and other settings. It will also take into account any [LODs ](https://docs.coherence.io/1.2/coherence-sdk-for-unity/optimization/level-of-detail)that have been added.

For every Prefab with a `CoherenceSync` component attached, the baking process will generate a C# baked script specifically tuned for it.

## Baking settings

Check [settings](https://docs.coherence.io/1.2/coherence-sdk-for-unity/settings-window).

When baking, the generated code will output to *Asset/coherence/baked*.&#x20;

* You can version the baked files or ignore them, your call.
  * If you work on a larger game or team, where you use continuous integration, chances are you are better off including the baked files on your VCS.
* Since baked scripts access your code, changing networked variables or commands with **coherence** will get you into compilation errors.

## Modifying networked variables or commands - The Watchdog

When you configure your Prefab to network variables, and then bake, **coherence** generates baked scripts that access your code directly, without using reflection. This means that whenever you change your code, you might break compilation by accident.

For example, if you have a `Health.cs` script which exposes a `public float health;` field, and you toggle `health` in the *Configure* window and bake, the generated baked script will access your component via its type, and your field via field name.

Like so:

```csharp
var healthComponent = GetComponent<Health>();
...
var healthField = healthComponent.health;
```

{% hint style="info" %}
Baked scripts will be located in :file\_folder:`Assets/coherence/baked`.
{% endhint %}

If you decide you want to change your component name (`Health`) or any of your bound fields (`health`), Unity script recompilation can fail. In this example, we will be removing `health` and adding `health2` in its place.

```csharp
//public float health;
public float health2;
```

When baking via assets, the watchdog is able to catch compilation problems related with this, and offer you a solution right away.

![](https://content.gitbook.com/content/naCaDgmbFRSw0e7nxNNK/blobs/J5uPq961AQ7h5Wz48WME/Screenshot%202022-07-06%20at%2013.13.32.png)

It will suggest that you delete the baked folder, and then diagnose the state of your Prefabs. After a few seconds of script recompilation, you will be presented with the *Diagnosis* window.

In this window, you can easily spot variables in your Prefabs that can't be resolved properly. In our example, `health` is no longer valid since we've moved it elsewhere (or deleted it).

From here, you can access the *Configure* window, where you can spot the problem.

![](https://content.gitbook.com/content/naCaDgmbFRSw0e7nxNNK/blobs/zOxgYcysWPuGcHQ93QKh/BrokenBindings.png)

Now, we can manually rebind our data: unbind `health` and bind `health2`. Once we do, we can now safely bake again.

{% hint style="info" %}
Remember to bake again after you fix your Prefabs.
{% endhint %}

## Switching a Prefab to reflection mode

Once the baked code has been generated, Prefabs will automatically make use of it. If you want to switch a particular Prefab to reflection code, you can do so in the Inspector of its `CoherenceSync`, by unchecking the **Baked** checkbox:

<figure><img src="https://content.gitbook.com/content/naCaDgmbFRSw0e7nxNNK/blobs/GsEPIiQauHZKbQyYqaT7/BakedCode.png" alt=""><figcaption></figcaption></figure>
