Search Unity

[Question] Syncing Activation/Deactivation/Reparenting of child objects across network

Discussion in 'Netcode for GameObjects' started by ktdab, Jul 22, 2022.

  1. ktdab

    ktdab

    Joined:
    Sep 22, 2018
    Posts:
    6
    Hi, this may be a noob question but I can't find anything while searching the forums. Thanks for reading:

    Here is a description of the question:
    • There are three networked player prefabs, one host and two clients.
    • When a client activates a child object for their networked player prefab, it is first done locally.
    • Then, the client fires a [ServerRPC] to activate the child object on the server, using
      NetworkManager.Singleton.ConnectedClients[localClientIdOrigin].PlayerObject.gameObject
      to find the player's root gameobject.
    • Changing the server does not sync to the second client, so a [ClientRPC] is fired from the server, however, using the same line of code (
      NetworkManager.Singleton.ConnectedClients[localClientIdOrigin].PlayerObject.gameObject
      ) throws an error
      NotServerException: ConnectedClients should only be accessed on server.
      .

    I wrongfully thought that only the server needed to activate/reparent the child game object for a network player prefab and it would sync to all clients. I also realize the design I tried to implement violates the fully server authoritative model of player network object ownership which isn't ideal.

    So the question is:

    How does one sync the activation/deactivation/reparenting of child objects across a network when
    NetworkManager.Singleton.ConnectedClients
    cannot be used?


    Thanks!

    Edit: I just found this page added last week: https://docs-multiplayer.unity3d.co...ced-topics/networkobject-parenting/index.html,

    I'll update if I get a solution to my own question

    Also, to clarify, my issue is not that objects are reparented to different owners, just when the heirarchy within an object is changed... for example

    rootPlayerNetworkGameObject
    - Child1
    - - Child1.1
    - Child 2
    - - Child 2.1

    reparenting 1.1 and 2.1

    rootPlayerNetworkGameObject
    - Child 1
    - - Child 2.1
    - Child 2
    - - Child 1.1
     
    Last edited: Jul 22, 2022
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    I'm not sure that changes in the hierarchy are allowed for NetworkObjects – but assuming that they are, a way to replicate this is to pass an address of sorts into the ServerRpc that you call to activate/reparent a child. For example, you can pass a NetworkObjectReference to the root objects along with integers that represent the (grand)child indices in the transform hierarchy.

    Even easier would be to cache the gameObjects/transforms of the children that you know beforehand will change at some point, and refer to them by an index in a list (e.g. swap object i with object j, or activate object k).