Skip to content

Commit 089c61b

Browse files
committed
fix synced values docs.
1 parent 2f7c87d commit 089c61b

1 file changed

Lines changed: 65 additions & 15 deletions

File tree

docs/guide/synced-values.mdx

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Synced Values & Adapters
55

66
# Synced Values & Adapters
77

8-
`@value()` (alias **`@syncedValue()`**) turns a field into …
8+
`@syncedValue()` turns a field into …
99

1010
* a network-replicated variable
1111
* an observable you can `onChanged()`
@@ -16,9 +16,9 @@ title: Synced Values & Adapters
1616
import { Behavior, value } from "@dreamlab/engine";
1717

1818
export default class MyBehavior extends Behavior {
19-
@value() myNumber = 10;
20-
@value() myBoolean = false;
21-
@value() myString = "test";
19+
@syncedValue() myNumber = 10;
20+
@syncedValue() myBoolean = false;
21+
@syncedValue() myString = "test";
2222
}
2323
```
2424
This will cause the class property to be visible in the inspector when the Behavior is attached to an entity:
@@ -44,14 +44,64 @@ Pass the adapter class as the first argument:
4444

4545
| Adapter class | Purpose / UI | Usage example |
4646
|--------------------------|------------------------------------|-----------------------------------------------------------------|
47-
| `EntityRef` | Drag-drop any scene entity | `@value(EntityRef) target?: Entity` |
48-
| `RelativeEntity` | Prefab-safe child/sibling reference| `@value(RelativeEntity) child?: Entity` |
49-
| `Vector2Adapter` | Two number inputs (X & Y) | `@value(Vector2Adapter) dir = new Vector2(1, 0)` |
50-
| `ColorAdapter` | Hex picker | `@value(ColorAdapter) tint = "#ff00ffff"` |
51-
| `TextureAdapter` | Texture path input + preview | `@value(TextureAdapter) tex = "res://img.png"` |
52-
| `SpritesheetAdapter` | JSON spritesheet picker | `@value(SpritesheetAdapter) sheet = "res://hero.json"` |
53-
| `AudioAdapter` | Audio asset picker | `@value(AudioAdapter) sfx = "res://hit.ogg"` |
54-
| `AspectRatioAdapter` | Two numbers (W x H) | `@value(AspectRatioAdapter) ratio = [16, 9]` |
55-
| `new EnumAdapter([...])` | Dropdown enum | `@value(new EnumAdapter(["Idle","Walk"])) state = "Idle"` |
56-
57-
Need something else? Extend `ValueTypeAdapter`.
47+
| `EntityRef` | Drag-drop any scene entity | `@syncedValue(EntityRef) target?: Entity` |
48+
| `RelativeEntity` | Prefab-safe child/sibling reference| `@syncedValue(RelativeEntity) child?: Entity` |
49+
| `Vector2Adapter` | Two number inputs (X & Y) | `@syncedValue(Vector2Adapter) dir = new Vector2(1, 0)` |
50+
| `ColorAdapter` | Hex picker | `@syncedValue(ColorAdapter) tint = "#ff00ffff"` |
51+
| `TextureAdapter` | Texture path input + preview | `@syncedValue(TextureAdapter) tex = "res://img.png"` |
52+
| `SpritesheetAdapter` | JSON spritesheet picker | `@syncedValue(SpritesheetAdapter) sheet = "res://hero.json"` |
53+
| `AudioAdapter` | Audio asset picker | `@syncedValue(AudioAdapter) sfx = "res://hit.ogg"` |
54+
| `AspectRatioAdapter` | Two numbers (W x H) | `@syncedValue(AspectRatioAdapter) ratio = [16, 9]` |
55+
| `enumAdapter` | Dropdown enum | See example below |
56+
57+
### enumAdapter
58+
`enumAdapter` allows you to specify a set of choices as valid values. This also conveniently as a drop down menu in the editor. They are used like this:
59+
```ts
60+
// above the behavior class
61+
const shapesAdapter = enumAdapter(["circle", "square", "triangle"]);
62+
type Shape = enumAdapter.Union<typeof shapesAdapter>;
63+
64+
export default class Example extends Behavior {
65+
@syncedValue(shapesAdapter)
66+
foo: Shape = "circle";
67+
}
68+
```
69+
70+
## 3. Disabling replication
71+
72+
This is useful if you want to expose the value to the editor or other scripts without networking it. If you change it on one client, the other clients or server don't get updated. For example:
73+
74+
```ts
75+
@syncedValue(EntityRef, {replicated: false})
76+
someEntity: Entity;
77+
```
78+
79+
or with a number:
80+
```ts
81+
@syncedValue(Number as ValueTypeTag<number>, { replicated: false })
82+
counter = 1;
83+
```
84+
85+
### Alternative `@value` syntax
86+
You might have noticed the syntax for the counter above was gross because @syncedValue forces us to pass a type to access the overrides.
87+
If you want to override replicated on a primitive value, you can use `@value` which has a slightly different method signature of `{type, ...overrides}`
88+
89+
For example:
90+
```ts
91+
@syncedValue(EntityRef, { replicated: false })
92+
someEntity: Entity;
93+
94+
@syncedValue(Number as ValueTypeTag<number>, { replicated: false })
95+
counter = 1;
96+
```
97+
becomes...
98+
```ts
99+
@value({ type: EntityRef, replicated: false })
100+
someEntity: Entity;
101+
102+
@value({ replicated: false })
103+
counter = 1;
104+
```
105+
106+
The only difference between `@syncedValue` and `@value` is the arrangement of the arguments. `@value` allows you to set overrides without specifying a type.
107+
Both are valid ways of representing the same thing.

0 commit comments

Comments
 (0)