Hi,
I’m currently fiddling around with NetCode for GameObjects version 1.0.0-pre.5 and I’ve set up the following scenario:
I have a SpawnManager that creates the player objects like this:
var playerObject = Instantiate(PlayerPrefab);
playerObject.gameObject.name = $"Player {clientId.ToString()}";
playerObject.SpawnAsPlayerObject(clientId, true);
Then, within the SpawnShip method of the PlayerController I do the following to spawn in the player’s visual representation:
public void SpawnShip(Vector3 position, Quaternion rotation)
{
PDebug.Log("Spawning ship for player {0}", OwnerClientId);
var networkShip = Instantiate(ShipPrefab, position, rotation);
networkShip.name = $"{name}_Ship";
networkShip.SpawnWithOwnership(OwnerClientId, true);
}
As you can see, I spawn the object with SpawnWithOwnership and I put in the OwnerClientId. Via the log I can verify that the server outputs the correct Id, so 0 for player 1 and 1 for player 2.
However, I also have this little debug UI that prints various things to the screen
GUILayout.Label("Owned Objects:" + NetworkManager.Singleton.LocalClient?.OwnedObjects?.Count);
The host outputs that it has 1 owned object, which is the additional spawned ship.
However on another client (with OwnerId 1) it reports it does not have owned objects hence being spawned with SpawnWithOwnership and id 1. I also tried using ChangeOwnership immediately, but it does not help.
Am I missing here something or do I have a misunderstanding about OwnedObjects?
I also dug a bit into the code, and I’ve found this here:
(see picture 1)
This is located in class NetworkSpawnManager. As I can see here, that on server side, OwnedObjects are tracked correctly. But on client side, this piece of code is missing.
I then went on to see the message handling of ChangeOwnership, that looks like:
(see picture 2)
on client side, the debugger did enter Line 35 and 43, so the owner is set correctly, so it only looks like the tracking of the OwnedObjects is missing on client side.
Or is that intended, that the client does not track ownership of NetworkObjects? If so, the API is misleading, because NetworkManager.Singleton.LocalClient has a property OwnedObjects.
Thanks!
Edit: For whatever reason I get an access denied error, if I try to add the pictures directly into the text as full image or thumbnails via the forum buttons.