Baking and 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 section.
coherence calls this mechanism baking.

Baking

Click on coherence > Bake.
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 (Level of Detail) that have been added.
For every Prefab with a CoherenceSync component, the baking process will generate a C# script specifically tuned for it.

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

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 IntelliSense. 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 to try both and check what works best for your workflow.

Using the Baked Script in your Prefab

Once the baked scripts have been generated, you can make use of it by ticking the checkbox Use Baked Script in the CoherenceSync inspector.

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:
var healthComponent = GetComponent<Health>();
...
var healthField = healthComponent.health;
When baking via assets, baked scripts will be located in Assets/coherence/baked.
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.
//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.
You can delete the baked folder manually through the coherence Settings window.
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.
Now, we can manually rebind our data: unbind health and bind health2. Once we do, we can now safely bake again.
Remember to bake again after you fix your Prefabs.
Export as PDF
Copy link
Edit on GitHub
Outline
Overview
Baking
Two bake modes
Using the Baked Script in your Prefab
Modifying bound data, safe mode and the watchdog