Skip to content

Commit 87658b2

Browse files
committed
Added OnGainedOwnership and OnLostOwnership virtual methods
1 parent 1fe6a74 commit 87658b2

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ public virtual void NetworkStart()
8787

8888
}
8989

90+
public virtual void OnGainedOwnership()
91+
{
92+
93+
}
94+
95+
public virtual void OnLostOwnership()
96+
{
97+
98+
}
99+
90100
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
91101
{
92102
int counter = MessageManager.AddIncomingMessageHandler(name, action, networkId);

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,32 @@ public void ChangeOwnership(int newOwnerClientId)
6161
SpawnManager.ChangeOwnership(NetworkId, newOwnerClientId);
6262
}
6363

64+
internal void InvokeBehaviourOnLostOwnership()
65+
{
66+
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>();
67+
for (int i = 0; i < netBehaviours.Length; i++)
68+
{
69+
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
70+
if (netBehaviours[i].networkedObject == this)
71+
{
72+
netBehaviours[i].OnLostOwnership();
73+
}
74+
}
75+
}
76+
77+
internal void InvokeBehaviourOnGainedOwnership()
78+
{
79+
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>();
80+
for (int i = 0; i < netBehaviours.Length; i++)
81+
{
82+
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
83+
if (netBehaviours[i].networkedObject == this)
84+
{
85+
netBehaviours[i].OnGainedOwnership();
86+
}
87+
}
88+
}
89+
6490
internal void InvokeBehaviourNetworkSpawn()
6591
{
6692
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>();

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,16 @@ private void HandleIncomingData(int clientId, byte[] data, int channelId)
645645
{
646646
uint netId = messageReader.ReadUInt32();
647647
int ownerClientId = messageReader.ReadInt32();
648+
if (SpawnManager.spawnedObjects[netId].OwnerClientId == MyClientId)
649+
{
650+
//We are current owner.
651+
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnLostOwnership();
652+
}
653+
if(ownerClientId == MyClientId)
654+
{
655+
//We are new owner.
656+
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnGainedOwnership();
657+
}
648658
SpawnManager.spawnedObjects[netId].OwnerClientId = ownerClientId;
649659
}
650660
}

0 commit comments

Comments
 (0)