Title: How about a NetworkBehaviourReference
Open Graph Title: How about a NetworkBehaviourReference
X Title: How about a NetworkBehaviourReference
Description: It would be amazing if RPC arguments could just actually be any NetworkBehaviour derivative class instead of only NetworkBehaviourReference so that the compiler could enforce type on those RPC arguments instead of boilerplate cast / TryG...
Open Graph Description: It would be amazing if RPC arguments could just actually be any NetworkBehaviour derivative class instead of only NetworkBehaviourReference so that the compiler could enforce type on those RPC argu...
X Description: It would be amazing if RPC arguments could just actually be any NetworkBehaviour derivative class instead of only NetworkBehaviourReference so that the compiler could enforce type on those RPC argu...
Opengraph URL: https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/2837
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"How about a NetworkBehaviourReference\u003cT>?","articleBody":"It would be amazing if RPC arguments could just actually be any `NetworkBehaviour` derivative class instead of only `NetworkBehaviourReference` so that the compiler could enforce type on those RPC arguments instead of boilerplate cast / TryGet code.\r\n\r\nShort of that, doing an RPC with `NetworkBehaviourReference\u003cMyNetworkBehaviourClass>` at least gives me peace of mind that the compiler can catch me trying to pass the RPC a reference to a `NetworkBehaviour` that isn't a `MyNetworkBehaviourClass`.\r\n\r\nI did it like this:\r\n\r\n```\r\nusing System;\r\nusing System.Runtime.CompilerServices;\r\n\r\nnamespace Unity.Netcode\r\n{\r\n public struct NetworkBehaviourReference\u003cT> : INetworkSerializable, IEquatable\u003cNetworkBehaviourReference\u003cT>>\r\n where T : NetworkBehaviour\r\n {\r\n private NetworkObjectReference m_NetworkObjectReference;\r\n private ushort m_NetworkBehaviourId;\r\n\r\n public NetworkBehaviourReference(NetworkBehaviour networkBehaviour)\r\n {\r\n if (networkBehaviour == null)\r\n {\r\n m_NetworkObjectReference = new NetworkObjectReference((NetworkObject)null);\r\n m_NetworkBehaviourId = 0;\r\n return;\r\n // throw new ArgumentNullException(nameof(networkBehaviour));\r\n }\r\n if (networkBehaviour.NetworkObject == null)\r\n {\r\n throw new ArgumentException($\"Cannot create {nameof(NetworkBehaviourReference\u003cT>)} from {nameof(NetworkBehaviour)} without a {nameof(NetworkObject)}.\");\r\n }\r\n\r\n m_NetworkObjectReference = networkBehaviour.NetworkObject;\r\n m_NetworkBehaviourId = networkBehaviour.NetworkBehaviourId;\r\n }\r\n\r\n public bool TryGet(out T networkBehaviourSubtype, NetworkManager networkManager = null)\r\n {\r\n networkBehaviourSubtype = GetInternal(this, null) as T;\r\n return networkBehaviourSubtype != null;\r\n }\r\n\r\n [MethodImpl(MethodImplOptions.AggressiveInlining)]\r\n private static NetworkBehaviour GetInternal(NetworkBehaviourReference\u003cT> networkBehaviourRef, NetworkManager networkManager = null)\r\n {\r\n if (networkBehaviourRef.m_NetworkObjectReference.TryGet(out NetworkObject networkObject, networkManager))\r\n {\r\n return networkObject.GetNetworkBehaviourAtOrderIndex(networkBehaviourRef.m_NetworkBehaviourId);\r\n }\r\n\r\n return null;\r\n }\r\n\r\n public bool Equals(NetworkBehaviourReference\u003cT> other)\r\n {\r\n return m_NetworkObjectReference.Equals(other.m_NetworkObjectReference) && m_NetworkBehaviourId == other.m_NetworkBehaviourId;\r\n }\r\n\r\n public override bool Equals(object obj)\r\n {\r\n return obj is NetworkBehaviourReference\u003cT> other && Equals(other);\r\n }\r\n\r\n public override int GetHashCode()\r\n {\r\n unchecked\r\n {\r\n return (m_NetworkObjectReference.GetHashCode() * 397) ^ m_NetworkBehaviourId.GetHashCode();\r\n }\r\n }\r\n\r\n public void NetworkSerialize\u003cT>(BufferSerializer\u003cT> serializer) where T : IReaderWriter\r\n {\r\n m_NetworkObjectReference.NetworkSerialize(serializer);\r\n serializer.SerializeValue(ref m_NetworkBehaviourId);\r\n }\r\n\r\n public static implicit operator T(NetworkBehaviourReference\u003cT> networkBehaviourRef) => GetInternal(networkBehaviourRef) as T;\r\n\r\n public static implicit operator NetworkBehaviourReference\u003cT>(T networkBehaviourSubtype) => new NetworkBehaviourReference\u003cT>(networkBehaviourSubtype);\r\n }\r\n}\r\n\r\n```","author":{"url":"https://github.com/zachstronaut","@type":"Person","name":"zachstronaut"},"datePublished":"2024-03-05T00:24:51Z","url":"https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/2837"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6dec7615-6928-1bcf-c129-8b781c88046b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D062:15E965:159703:1E130A:6B0055D0 |
| html-safe-nonce | a31a03c9956590ec227b7508203f246ae6897ff6e5eee4df38766bd5b936b77f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDYyOjE1RTk2NToxNTk3MDM6MUUxMzBBOjZCMDA1NUQwIiwidmlzaXRvcl9pZCI6IjE0MTQyMDMwNTAxMjU0NDg2NTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e6dc2d373e161331ff62b58c9facceab0e3de1891ddae4ab2122da97f301f36b |
| hovercard-subject-tag | issue:2168039617 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/Unity-Technologies/com.unity.netcode.gameobjects/2837/issue_layout |
| twitter:image | https://opengraph.githubassets.com/605cef77b7a5c02b5cf1cf9d9e749d9974e5eb89690af43113347382cbc3fd93/Unity-Technologies/com.unity.netcode.gameobjects/issues/2837 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/605cef77b7a5c02b5cf1cf9d9e749d9974e5eb89690af43113347382cbc3fd93/Unity-Technologies/com.unity.netcode.gameobjects/issues/2837 |
| og:image:alt | It would be amazing if RPC arguments could just actually be any NetworkBehaviour derivative class instead of only NetworkBehaviourReference so that the compiler could enforce type on those RPC argu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zachstronaut |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| turbo-cache-control | no-preview |
| go-import | github.com/Unity-Technologies/com.unity.netcode.gameobjects git https://github.com/Unity-Technologies/com.unity.netcode.gameobjects.git |
| octolytics-dimension-user_id | 426196 |
| octolytics-dimension-user_login | Unity-Technologies |
| octolytics-dimension-repository_id | 116385203 |
| octolytics-dimension-repository_nwo | Unity-Technologies/com.unity.netcode.gameobjects |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 116385203 |
| octolytics-dimension-repository_network_root_nwo | Unity-Technologies/com.unity.netcode.gameobjects |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width