com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

Custom NetworkVariable with access to m_NetworkBehaviour

Open fachryip opened this issue 2 years ago • 4 comments

Hello,

I just upgrade MLAPI in develop and I have a bunch of error that need to be fixing. I create custom NetworkVariable so that client can also write. I need to determine that only client that owner of this NetworkVariable that can write. So I need to access m_NetworkBehaviour (before it was named _owner) to check it. But this changes and I cannot access it anymore.

In the code I see private protected NetworkBehaviour m_NetworkBehaviour;

Therefore I think this m_NetworkBehaviour can only be access from child in the same assembly. Why you restrict the access of this network behaviour? And what are the solution or maybe workaround to access it?

Thanks in advanced.

fachryip avatar Oct 01 '21 03:10 fachryip

I am not quite sure why the access is restricted in sub-classes. So take my solution with a grain of salt.

An easy way to get access to the NetworkBehaviour would be to override the Initialize(NetworkBehaviour) method and store it for yourself. Don't forget to call base.Initialize.

Vulcanostrol avatar Jun 01 '22 16:06 Vulcanostrol

+1 for this issue - I can't see why the m_NetworkBehaviour variable would be restricted outside the assembly. I'm trying to make a custom NetworkVariable type by inheriting from NetworkBehaviourBase and should be able to access this field.

starchitectus avatar Apr 09 '23 00:04 starchitectus

I am not quite sure why the access is restricted in sub-classes. So take my solution with a grain of salt.

An easy way to get access to the NetworkBehaviour would be to override the Initialize(NetworkBehaviour) method and store it for yourself. Don't forget to call base.Initialize.

If this worked when this was posted, it no longer does. Initialize(NetworkBehaviour) is not virtual and can't be overridden.

starchitectus avatar Apr 09 '23 00:04 starchitectus

I am not quite sure why the access is restricted in sub-classes. So take my solution with a grain of salt.

An easy way to get access to the NetworkBehaviour would be to override the Initialize(NetworkBehaviour) method and store it for yourself. Don't forget to call base.Initialize.

If this worked when this was posted, it no longer does. Initialize(NetworkBehaviour) is not virtual and can't be overridden.

Yeah, its a bummer. Originally with MLAPI, initialize was override-able.

apilola avatar Sep 28 '23 07:09 apilola