How about a NetworkBehaviourReference<T>?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp, unity
- Domain
- game-dev, networking
Research direction
Start by reviewing NetworkBehaviourReference, NetworkObjectReference, and the RPC argument handling described in the issue. Determine whether generic NetworkBehaviour references or direct typed RPC arguments fit the existing serialization and lookup paths; done means typed references are enforced and resolved without the current casts or TryGet boilerplate, with appropriate coverage added.
Written by the indexing model from the issue text.
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 / TryGet code.
Short of that, doing an RPC with NetworkBehaviourReference<MyNetworkBehaviourClass> 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.
I did it like this:
using System;
using System.Runtime.CompilerServices;
namespace Unity.Netcode
{
public struct NetworkBehaviourReference<T> : INetworkSerializable, IEquatable<NetworkBehaviourReference<T>>
where T : NetworkBehaviour
{
private NetworkObjectReference m_NetworkObjectReference;
private ushort m_NetworkBehaviourId;
public NetworkBehaviourReference(NetworkBehaviour networkBehaviour)
{
if (networkBehaviour == null)
{
m_NetworkObjectReference = new NetworkObjectReference((NetworkObject)null);
m_NetworkBehaviourId = 0;
return;
// throw new ArgumentNullException(nameof(networkBehaviour));
}
if (networkBehaviour.NetworkObject == null)
{
throw new ArgumentException($"Cannot create {nameof(NetworkBehaviourReference<T>)} from {nameof(NetworkBehaviour)} without a {nameof(NetworkObject)}.");
}
m_NetworkObjectReference = networkBehaviour.NetworkObject;
m_NetworkBehaviourId = networkBehaviour.NetworkBehaviourId;
}
public bool TryGet(out T networkBehaviourSubtype, NetworkManager networkManager = null)
{
networkBehaviourSubtype = GetInternal(this, null) as T;
return networkBehaviourSubtype != null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static NetworkBehaviour GetInternal(NetworkBehaviourReference<T> networkBehaviourRef, NetworkManager networkManager = null)
{
if (networkBehaviourRef.m_NetworkObjectReference.TryGet(out NetworkObject networkObject, networkManager))
{
return networkObject.GetNetworkBehaviourAtOrderIndex(networkBehaviourRef.m_NetworkBehaviourId);
}
return null;
}
public bool Equals(NetworkBehaviourReference<T> other)
{
return m_NetworkObjectReference.Equals(other.m_NetworkObjectReference) && m_NetworkBehaviourId == other.m_NetworkBehaviourId;
}
public override bool Equals(object obj)
{
return obj is NetworkBehaviourReference<T> other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
return (m_NetworkObjectReference.GetHashCode() * 397) ^ m_NetworkBehaviourId.GetHashCode();
}
}
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
m_NetworkObjectReference.NetworkSerialize(serializer);
serializer.SerializeValue(ref m_NetworkBehaviourId);
}
public static implicit operator T(NetworkBehaviourReference<T> networkBehaviourRef) => GetInternal(networkBehaviourRef) as T;
public static implicit operator NetworkBehaviourReference<T>(T networkBehaviourSubtype) => new NetworkBehaviourReference<T>(networkBehaviourSubtype);
}
}
- Dominant language
- C#
- Stars
- 2.3k
- Forks
- 461
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 20
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Unity-Technologies/com.unity.netcode.gameobjects
-
stat:import type:bug
Difficulty 4/5 3-5 days Newbie friendliness 68/100
-
stat:reply-needed type:support
Difficulty 4/5 3-5 days Newbie friendliness 55/100
Unity-Technologies/com.unity.netcode.gameobjects#4095 · 10 comments ·
-
stat:awaiting-triage stat:Investigating type:bug
Unity-Technologies/com.unity.netcode.gameobjects#3912 · 5 comments · 1 assignee ·
-
Tracking type:feature-2.x
Difficulty 5/5 Over a week Newbie friendliness 35/100
Unity-Technologies/com.unity.netcode.gameobjects#3870 · 5 comments ·
-
Tracking type:feature
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Unity-Technologies/com.unity.netcode.gameobjects#3830 · 7 comments ·
All issues in Unity-Technologies/com.unity.netcode.gameobjects
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·