Details about SyncField decorator

Could you explain me in detail the decorator SyncField and how to use it?

Original Post on Discord

by user 632418299711324161

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Here is what I found for you :brain: Discord

Hello Diego/Ken2,

SyncField is a decorator provided by Needle Engine’s networking system which is used to synchronize a field value across the network, ensuring that all clients connected to the same room have the same value for that field.

Here’s how to use the SyncField decorator in a Needle Engine component:

  1. Import the decorator from the Needle Engine networking module.
  2. Apply the SyncField decorator to class fields you want to synchronize.

The SyncField decorator takes an options object which can define various parameters such as the interpolation type.


import { Behaviour, SyncField } from '@needle-tools/engine';

export class MySynchronizedComponent extends Behaviour {
    @SyncField({ interpolate: true })
    public mySyncedVariable: number = 0;

    start() {
        // This code will run when the component is initialized

    update() {
        // This code will run every frame
        // Use this.mySyncedVariable which will be in sync across clients

In the example, the mySyncedVariable field is marked with @SyncField; it is initialized with a value of 0. The { interpolate: true } option means that the value will smoothly transition to match values as they’re updated across the network.

Make sure your class extends Behaviour for the framework to properly manage lifecycle methods and networking capabilities. Also, remember that synchronization only works properly when you are connected to a room with the networking system appropriately set up in your scene.

Refer to the Networking section in the Needle Engine documentation for more details on how to set up and use networking.

4 messages left GPT 4
Thanks for your feedback!

Can I use @SyncField decorator on an array ?

by user 632418299711324161

Yes, you can use the @SyncField decorator on an array. When using it on an array field, the @SyncField decorator will synchronize the entire array across the network, ensuring that all clients have the same array elements and length. Just keep in mind that the array elements must also be serializable to ensure proper synchronization.

3 messages left GPT 3.5

I don’t know why I am tagged but, I am not in charge. I am just a noob😄

by user 151660356224286721

Srry hahaha, I saw your name in a thread and I tought that you were the bot. Srrry budy :sweat_smile:

by user 632418299711324161

Could you give an example of an array of numbers which has the @SyncField decorator and how Can the users in the same room can access to that array ?

by user 632418299711324161

Thinking :exploding_head: thread_kZz3vjVW2k0A2brpnZuz4J3n

no Problem :slightly_smiling_face:

by user 151660356224286721

@DiegoEloko see my replies here: Discord


by user 632418299711324161

How can a Player prefab, which has the Player State component, access to a array in another class which has the @SyncField decorator ? I want that the Players prefabs can modify the same @SyncField array through the same NetworkConnection. Could you give an example ?

by user 632418299711324161

Here is what I found for you :brain: Discord

Hello DiegoEloko. Here’s an example of using a @SyncField decorator to allow Player prefabs with the Player State component to access and modify a shared array:

  1. Define the array with the @SyncField decorator in a component that manages the shared state.
  2. Ensure that each Player prefab’s Player State component has a reference to the shared state manager.
  3. Modify the shared array through the Player State component using the reference to the shared state manager.

The example below shows how to set up the shared state manager with a networked field and a Player component that modifies the shared array:

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

// Shared state manager component with a networked array field
class SharedStateManager extends Behaviour {
    sharedArray: number[] = [];

    // Logic for modifying the shared array...

// Player component that accesses and modifies the shared array
class PlayerState extends Behaviour {
    sharedStateManager: SharedStateManager;

    start() {
        this.sharedStateManager = this.gameObject.getComponent(SharedStateManager);

    update() {
        // Logic to modify the shared array
        if (this.sharedStateManager) {
            // Make sure to re-assign the array to trigger an update
            this.sharedStateManager.sharedArray = this.sharedStateManager.sharedArray;

Make sure to set up the connection properly to the NetworkConnection, and include necessary logic to handle network synchronization according to your game’s requirements.

Refer to the documentation on how to use @syncField for more detailed information and examples (see the documentation).

1 messages left GPT 4