LogoLogo
HomeOnline DashboardAPIDiscordForums
SDK 0.5.2
SDK 0.5.2
  • Welcome
  • Overview
    • What is coherence?
    • How does coherence work?
    • Features and Roadmap
    • Requirements
  • Get Started
    • Install coherence
    • Scene setup
    • Prefab setup
    • Build and run
    • Baking and code generation
    • Create a free account
    • Deploy replication server
    • Share builds
  • Authority and communication
    • How authority works
    • Authority transfer
    • Commands
    • Client messages
    • Server-side and input queues
    • Animations
  • Persistence
    • Overview
    • Configuring persistence
    • Storage
    • Example – A global counter
  • Optimization
    • Overview
    • Simulation frequency
    • Areas of interest
    • World size
    • Level of detail
    • Interpolation
    • Extrapolation
  • Connected entities
    • Overview
    • Entity references
    • Parent-child relationships
  • Simulators
    • Overview
    • Client vs. simulator logic
    • Build and deploy
    • Simulator load balancing
  • Tutorial project
    • Get the Tutorial Project
    • Start Tutorial
      • 1. Transforms
      • 2. Physics
      • 3. Persistence
      • 4. Animation and Variables
      • 5. AI Navigation
      • 6. Network Commands
      • 7. Network Teams (draft)
  • Game Services
    • Game account
    • Key-value store
    • Matchmaking
  • Developer Portal
    • Overview
    • Dashboard
    • Resource Usage
    • Replicator and Simulator Configuration
    • Enabling Game Services
  • API reference
    • Network SDK
      • CoherenceSync
      • MonoBridge
      • LiveQuery
      • Archetype
      • Sample UI
      • Settings Window
      • Custom Bindings
    • Cloud API
      • API tokens and keys
      • Server discovery
      • Game account
      • Key-value store
      • Matchmaking
    • Replication Server
    • Simulation Server
  • Schema reference
    • Overview
    • Specification
    • Field Settings
    • Archetypes and LOD-ing
  • Resources
    • Downloads
    • SDK Update Guide
    • Video Tutorials
    • Glossary
    • CLI Utilities
    • Helper Scripts
    • Troubleshooting
  • Community
    • Discord
  • Additional information
    • Pricing
    • SLA
    • Unreal Engine support
    • WebGL
    • Peer-to-Peer (P2P)
    • Known Issues
    • Changelog
Powered by GitBook
On this page
  • Baking Video Tutorial
  • Overview
  • Bake schemas
  • Activate baked schema on prefab
  • Modifying bound data, safe mode and the watchdog

Was this helpful?

Export as PDF
  1. Get Started

Baking and code generation

Last updated 3 years ago

Was this helpful?

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.

coherence offers an automatic way of doing that called baking.

Bake schemas

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.

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.

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:

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

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

//public float health;
public float health2;

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

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.

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:

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.

For optimal runtime performance, we need to create a schema and perform code generation specific to our project. Learn more about this in the section.

How does coherence work