com.unity.netcode.gameobjects
com.unity.netcode.gameobjects copied to clipboard
Open NetworkVariableBase initialization path to derivations
Small but helpful QoL request
Is your feature request related to a problem? Please describe. NetworkVariableBase is available for developers to create their own implementations, however if additional logic is needed for initializing the custom network variable, a new entry point must be added and manually called whenever used.
Describe the solution you'd like
Add virtual keyword to NetworkVariableBase.Initialize()
Describe alternatives you've considered Could instead provide a virtual OnInitialize() that gets called from Initialize() if concerned about base.Initialize() not being called. NetworkBehaviour param should be either passed through or a protected getter for m_NetworkBehaviour should be provided in this case.
Additional context Use case being applied to: Custom NetworkVariableBase that wraps a NetworkVariable<NetworkObjectReference> to provide automatic full reference resolution on clients when the target reference is spawned. Additional initialization logic is required in addition to calling Initialize() on the internal NetworkVariable.
Can I ask why this was triaged as low? The docs suggest making custom network variables and even give minimal demo code to show what to do. However, they just can't be done as it stands without completely copying and pasting CanClientRead, CanClientWrite and OwnerClientId as well as managing your own m_NetworkBehaviour. It seems in this case the API is broken and m_NetworkBehaviour should be protected, not private protected.
Thanks
I've been trying to write a CustomNetworkVariable and am hitting the same issue with the NetworkVariableBase
not quite being extensible enough. The current documentation only shows a minimal example and trying to use the techniques in NetworkVariable
and NetworkList
for checking client read/write permissions hits issues with the m_NetworkBehaviour
not being accessible.
There are possible workarounds like passing the NetworkBehaviour to the constructor during OnNetworkSpawn and reimplementing the CanClientRead etc stuff, or reflection to grab the m_NetworkBehaviour
from the base class.
It would be great to get this fixed up and a more comprehensive example added to the docs.
Access to m_NetworkBehaviour
and not being able to override OnClientRead
and OnClientWrite
are the exact roadblocks I hit with implementing my own networked variable.
Please consider changing the current implementation. As is, the idea of custom implementations of NetworkVariableBase
is nice, but cumbersome in practice.
Bump.