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 class | Purpose / UI | Usage example |
---|---|---|
EntityRef | Drag-drop any scene entity | @value(EntityRef) target?: Entity |
RelativeEntity | Prefab-safe child/sibling reference | @value(RelativeEntity) child?: Entity |
Vector2Adapter | Two number inputs (X & Y) | @value(Vector2Adapter) dir = new Vector2(1, 0) |
ColorAdapter | Hex picker | @value(ColorAdapter) tint = "#ff00ffff" |
TextureAdapter | Texture path input + preview | @value(TextureAdapter) tex = "res://img.png" |
SpritesheetAdapter | JSON spritesheet picker | @value(SpritesheetAdapter) sheet = "res://hero.json" |
AudioAdapter | Audio asset picker | @value(AudioAdapter) sfx = "res://hit.ogg" |
AspectRatioAdapter | Two 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
.