Baking and code generation
Baking Video Tutorial
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 section.
coherence offers an automatic way of doing that called baking.
Bake schemas
![](https://docs.coherence.io/~gitbook/image?url=https%3A%2F%2F3580906557-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUrQ8GJkdHxImzMEWuxaz%252Fuploads%252Fgit-blob-8d8ab90df0200fe3ef045437847d89c545116117%252FScreenshot%25202021-12-22%2520at%252017.25.22.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=64bc3fd6&sv=1)
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 LODs
added.
For every prefab with a CoherenceSync
object, the baking process will generate a bespoke C# file in the coherence/baked
folder in the project.
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://docs.coherence.io/~gitbook/image?url=https%3A%2F%2F3580906557-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUrQ8GJkdHxImzMEWuxaz%252Fuploads%252Fgit-blob-522b3552f69196d433f595c21f2eda015ce1cbeb%252FScreenshot%25202021-12-22%2520at%252017.27.03.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=d9a0b62a&sv=1)
The name of the baked script will be CoherenceSync[prefabName]
.
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.
Your baked script might now reference your component:
Baked scripts reside by default in Assets/coherence/baked
, but you can check where exactly they're located in the settings window.
This means that if you decide to change your component name (Health
) or 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.
Our watchdog is able to understand when this happens, and offer you a solution right away.
![](https://docs.coherence.io/~gitbook/image?url=https%3A%2F%2F3580906557-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUrQ8GJkdHxImzMEWuxaz%252Fuploads%252Fgit-blob-b97a70de13a3deee7dc4e94cebf5c3ee2d986b49%252FScreenshot%25202021-07-07%2520at%252018.04.18.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=3b2b0869&sv=1)
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://docs.coherence.io/~gitbook/image?url=https%3A%2F%2F3580906557-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUrQ8GJkdHxImzMEWuxaz%252Fuploads%252Fgit-blob-cddca53907bc85db2d14c17704c43fcb64eaf083%252FScreenshot%25202021-07-07%2520at%252018.06.43.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=b5775d56&sv=1)
You can enter safe mode manually via coherence > Schema and Baking > Bake Schemas (Safe Mode)
.
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://docs.coherence.io/~gitbook/image?url=https%3A%2F%2F3580906557-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FUrQ8GJkdHxImzMEWuxaz%252Fuploads%252Fgit-blob-bcecf5e0871a99c886a58f43808b5994720296cb%252FScreenshot%25202021-07-07%2520at%252018.08.51.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=924a3135&sv=1)
Now, we can manually rebind our data: unbind health
and bind health2
. Once we do, we can now safely bake again.
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.
Last updated