Skip to main content

Synced Values & Adapters

@value() (alias @syncedValue()) turns a field into …

  • a network-replicated variable
  • an observable you can onChanged()
  • a row in the Inspector (Properties Panel)
import { Behavior, value } from "@dreamlab/engine";

export default class MyBehavior extends Behavior {
@value() myNumber = 10;
@value() myBoolean = false;
@value() myString = "test";
}

This will cause the class property to be visible in the inspector when the Behavior is attached to an entity:

The changes made in the inspector will apply to the class instance and any syncedValue will also be replicated over the network.

1 · Listening for changes

this.values.get("myNumber")?.onChanged((next, prev) => {
console.log(`+${next - prev} pts`);
});

2 · Built-in adapters

Adapters allow you to use more complex types with syncedValues. By default, Dreamlab only supports primitive types (number, string, boolean) in synced values. Pass the adapter class as the first argument:

Adapter classPurpose / UIUsage example
EntityRefDrag-drop any scene entity@value(EntityRef) target?: Entity
RelativeEntityPrefab-safe child/sibling reference@value(RelativeEntity) child?: Entity
Vector2AdapterTwo number inputs (X & Y)@value(Vector2Adapter) dir = new Vector2(1, 0)
ColorAdapterHex picker@value(ColorAdapter) tint = "#ff00ffff"
TextureAdapterTexture path input + preview@value(TextureAdapter) tex = "res://img.png"
SpritesheetAdapterJSON spritesheet picker@value(SpritesheetAdapter) sheet = "res://hero.json"
AudioAdapterAudio asset picker@value(AudioAdapter) sfx = "res://hit.ogg"
AspectRatioAdapterTwo numbers (W x H)@value(AspectRatioAdapter) ratio = [16, 9]
new EnumAdapter([...])Dropdown enum@value(new EnumAdapter(["Idle","Walk"])) state = "Idle"

Need something else? Extend ValueTypeAdapter.