Skip to content

Commit 5b68637

Browse files
committed
Adds comment for data handling workaround
Clarifies the purpose and usage of the static data map.
1 parent 1f27a68 commit 5b68637

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Spawning/INetworkPrefabInstanceHandlerWithData.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ namespace Unity.Netcode
1010
/// </summary>
1111
public interface INetworkPrefabInstanceHandlerWithData<T> : INetworkPrefabInstanceHandlerWithData where T : struct, INetworkSerializable
1212
{
13-
static readonly Dictionary<INetworkPrefabInstanceHandlerWithData, T> _table = new();
13+
// Propagating custom data through the entire spawn pipeline would add complexity and reduce modularity.
14+
// To work around this, deserialization injects the data into this static map, keyed by the handler instance.
15+
// The handler then reads the data at instantiation time, keeping the interface clean and stateless from the user’s perspective.
16+
// This avoids requiring implementers to store the data explicitly or declare additional fields.
17+
static readonly Dictionary<INetworkPrefabInstanceHandlerWithData, T> _handlerToData = new();
1418

15-
NetworkObject INetworkPrefabInstanceHandler.Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation) => Instantiate(ownerClientId, position, rotation, _table[this]);
16-
void INetworkPrefabInstanceHandlerWithData.RemoveDataEntry(INetworkPrefabInstanceHandlerWithData instance) => _table.Remove(instance);
19+
NetworkObject INetworkPrefabInstanceHandler.Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation) => Instantiate(ownerClientId, position, rotation, _handlerToData[this]);
20+
void INetworkPrefabInstanceHandlerWithData.RemoveDataEntry(INetworkPrefabInstanceHandlerWithData instance) => _handlerToData.Remove(instance);
1721
bool INetworkPrefabInstanceHandlerWithData.HandlesDataType<U>() => typeof(T) == typeof(U);
1822
void INetworkPrefabInstanceHandlerWithData.ReadInstantiationData<RW>(ref BufferSerializer<RW> serializer)
1923
{
20-
_table.TryGetValue(this, out var value);
24+
_handlerToData.TryGetValue(this, out var value);
2125
serializer.SerializeValue(ref value);
22-
_table[this] = value;
26+
_handlerToData[this] = value;
2327
}
2428

2529
NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation, T instantiationData);

0 commit comments

Comments
 (0)