Skip to content

Commit 718b924

Browse files
committed
fix: NetworkedBehaviours can now use OnEnable, OnDisable and OnDestroy
1 parent 4853541 commit 718b924

File tree

2 files changed

+11
-72
lines changed

2 files changed

+11
-72
lines changed

MLAPI/Core/NetworkedBehaviour.cs

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,6 @@ public NetworkedObject NetworkedObject
127127
/// </summary>
128128
public ulong OwnerClientId => NetworkedObject.OwnerClientId;
129129

130-
private void OnEnable()
131-
{
132-
if (_networkedObject == null)
133-
_networkedObject = GetComponentInParent<NetworkedObject>();
134-
135-
OnEnabled();
136-
}
137-
138-
private void OnDisable()
139-
{
140-
OnDisabled();
141-
}
142-
143-
private void OnDestroy()
144-
{
145-
CachedClientRpcs.Remove(this);
146-
CachedServerRpcs.Remove(this);
147-
OnDestroyed();
148-
}
149-
150130
internal bool networkedStartInvoked = false;
151131
/// <summary>
152132
/// Gets called when message handlers are ready to be registered and the networking is setup
@@ -168,58 +148,9 @@ public virtual void NetworkStart(Stream stream)
168148
internal void InternalNetworkStart()
169149
{
170150
CacheAttributes();
171-
WarnUnityReflectionMethodUse();
172151
NetworkedVarInit();
173152
}
174-
175-
private void WarnUnityReflectionMethodUse()
176-
{
177-
if (Debug.isDebugBuild)
178-
{
179-
MethodInfo[] methods = GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
180-
181-
for (int i = 0; i < methods.Length; i++)
182-
{
183-
if (methods[i].Name == "OnDestroy")
184-
{
185-
throw new Exception("The method \"OnDestroy\" is not allowed to be defined in classes that inherit NetworkedBehaviour. Please override the \"OnDestroyed\" method instead");
186-
}
187-
else if (methods[i].Name == "OnDisable")
188-
{
189-
throw new Exception("The method \"OnDisable\" is not allowed to be defined in classes that inherit NetworkedBehaviour. Please override the \"OnDisabled\" method instead");
190-
}
191-
else if (methods[i].Name == "OnEnable")
192-
{
193-
throw new Exception("The method \"OnEnable\" is not allowed to be defined in classes that inherit NetworkedBehaviour. Please override the \"OnEnable\" method instead");
194-
}
195-
}
196-
}
197-
}
198-
199-
/// <summary>
200-
/// Invoked when the object is Disabled
201-
/// </summary>
202-
public virtual void OnDisabled()
203-
{
204-
205-
}
206-
207-
/// <summary>
208-
/// Invoked when the object is Destroyed
209-
/// </summary>
210-
public virtual void OnDestroyed()
211-
{
212-
213-
}
214-
215-
/// <summary>
216-
/// Invoked when the object is Enabled
217-
/// </summary>
218-
public virtual void OnEnabled()
219-
{
220-
221-
}
222-
153+
223154
/// <summary>
224155
/// Gets called when SyncedVars gets updated
225156
/// </summary>
@@ -699,8 +630,8 @@ internal static void SetNetworkedVarData(List<INetworkedVar> networkedVarList, S
699630
#endregion
700631

701632
#region MESSAGING_SYSTEM
702-
private readonly Dictionary<NetworkedBehaviour, Dictionary<ulong, ClientRPCAttribute>> CachedClientRpcs = new Dictionary<NetworkedBehaviour, Dictionary<ulong, ClientRPCAttribute>>();
703-
private readonly Dictionary<NetworkedBehaviour, Dictionary<ulong, ServerRPCAttribute>> CachedServerRpcs = new Dictionary<NetworkedBehaviour, Dictionary<ulong, ServerRPCAttribute>>();
633+
internal readonly Dictionary<NetworkedBehaviour, Dictionary<ulong, ClientRPCAttribute>> CachedClientRpcs = new Dictionary<NetworkedBehaviour, Dictionary<ulong, ClientRPCAttribute>>();
634+
internal readonly Dictionary<NetworkedBehaviour, Dictionary<ulong, ServerRPCAttribute>> CachedServerRpcs = new Dictionary<NetworkedBehaviour, Dictionary<ulong, ServerRPCAttribute>>();
704635
private static readonly Dictionary<Type, MethodInfo[]> Methods = new Dictionary<Type, MethodInfo[]>();
705636
private static readonly Dictionary<ulong, string> HashResults = new Dictionary<ulong, string>();
706637
private static readonly Dictionary<MethodInfo, ulong> methodInfoHashTable = new Dictionary<MethodInfo, ulong>();

MLAPI/Core/NetworkedObject.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,15 @@ public void NetworkHide(ulong clientId)
252252
private void OnDestroy()
253253
{
254254
if (NetworkingManager.Singleton != null)
255+
{
255256
SpawnManager.OnDestroyObject(NetworkId, false);
257+
258+
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
259+
{
260+
childNetworkedBehaviours[i].CachedServerRpcs.Remove(childNetworkedBehaviours[i]);
261+
childNetworkedBehaviours[i].CachedClientRpcs.Remove(childNetworkedBehaviours[i]);
262+
}
263+
}
256264
}
257265

258266
/// <summary>

0 commit comments

Comments
 (0)