# 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.

Learn more about this in the [How does coherence work](https://docs.coherence.io/1.0/overview/how-does-coherence-work) section.

**coherence** calls this mechanism **baking.**

### Baking

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.0/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.0/coherence-sdk-for-unity/settings-window).

### Two bake modes

**coherence** offers two mechanisms to generate baked scripts: through Assets or through a Source Generator. By default, coherence baked using Assets, but you can change this setting anytime in the **coherence** *Settings* window.

![Bake modes in the coherence settings window](https://352971571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOnNWMLfYsbCzCnIbNrcP%2Fuploads%2F27ij5IXvOx4cGhGjz7W7%2FScreenshot%202022-07-07%20at%2010.57.45.png?alt=media\&token=275e7637-f94d-4f3e-93bc-e050a1021d65)

#### Bake mode: Assets

When you bake using Assets, the generated code will output to *Asset/coherence/baked*. This is a simple solution, allowing for an easy inspection and debugging of the generated files, but it comes with a few drawbacks:

* You should version the baked files, which can clutter your VCS workflow.
* Since baked scripts access your code, changing your code will get you into compilation errors.

#### Bake mode: Source Generator

When you bake using the Source Generator, the generated code is fed directly to the compilation pipeline, not generating the files in the Assets folder. In this process, **coherence** can analyze your code syntactically and semantically. This means it can detect cases where changes in your code can affect the baked files, anticipating compiler errors and avoiding them altogether. This mode comes with a few drawbacks too:

* File `Assets/coherence/Footprint.cs` is created/updated every bake operation, to trigger a recompile on your code. This file (and its `.meta`) can be ignored in your VCS.
* A bake operation is performed on every recompile. For most projects this is not noticeable. But if your project is heavy or your computer is slow, this can take additional time on top of the normal recompilation time.
* Since baked files are not versioned and have to be generated, the protocol code generator (executable bundled with the SDK package) needs to keep its execute permissions and should have write permission on `<project>/Library/coherence`. This is usually not a problem, except in continuous integration scenarios, where there might be strict rules on files having the execute permission.
* Harder to debug. Since files are not in Assets anymore, you can't click on them and have proper code completion. The last generation made through the Source Generator is available in `Library/coherence/LastBake`.

As you can see, there are pros and cons to each mechanism, so we recommend you try both and check what works best for your workflow.

{% hint style="danger" %}
Source generators do not work on Unity version 2021.1. This is a known Unity issue that has no other fix than to upgrade (or downgrade) the version.
{% endhint %}

### Using the Baked Script in your Prefab

Once the baked scripts have been generated, you can make use of it by ticking the checkbox **Baked** in the CoherenceSync inspector. This is on by default.

<figure><img src="https://352971571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOnNWMLfYsbCzCnIbNrcP%2Fuploads%2FngXyae39yprFxXT2k05f%2FCoherenceSync_Baked.png?alt=media&#x26;token=29b4c141-8b0e-4614-bea1-09d084674d7a" alt=""><figcaption></figcaption></figure>

### Modifying bound data, safe mode and 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" %}
When baking via assets, baked scripts will be located in *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://352971571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOnNWMLfYsbCzCnIbNrcP%2Fuploads%2Fyu1PZLdqqTro5df8DCdw%2FScreenshot%202022-07-06%20at%2013.13.32.png?alt=media\&token=b2d7a436-90c4-4c5b-a154-8e1b562ac4da)

{% hint style="info" %}
You can delete the baked folder manually through the **coherence** *Settings* window.
{% endhint %}

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://352971571-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOnNWMLfYsbCzCnIbNrcP%2Fuploads%2F8BqQIpL7ktYcDx9AtTQj%2FScreenshot%202021-07-07%20at%2018.08.51.png?alt=media)

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 %}
