I want to try and migrate from a project that use Unity Netcode for GameObject to Coherence.
I couldn’t find one place with all the relevant info and tips, so I hope this post would help not just me.
I want to focus on using existing architecture, with minimal changes. While re-writing everything from scratch might be better architecture wise, I don’t find it much practical for this scope.
The project I’m working on is this: GitHub - De-Panther/webxr-multiplayer-template: Unity WebXR Multiplayer Template, based on Unity VR Multiplayer Template.
It has Lobby system (with Relay and Transport).
The Host is one of the Clients, and there’s yet no implementation of server hosting or transferring the Host role.
There are some shared 3D menus and mini-games.
So far I imported Coherence package version 1.3.1. And added the Bridge and Query objects to the scene.
The first prefab I tried to migrate is the player prefab, as I think it is one of the more complicated ones.
It has 2 NetworkBehaviour
classes. XRINetworkPlayer and XRHandPoseReplicator.
I removed the NetworkObject
component from the prefab, and added CoherenceSync
component instead.
I replaced NetworkBehaviour
with MonoBehaviour
in both classes and added a reference to the CoherenceSync
.
And here comes the troubles
The project is heavily relay on the NetworkBehaviour
properties.
IsOwner
is now based on coherenceSync.HasStateAuthority
.
IsLocalPlayer
is also based on coherenceSync.HasStateAuthority
but I’m not sure if there’s a more precise property.
IsOwnedByServer
in this case is used to check if it’s the host, I don’t know what to use here for now. Any suggestions?
OwnerClientId
is ulong
but maybe using the string coherenceSync.EntityState.CoherenceUUID
would work for the use cases in this project. Is it correct to use CoherenceUUID
of a player prefab to identify client?
Next are NetworkVariable
and NetworkList
.
The CoherenceSync
component creates a scheme from selected public variables, and let us sync them.
The NetworkVariable
is similar to such variables, but it also allows to Subscribe and Unsubscribe for when the value changed. I tried to find a way to subscribe to CoherenceSync
variables events, but found only Rigidbody related ones. Are there events for custom variables as well?
I couldn’t find similar solution to sync lists like the NetworkList
does. What would you suggest to sync lists of float
and of vector3
? (no need to subscribe for changes)
Project question:
While Coherence has Lobby system, I think the way this Unity project was made, it’s better to use only the Rooms system as players are still in the same room while in the mini-games. Any suggestions on how to handle the Host role and transferring it when the host disconnects? (Using the Rooms system)
Thanks