# Baking and code generation

### Baking Video Tutorial

{% embed url="<https://www.youtube.com/watch?v=_AqX5UUBHzI&ab_channel=coherence>" %}

### Overview

Out of the box, **coherence** will use *C# Reflection* to sync all the data at runtime. This is a great way to get started but it is very costly performance-wise.

For optimal runtime performance, 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/0.5.2/overview/how-does-coherence-work) section.

**coherence** offers an automatic way of doing that called **baking.**

### Bake schemas

![](https://1930722764-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-MdvIEvGay8wwa18QPuE%2F-MdvJsj9Ec8qrlPjXuc-%2FScreenshot%202021-07-06%20at%2012.18.10.png?alt=media\&token=a3cf3324-1a68-44c0-95d5-e293bc127f9e)

Click on `coherence -> Schema and Baking -> Bake Schemas`.

This will go through all `CoherenceSync` components in the project and generate a schema file based on the selected variables, commands and other settings. It will also take into account any `CoherenceArchetype` components.

For every prefab with a `CoherenceSync` object, the baking process will generate a bespoke C# file in the `coherence/baked` folder in the project.&#x20;

Adding that file to the prefab will make that prefab use bespoke generated code instead of C# reflection.

### Activate baked schema on prefab

Once the Schema has been baked, you will be able to switch to baked mode in the CoherenceSync inspector.

![](https://1930722764-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWd0ZPEK7vE9nkE0b7G-1552174847%2Fuploads%2FsfAzgXduSOZi6Qk78DPQ%2Fimage.png?alt=media\&token=4af97fc7-0ee4-41b4-a238-26fb37fef7da)

The name of the baked script will be `CoherenceSync[prefabName]`.

![](https://1930722764-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-MYvSkLFiquVG19Wn1e7%2F-MYvU4ONphLnSziyJAO-%2Fimage.png?alt=media\&token=4a0af6e2-ebe9-4371-87d7-c20d03a3d24e)

### Modifying bound data, safe mode and the watchdog

When you bind to your script's fields and bake, coherence generates specific code that accesses your code directly, without using reflection. This means, whenever you change your scripts, you might break compilation.

For example, if you have a `Health.cs` script which exposes a `public float health;` field, and you mark `health` as a binding in the Bindings window, the baked script will access your component via type name, and your field via field name.&#x20;

Your baked script might now reference your component:

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

{% hint style="info" %}
Baked scripts reside by default in `Assets/coherence/baked`, but you can check where exactly they're located in the settings window.&#x20;
{% endhint %}

This means that if you decide to change your component name (`Health`) or your any of your bound field names (`health`), Unity script recompilation will fail. In this example, we will be deprecating `health` and adding `health2` in its place.

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

Our watchdog is able to understand when this happens, and offer you a solution right away.

![](https://1930722764-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-Me0dMRJUAzTq6Pw3EeV%2F-Me0he72uzUElYk8-IJK%2FScreenshot%202021-07-07%20at%2018.04.18.png?alt=media\&token=5385510c-aa39-4d36-90f1-995dd77731b0)

It will suggest you to bake in safe mode, and then diagnose the state of your prefabs. After a few seconds of script recompilation, you'll be presented with the diagnosis window.

![](https://1930722764-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-Me0dMRJUAzTq6Pw3EeV%2F-Me0iIYcpseYzGmxMCS1%2FScreenshot%202021-07-07%20at%2018.06.43.png?alt=media\&token=62902384-8b64-43f9-ae5f-a378c29c8b69)

{% hint style="info" %}
You can enter safe mode manually via `coherence > Schema and Baking > Bake Schemas (Safe Mode)`.
{% endhint %}

In this window, you can easily spot bindings in your prefabs that are no longer valid. In our example, health is no longer valid since we've moved it elsewhere (or deleted it).

Click on the hand pointing button to open the bindings window, and take a look at your script:

![](https://1930722764-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-Me0dMRJUAzTq6Pw3EeV%2F-Me0ivF60p_p2oCJzMoG%2FScreenshot%202021-07-07%20at%2018.08.51.png?alt=media\&token=0ded4272-990e-4056-a39a-fbdb783988be)

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

{% hint style="info" %}
Baking in safe mode creates scripts that will help avoid compilation errors, but prefabs that use these will not work in runtime. Remember to bake again normally when you're done fixing your prefabs.
{% endhint %}
