SynceField

How can I use syncField on a variable without triggering any function. I dont want anything to happen just want to have a variable synced that i can later use

To use the @syncField decorator in Needle Engine to synchronize a variable without triggering any functions, simply use it on your variable without passing a callback function. This way, the field will be automatically networked, but no function will be invoked when the value changes.

Here’s how you can set it up:

import { Behaviour, syncField, serializable } from "@needle-tools/engine";

export class MyScript extends Behaviour {

    @syncField()
    @serializable()
    mySyncedVariable: number = 42; // default value 

    // No callback function defined for changes
}

A few tips:

  • Ensure that your tsconfig.json has experimentalDecorators: true to use decorators.
  • The @serializable decorator is often used alongside @syncField to ensure the value is part of the serialized and synchronized state.
  • Re-assign the field if you need to ensure the system detects changes for reference types (e.g., this.mySyncedVariable = this.mySyncedVariable) (see typescript decorators).

This setup will sync the variable across networked clients without attaching any change callbacks. Check out the documentation or examples to extend this by listening to changes when needed (see the documentation) (see networking).

24 replies left
thread_erdheWqkhX4K88oshYNPWIJu
Reply to this message to continue the conversation

This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.