Delete object across network

How does deleting object works across the network?

In Needle Engine, deleting an object across the network involves a few steps that ensure consistency across all connected clients. Here’s how it generally works:

  1. Identify the Object: Each object that is networked has a unique identifier (GUID). This is crucial to ensure that the correct object is being targeted for deletion.

  2. Initiate Object Deletion: When an object needs to be deleted, a method like syncDestroy is called. This method sends a signal to the networking system indicating that a specific object should be removed from all connected clients (see the documentation).

  3. Network Communication: The deletion request is sent over the network using the websocket connection established by Needle Engine. The server, or each client, receives the message that includes the guid of the object to be deleted.

  4. Execute Object Deletion on Clients: Once the message is received, the client-side code will remove the object from the scene graph, effectively deleting it from the visual representation and any logic running on that object (see the documentation).

  5. Persistency and Cleanup: If the object state was stored persistently or used in any client-specific operations, this would be cleaned up. Needle provides mechanisms to delete states for specific GUIDs using send("delete-state", { guid: guid, dontSave: true }) (see the documentation).

Using these steps, Needle Engine ensures that object deletions are properly synchronized across the network, maintaining consistency for all users in the networked room.

For more detailed examples and further exploration, you might want to review Needle Engine’s documentation here.

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

if I use syncDestroy() will it also remove the GUID of the object as well?

if I use syncDestroy() will it also remove the GUID of the object as well?

Hi, Saad Baig. Yes, when you use syncDestroy() to delete an object in Needle Engine, it not only removes the object from the scene but also unregisters its GUID from the network.

This means that after calling syncDestroy(), the GUID will no longer reference that object, and any further attempts to interact with the object using its GUID will not work. It effectively performs a cleanup ensuring that the object is fully removed from both the visual representation and the network state.

If you need additional details or examples on how to implement this, feel free to ask!

23 replies left
thread_nDk26zcFsYVjfq4oIvYfn9tB
Reply to this message to continue the conversation

i have a object3D targetObject. On using syncDestroy(this.targetObject, this.context.connection); it gives an error Argument of type ‘Object3D’ is not assignable to parameter of type ‘IComponent | IGameObject’.
Type ‘Object3D’ is missing the following properties from type ‘IGameObject’: guid, activeSelf, addNewComponent

what can be wrong here? does the object needs to have deleteable component attached to it like in the sample?

Hi, Saad Baig. The error you’re encountering indicates that the targetObject you are trying to delete is an instance of Object3D, but the syncDestroy function expects an argument of type IComponent or IGameObject.

In Needle Engine, for an object to be deleted using syncDestroy, it typically needs to be a GameObject that implements the IGameObject interface, which includes the properties like guid, activeSelf, and methods like addNewComponent.

To resolve this issue, you should ensure that your targetObject is indeed a GameObject or has been correctly wrapped as such. Here are a few steps to check:

  1. Attach the Deletable Component: As you noted, the object may need to have a specific component (like a Deletable component) that marks it for network deletion. Ensure that the component is attached to your GameObject.

  2. Verify Type: Make sure that you’re working with a GameObject or an object that meets the IGameObject interface requirements. You may need to obtain the correct reference to the GameObject rather than the raw Object3D.

  3. Documentation Reference: You can look at the specific sample code to see an example of how objects are set up for deletion. This may provide you with insights into what components need to be attached.

Here’s a general code suggestion for deleting a GameObject:

const gameObject = this.targetObject as IGameObject; // Ensure it's cast to IGameObject
if (gameObject) {
    syncDestroy(gameObject, this.context.connection);
}

This ensures that you’re passing the correct type to syncDestroy(). If you need more specific guidance on types or components, please provide additional details on how you are structuring your objects!

22 replies left
thread_nDk26zcFsYVjfq4oIvYfn9tB
Reply to this message to continue the conversation

public deleteObject() {
const gameObject = this.targetObject as IGameObject;
syncDestroy(gameObject, this.context.connection);
}

why does this logic doesnt work?

Hi, Saad Baig. The logic you provided for deleting the object might not work due to a couple of reasons:

  1. Type Assertion Failure: The line const gameObject = this.targetObject as IGameObject; is assuming that this.targetObject is of type IGameObject. If this.targetObject is actually an instance of Object3D and does not implement the IGameObject interface, then this type assertion will only compile without ensuring the runtime validity. You need to check if this.targetObject is actually part of the networking system and has the necessary components.

  2. Nullish Check: Before proceeding with syncDestroy, ensure that gameObject is not null or undefined. If your targetObject isn’t correctly set up or has been destroyed already, attempting to call syncDestroy on it will result in an error.

  3. Component Attachment: Ensure that the targetObject is a GameObject with the appropriate components attached. In some cases, simply casting it to IGameObject may not be enough if it isn’t properly configured.

Here’s an updated version of your deleteObject method that includes a type guard to verify the object:

public deleteObject() {
    const gameObject = this.targetObject as IGameObject;

    // Check if gameObject is indeed an IGameObject
    if (gameObject && gameObject.guid) {
        syncDestroy(gameObject, this.context.connection);
    } else {
        console.error("targetObject is not a valid IGameObject");
    }
}

This code checks if gameObject is valid and has a guid before proceeding to delete it. Double-check that this.targetObject is correctly set up and that it is indeed a GameObject meant for deletion. If further issues persist, providing the structure or components of this.targetObject would help diagnose the issue.

24 replies left
thread_nDk26zcFsYVjfq4oIvYfn9tB
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.