Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

NGO 1.5.1 OwnedObjects broken #2626

Closed luishnc closed 1 year ago

luishnc commented 1 year ago

Description

NGO 1.5.1 Method Singleton.ConnectedClients[clientID].OwnedObjects not working anymore.

Reproduce Steps

1 Sample class that Spawin with ownership:

public void SpawnTestServerRpc(ServerRpcParams serverRpcParams = default) {

    //assign transform to the Sender parent transform
    var clientID = serverRpcParams.Receive.SenderClientId;

    if (NetworkManager.Singleton.ConnectedClients.ContainsKey(clientID))
    {
        var client = NetworkManager.ConnectedClients[clientID];
        //var client = NetworkManager.Singleton.ConnectedClients.ContainsKey(clientID);
        Transform clientTransform = NetworkManager.ConnectedClients[clientID].PlayerObject.transform.GetComponent<Transform>();
        Debug.Log("Client: " + clientID + " Spawned Muffin");

        foreach (Transform t in creatureListPrefab)
        {
            Debug.Log("Trying to spawn Muffin");
            Transform spawnedObjectTransform = Instantiate(t);

            spawnedObjectTransform.GetComponent<NetworkObject>().SpawnWithOwnership(client.ClientId);

        }

    }

}
  1. Sample method showing that OwnedObjects is broken:

    [ServerRpc(RequireOwnership = false)] private void AssignAttackedCreatureServerRpc(NetworkObjectReference enemyObjectReference, ServerRpcParams serverRpcParams = default) { enemyObjectReference.TryGet(out NetworkObject enemyNetworkObject);

    var clientID = serverRpcParams.Receive.SenderClientId;
    
    if (NetworkManager.Singleton.ConnectedClients.ContainsKey(clientID))
    {
        var client = NetworkManager.ConnectedClients[clientID];
        Debug.Log("Trying to set tartget creature. Owned objects: " + NetworkManager.Singleton.ConnectedClients[clientID].OwnedObjects.Count);
    
        foreach (NetworkObject netObj in NetworkManager.Singleton.ConnectedClients[clientID].OwnedObjects)
        {
            if (netObj.TryGetComponent<CreatureAI>(out CreatureAI dcClientsCreatureAI))
            {
    
                dcClientsCreatureAI.SetTargetPosition(enemyNetworkObject.transform);
            }
        }
    
    }

    }

Actual Outcome

OwnedObjects.count = 0

Expected Outcome

OwnedObjects.count = 1

Environment

Additional Context

I tested with NGO 1.2.0, 1.31 and 1.4.0 and they all are working as expected.

NoelStephensUnity commented 1 year ago

@luishnc This will be fixed in the up-coming patch. Thank you for reporting this issue! 👍

luishnc commented 1 year ago

@NoelStephensUnity cool, thanks Noel for the fast response 👍