@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.
52 lines (51 loc) • 1.9 kB
TypeScript
export declare type SyncFieldOptions = {
onPropertyChanged: Function;
};
export declare type FieldChangedCallbackFn = (newValue: any, previousValue: any) => void | boolean | any;
/**
* Marks a field for automatic network synchronization across connected clients.
* When a synced field changes, the new value is automatically broadcast to all users in the room.
*
* Primitives (string, number, boolean) sync automatically.
* For arrays/objects, reassign to trigger sync: `this.myArray = this.myArray`
*
* @param onFieldChanged Optional callback when the field changes (locally or from network).
* Return `false` to prevent syncing this change to others.
*
* @example Basic sync
* ```ts
* class MyComponent extends Behaviour {
* @syncField() playerScore: number = 0;
* }
* ```
* @example With change callback
* ```ts
* class MyComponent extends Behaviour {
* @syncField("onHealthChanged") health: number = 100;
*
* onHealthChanged(newValue: number, oldValue: number) {
* console.log(`Health: ${oldValue} → ${newValue}`);
* }
* }
* ```
* @example Preventing sync (one-way)
* ```ts
* class MyComponent extends Behaviour {
* @syncField(function(newVal, oldVal) {
* // Process incoming value but don't sync our changes
* return false;
* }) serverControlled: string = "";
* }
* ```
* @see {@link serializable} for editor serialization
* @link https://engine.needle.tools/docs/how-to-guides/networking/
*/
export declare const syncField: (onFieldChanged?: string | FieldChangedCallbackFn | undefined | null) => (target: any, _propertyKey: string | {
name: string;
}) => void;
export declare type SyncOptions = {
key?: string;
fieldName?: string;
};
/** experimental - use syncField instead */
export declare const sync: (_options?: SyncOptions) => <T>(target: any, _propertyKey: string, descriptor: PropertyDescriptor) => void;